first version of new polygon intersector
[swftools.git] / lib / gfxpoly / convert.c
1 #include <stdlib.h>
2 #include <math.h>
3 #include <assert.h>
4 #include "../gfxdevice.h"
5 #include "poly.h"
6
7 static inline void gfxpoly_add_segment(segment_t**list, double _x1, double _y1, double _x2, double _y2)
8 {
9     int x1 = ceil(_x1);
10     int y1 = ceil(_y1);
11     int x2 = ceil(_x2);
12     int y2 = ceil(_y2);
13     if(y1!=y2) {
14         segment_t*s = segment_new(x1, y1, x2, y2);
15         segment_insert_before(list, s);
16     }
17 }
18
19 gfxpoly_t* gfxpoly_fillToPoly(gfxline_t*line)
20 {
21     segment_t*s = 0;
22
23     /* factor that determines into how many line fragments a spline is converted */
24     double subfraction = 2.4;//0.3
25
26     double lastx=0, lasty=0;
27     assert(!line || line[0].type == gfx_moveTo);
28     while(line) {
29         if(line->type == gfx_moveTo) {
30         } else if(line->type == gfx_lineTo) {
31             gfxpoly_add_segment(&s, lastx, lasty, line->x, line->y);
32         } else if(line->type == gfx_splineTo) {
33             int parts = (int)(sqrt(fabs(line->x-2*line->sx+lastx) + 
34                                    fabs(line->y-2*line->sy+lasty))*subfraction);
35             if(!parts) parts = 1;
36             double stepsize = 1.0/parts;
37             int i;
38             for(i=0;i<parts;i++) {
39                 double t = (double)i*stepsize;
40                 double x = line->x*t*t + 2*line->sx*t*(1-t) + x*(1-t)*(1-t);
41                 double y = line->y*t*t + 2*line->sy*t*(1-t) + y*(1-t)*(1-t);
42                 gfxpoly_add_segment(&s, lastx, lasty, x, y);
43                 lastx = x;
44                 lasty = y;
45             }
46             gfxpoly_add_segment(&s, lastx, lasty, line->x, line->y);
47         }
48         lastx = line->x;
49         lasty = line->y;
50         line = line->next;
51     }
52
53     gfxline_free(line);
54     return s;
55 }
56