/*
 * p2d_area.c - Fill a set of nested polygons
 *
 * Written 2012 by Werner Almesberger
 * Copyright 2012 Werner Almesberger
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */


#include "poly2d.h"
#include "p2d_hsort.h"


static void recurse_area(struct p2d_hier *t, double offset, double overlap,
    struct p2d ***last)
{
	const struct p2d *p, *h;

	for (p = &t->p; p; p = p->next) {
		h = &p2d_to_hier(p)->holes->p;
		p2d_area_holes_append(p, h, offset, overlap, last);
		while (h) {
			recurse_area(p2d_to_hier(h)->holes, offset, overlap,
			    last);
			h = h->next;
		}
	}
}


struct p2d *p2d_area(const struct p2d *p, double first, double next)
{
	struct p2d_hier *t;
	struct p2d *res = NULL, **last = &res;

	t = p2d_hsort(p);
	recurse_area(t, first, next, &last);
	p2d_hier_free(t);
	return res;
}