numerous bigfixes in stroke->polygon conversion
[swftools.git] / lib / gfxpoly / test_stroke.c
1 #include <math.h>
2 #include "../gfxtools.h"
3 #include "stroke.h"
4
5 int main()
6 {
7     gfxline_t l[512], f[256*5];
8
9     int width=700,height=700;
10     gfxdevice_t dev;
11     gfxdevice_swf_init(&dev);
12     dev.setparameter(&dev, "framerate", "25.0");
13     int t;
14     for(t=0;t<300;t++) {
15         dev.startpage(&dev, 700,700);
16         /*gfxline_t*g = l;
17         while(g) {
18             g->x += g->sx;
19             g->y += g->sy;
20             if(g->sx || g->sy) {
21                 if(g->x<200) {g->x=400-g->x;g->sx=-g->sx;}
22                 if(g->y<200) {g->y=400-g->y;g->sy=-g->sy;}
23                 if(g->x>500) {g->x=1000-g->x;g->sx=-g->sx;}
24                 if(g->y>500) {g->y=1000-g->y;g->sy=-g->sy;}
25             }
26             g = g->next;
27         }*/
28
29         int i;
30         for(i=0;i<512;i++) {
31             double a = i*0.05+t*M_PI/150;
32             double r = 50+i*0.5;
33             l[i].x = cos(a)*r + width/2;
34             l[i].y = sin(a)*r + height/2;
35             l[i].sx = (int)((l[i].x-width/2)/30);
36             l[i].sy = (int)((l[i].y-height/2)/30);
37             l[i].sx = l[i].sy = 0;
38             l[i].type = gfx_lineTo;
39             l[i].next = &l[i+1];
40         }
41         l[0].type = gfx_moveTo;
42         l[i-1].next = 0;
43
44         int xx,yy;
45         i = 0;
46         for(yy=0;yy<16;yy++) 
47         for(xx=0;xx<16;xx++) if((xx^yy)&1) {
48             double x = -128+xx*64+t*128.0/300;
49             double y = -128+yy*64;//+t*64.0/300;
50             f[i].x = x;
51             f[i].y = y;
52             f[i].next = &f[i+1];f[i++].type = gfx_moveTo;
53             f[i].x = x+64;
54             f[i].y = y;
55             f[i].next = &f[i+1];f[i++].type = gfx_lineTo;
56             f[i].x = x+64;
57             f[i].y = y+64;
58             f[i].next = &f[i+1];f[i++].type = gfx_lineTo;
59             f[i].x = x;
60             f[i].y = y+64;
61             f[i].next = &f[i+1];f[i++].type = gfx_lineTo;
62             f[i].x = x;
63             f[i].y = y;
64             f[i].next = &f[i+1];f[i++].type = gfx_lineTo;
65         }
66         f[i-1].next = 0;
67
68         double width = 40;
69         
70         //gfxdrawer_t d;
71         //gfxdrawer_target_gfxline(&d);
72         //draw_stroke(l, &d, width, gfx_capRound, gfx_joinBevel, 500);
73         //gfxline_t*line = (gfxline_t*)d.result(&d);
74
75         //gfxpoly_t*p = gfxpoly_fromstroke(l, width, gfx_capRound, gfx_joinRound, 500);
76         gfxpoly_t*p1 = gfxpoly_from_stroke(l, width, gfx_capRound, gfx_joinRound, 500, 0.05);
77         assert(gfxpoly_check(p1));
78         
79         //gfxpoly_t*p2 = gfxpoly_from_fill(f, 0.05);
80         gfxline_t*l2 = gfxline_clone(l);
81
82         double c = cos(t*M_PI/75);
83         double s = sin(t*M_PI/75);
84         static int x1 = 0, xdir = 1;
85         static int y1 = 0, ydir = 5;
86         x1+=xdir; if(x1>=150)  {x1=300-x1;xdir=-xdir;} if(x1<-150) {x1=-300-x1;xdir=-xdir;}
87         y1+=ydir; if(y1>=150)  {y1=300-y1;ydir=-ydir;} if(y1<-150) {y1=-300-y1;ydir=-ydir;}
88         gfxmatrix_t m = { c, s, -(350+x1)*c-350*s+350,
89                          -s, c,  350*s-(350+x1)*c+350};
90         gfxline_transform(l2, &m);
91         gfxpoly_t*p2 = gfxpoly_from_stroke(l2, width, gfx_capRound, gfx_joinRound, 500, 0.05);
92         assert(gfxpoly_check(p2));
93
94         gfxpoly_t*p3 = gfxpoly_intersect(p1, p2);
95         assert(gfxpoly_check(p3));
96         
97         //gfxpoly_t*p4 = gfxpoly_from_fill(f, 0.05);
98         //gfxpoly_t*p5 = gfxpoly_intersect(p1, p4);
99
100         gfxline_t*line = gfxline_from_gfxpoly(p3);
101         gfxpoly_destroy(p1);
102         gfxpoly_destroy(p2);
103         gfxpoly_destroy(p3);
104
105         //gfxline_dump(line, stdout, "");
106
107         gfxcolor_t blue = {255,0,0,255};
108         gfxline_t*r = gfxline_makerectangle(0,0,700,700);
109         dev.fill(&dev, r, &blue);
110
111         gfxcolor_t black = {255,0,0,64};
112         gfxcolor_t white = {255,255,255,255};
113         gfxcolor_t cyan = {255,0,192,192};
114         //dev.stroke(&dev, l, 2, &black, gfx_capRound, gfx_joinRound, 0);
115         //dev.stroke(&dev, line, 2, &cyan, gfx_capRound, gfx_joinRound, 0);
116         dev.fill(&dev, line, &black);
117         gfxmatrix_t m2 = {1.0,   0, -10,
118                            0, 1.0, -10};
119         gfxline_transform(line, &m2);
120         dev.fill(&dev, line, &cyan);
121         dev.stroke(&dev, line, 2.0, &white, gfx_capRound, gfx_joinRound, 0);
122
123         gfxline_free(line);
124         dev.endpage(&dev);
125     }
126
127     gfxresult_t* result = dev.finish(&dev);
128     result->save(result, "test.swf");
129     result->destroy(result);
130 }