fixed off-by-one bug
[swftools.git] / lib / gfxpoly / speedtest.c
blob9fe713eec6958233f759a08f4ada1023dee49ee7
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <memory.h>
4 #include <math.h>
5 #include <sys/times.h>
6 #include "../gfxtools.h"
7 #include "poly.h"
8 #include "convert.h"
9 #include "renderpoly.h"
10 #include "stroke.h"
12 #ifdef CHECKS
13 #error "speedtest must be compiled without CHECKS"
14 #endif
16 #ifdef DEBUG
17 #error "speedtest must be compiled without DEBUG"
18 #endif
20 gfxline_t* mkchessboard()
22 gfxline_t*b = 0;
23 int x,y;
24 unsigned int r = 0;
25 int spacing = 20;
27 int num_caros = 40;
28 int l = 5;
29 char do_centerpiece=1;
31 //int num_caros = 4;
32 //int l=1;
33 //char do_centerpiece=0;
35 for(x=-l;x<=l;x++)
36 for(y=-l;y<=l;y++) {
37 /* pseudo random */
38 r = crc32_add_byte(r, x);r = crc32_add_byte(r, y);
39 if(r&1) {
40 gfxline_t*box;
41 if(r&2) {
42 box = gfxline_makerectangle(x*spacing,y*spacing,(x+1)*spacing,(y+1)*spacing);
43 } else {
44 box = gfxline_makerectangle((x+1)*spacing,y*spacing,x*spacing,(y+1)*spacing);
46 b = gfxline_append(b, box);
50 int t;
51 for(t=0;t<num_caros;t++) {
52 r = crc32_add_byte(r, t);
53 int x=(r%10-5)*spacing;
54 int y=((r>>4)%10-5)*spacing;
55 int sizex = ((r>>8)%4)*spacing;
56 int sizey = sizex;
57 if(r&65536)
58 sizex = -sizex;
59 gfxline_t*l = malloc(sizeof(gfxline_t)*5);
60 l[0].type = gfx_moveTo;l[0].next = &l[1];
61 l[1].type = gfx_lineTo;l[1].next = &l[2];
62 l[2].type = gfx_lineTo;l[2].next = &l[3];
63 l[3].type = gfx_lineTo;l[3].next = &l[4];
64 l[4].type = gfx_lineTo;l[4].next = 0;
65 l[0].x = x;
66 l[0].y = y-sizey;
67 l[1].x = x+sizex;
68 l[1].y = y;
69 l[2].x = x;
70 l[2].y = y+sizey;
71 l[3].x = x-sizex;
72 l[3].y = y;
73 l[4].x = x;
74 l[4].y = y-sizey;
75 gfxline_append(b, l);
77 if(do_centerpiece) {
78 for(t=0;t<5;t++) {
79 gfxline_t*l = gfxline_makerectangle(-9*spacing,-10,9*spacing,10);
80 gfxmatrix_t matrix;
81 memset(&matrix, 0, sizeof(gfxmatrix_t));
82 double ua=t*0.43;
83 matrix.m00=cos(ua);matrix.m10=sin(ua);
84 matrix.m01=-sin(ua);matrix.m11=cos(ua);
85 gfxline_transform(l, &matrix);
86 gfxline_append(b, l);
88 gfxline_append(b, gfxline_makecircle(100,100,100,100));
90 return b;
93 gfxline_t* make_circles(gfxline_t*b, int n)
95 unsigned int c = 0;
96 int t;
97 for(t=0;t<n;t++) {
98 c = crc32_add_byte(c, t);
99 int x = c%200;
100 c = crc32_add_byte(c, t);
101 int y = c%200;;
102 c = crc32_add_byte(c, t^0x55);
103 int r = c%100;
104 gfxline_t*c = gfxline_makecircle(x,y,r,r);
105 b = gfxline_append(b, c);
106 //b = gfxline_append(b, gfxline_makerectangle(10,10,100,100));
108 return b;
111 static windcontext_t onepolygon = {1};
112 static windcontext_t twopolygons = {2};
114 int test_speed()
116 gfxline_t* b = mkchessboard();
117 b = make_circles(b, 30);
119 gfxmatrix_t m;
120 memset(&m, 0, sizeof(gfxmatrix_t));
121 int t;
122 for(t=0;t<360;t++) {
123 //printf("%d\n", t);
124 m.m00 = cos(t*M_PI/180.0);
125 m.m01 = sin(t*M_PI/180.0);
126 m.m10 = -sin(t*M_PI/180.0);
127 m.m11 = cos(t*M_PI/180.0);
128 m.tx = 400*1.41/2;
129 m.ty = 400*1.41/2;
130 gfxline_t*l = gfxline_clone(b);
131 gfxline_transform(l, &m);
132 gfxpoly_t*poly = gfxpoly_from_fill(b, 0.05);
134 gfxpoly_t*poly2 = gfxpoly_process(poly, 0, &windrule_evenodd, &onepolygon, 0);
135 gfxpoly_destroy(poly);
136 gfxpoly_destroy(poly2);
137 gfxline_free(l);
139 gfxline_free(b);
142 int main(int argn, char*argv[])
144 struct tms t1,t2;
145 times(&t1);
146 test_speed();
147 times(&t2);
148 printf("%d\n", t2.tms_utime - t1.tms_utime);