first version of new polygon intersector
[swftools.git] / lib / gfxpoly / xrow.c
1 #include "../mem.h"
2 #include "xrow.h"
3
4 xrow_t* xrow_new()
5 {
6     xrow_t*r = rfx_calloc(sizeof(xrow_t));
7     r->size = 16;
8     r->x = rfx_alloc(sizeof(r->x[0])*r->size);
9     return r;
10 }
11
12 void xrow_add(xrow_t*r, int32_t x)
13 {
14     if(r->num >= r->size) {
15         r->size *= 2;
16         r->x = rfx_realloc(r->x, sizeof(r->x[0])*r->size);
17     }
18     r->x[r->num++]=x;
19 }
20
21 int compare_int32(const void*_i1,const void*_i2)
22 {
23     int32_t*i1 = (int32_t*)_i1;
24     int32_t*i2 = (int32_t*)_i2;
25     return i1-i2;
26 }
27
28 void xrow_sort(xrow_t*r)
29 {
30     if(!r->num)
31         return;
32     qsort(r->x, r->num, sizeof(r->x[0]), compare_int32);
33     int t;
34     int pos = 1;
35     int32_t lastx=r->x[0];
36     for(t=1;t<r->num;t++) {
37         if(r->x[t]!=lastx) {
38             r->x[pos++] = lastx = r->x[t];
39         }
40     }
41     r->num = pos;
42 }
43
44 void xrow_reset(xrow_t*r)
45 {
46     r->num = 0;
47 }
48
49 void xrow_destroy(xrow_t*r)
50 {
51     if(r->x) {
52         free(r->x);r->x = 0;
53     }
54     free(r);
55 }