Root/
| 1 | /* |
| 2 | * p2d_make.c - Polygon creation |
| 3 | * |
| 4 | * Written 2012 by Werner Almesberger |
| 5 | * Copyright 2012 Werner Almesberger |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2.1 of the License, or (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | |
| 14 | #include <assert.h> |
| 15 | |
| 16 | #include "util.h" |
| 17 | #include "poly2d.h" |
| 18 | |
| 19 | |
| 20 | struct p2d *p2d_new(void) |
| 21 | { |
| 22 | struct p2d *np; |
| 23 | |
| 24 | np = alloc_type(struct p2d); |
| 25 | np->v = NULL; |
| 26 | np->last = NULL; |
| 27 | np->next = NULL; |
| 28 | return np; |
| 29 | } |
| 30 | |
| 31 | |
| 32 | struct v2d *v2d_new(double x, double y) |
| 33 | { |
| 34 | struct v2d *nv; |
| 35 | |
| 36 | nv = alloc_type(struct v2d); |
| 37 | nv->x = x; |
| 38 | nv->y = y; |
| 39 | nv->next = NULL; |
| 40 | return nv; |
| 41 | } |
| 42 | |
| 43 | |
| 44 | void p2d_append(struct p2d *p, struct v2d *v) |
| 45 | { |
| 46 | if (p->last && p->last->next) |
| 47 | v->next = p->v; |
| 48 | if (p->last) |
| 49 | p->last->next = v; |
| 50 | else |
| 51 | p->v = v; |
| 52 | p->last = v; |
| 53 | } |
| 54 | |
| 55 | |
| 56 | void p2d_prepend(struct p2d *p, struct v2d *v) |
| 57 | { |
| 58 | v->next = p->v; |
| 59 | p->v = v; |
| 60 | if (p->last) { |
| 61 | if (p->last->next) |
| 62 | p->last->next = v; |
| 63 | } else { |
| 64 | p->last = v; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | |
| 69 | void p2d_close(struct p2d *p) |
| 70 | { |
| 71 | assert(!p->v || !p->last->next); |
| 72 | p->last->next = p->v; |
| 73 | } |
| 74 |
Branches:
master
