cae-tools/poly2d/p2d_copy.c

96 lines
1.5 KiB
C

/*
* p2d_copy.c - Copy a polygon, with or without reversing it
*
* Written 2012 by Werner Almesberger
* Copyright 2012 Werner Almesberger
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*/
#include <math.h>
#include "util.h"
#include "poly2d.h"
struct p2d *p2d_copy(const struct p2d *p)
{
struct p2d *np;
const struct v2d *v;
struct v2d *nv, **last;
np = alloc_type(struct p2d);
np->v = NULL;
np->last = NULL;
np->next = NULL;
last = &np->v;
v = p->v;
while (v) {
nv = alloc_type(struct v2d);
nv->x = v->x;
nv->y = v->y;
nv->next = NULL;
*last = nv;
last = &nv->next;
np->last = nv;
v = v->next;
if (v == p->v)
break;
}
if (v)
*last = np->v;
return np;
}
struct p2d *p2d_copy_all(const struct p2d *p)
{
struct p2d *res = NULL, **last = &res;
while (p) {
*last = p2d_copy(p);
last = &(*last)->next;
p = p->next;
}
return res;
}
struct p2d *p2d_reverse(const struct p2d *p)
{
struct p2d *np;
const struct v2d *v;
struct v2d *nv;
np = alloc_type(struct p2d);
np->v = NULL;
np->last = NULL;
np->next = NULL;
v = p->v;
while (v) {
nv = alloc_type(struct v2d);
nv->x = v->x;
nv->y = v->y;
nv->next = np->v;
np->v = nv;
if (!np->last)
np->last= nv;
v = v->next;
if (v == p->v)
break;
}
if (v && np->last)
np->last->next = np->v;
return np;
}