The BIG graph update
[rrdtool.git] / libraries / libart_lgpl-2.3.7 / testuta.c
1 /* Libart_LGPL - library of basic graphic primitives
2  * Copyright (C) 1998 Raph Levien
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <math.h>
23 #include "art_misc.h"
24 #include "art_uta.h"
25 #include "art_vpath.h"
26 #include "art_uta_vpath.h"
27 #include "art_rect.h"
28 #include "art_rect_uta.h"
29 #include "art_uta_rect.h"
30
31 #define TEST_UTA
32 #define noTEST_UTA_SPEED
33
34 #define XOFF 50
35 #define YOFF 700
36
37 static void
38 print_uta_ps (ArtUta *uta)
39 {
40   int x, y;
41   int x0, y0, x1, y1;
42   int width = uta->width;
43   ArtUtaBbox ub;
44
45   for (y = 0; y < uta->height; y++)
46     for (x = 0; x < width; x++)
47       {
48         ub = uta->utiles[y * width + x];
49         if (ub != 0)
50           {
51             x0 = (uta->x0 + x) * ART_UTILE_SIZE + ART_UTA_BBOX_X0(ub);
52             x1 = (uta->x0 + x) * ART_UTILE_SIZE + ART_UTA_BBOX_X1(ub);
53             y0 = (uta->y0 + y) * ART_UTILE_SIZE + ART_UTA_BBOX_Y0(ub);
54             y1 = (uta->y0 + y) * ART_UTILE_SIZE + ART_UTA_BBOX_Y1(ub);
55             printf ("%% tile %d, %d: %d %d %d %d\n",
56                     x, y,
57                     ART_UTA_BBOX_X0(ub),
58                     ART_UTA_BBOX_Y0(ub),
59                     ART_UTA_BBOX_X1(ub),
60                     ART_UTA_BBOX_Y1(ub));
61             printf ("%d %d moveto %d %d lineto %d %d lineto %d %d lineto closepath fill\n",
62                     XOFF + x0, YOFF - y0, XOFF + x1, YOFF - y0,
63                     XOFF + x1, YOFF - y1, XOFF + x0, YOFF - y1);
64           }
65       }
66 }
67
68 static void
69 print_rbuf_ps (int *rbuf, int width, int height)
70 {
71   int x, y;
72
73   for (y = 0; y < height; y++)
74     for (x = 0; x < width; x++)
75       if (1 && rbuf[y * width + x] != 0)
76         printf ("%d %d moveto (%d) show\n", x * ART_UTILE_SIZE, y * ART_UTILE_SIZE,
77                 rbuf[y * width + x]);   
78 }
79
80 #if 0
81 void
82 randline (ArtUta *uta, int *rbuf, int rbuf_rowstride)
83 {
84   double x0, y0, x1, y1;
85
86   x0 = rand () * (500.0 / RAND_MAX);
87   y0 = rand () * (500.0 / RAND_MAX);
88   x1 = rand () * (500.0 / RAND_MAX);
89   y1 = rand () * (500.0 / RAND_MAX);
90
91   printf ("%g %g moveto %g %g lineto stroke\n", x0, y0, x1, y1);
92   art_uta_add_line (uta, x0, y0, x1, y1, rbuf, rbuf_rowstride);
93 }
94 #endif
95
96 static void
97 print_ps_vpath (ArtVpath *vpath)
98 {
99   int i;
100
101   for (i = 0; vpath[i].code != ART_END; i++)
102     {
103       switch (vpath[i].code)
104         {
105         case ART_MOVETO:
106           printf ("%g %g moveto\n", XOFF + vpath[i].x, YOFF - vpath[i].y);
107           break;
108         case ART_LINETO:
109           printf ("%g %g lineto\n", XOFF + vpath[i].x, YOFF - vpath[i].y);
110           break;
111         default:
112           break;
113         }
114     }
115   printf ("stroke\n");
116 }
117
118 static ArtVpath *
119 randstar (int n)
120 {
121   ArtVpath *vec;
122   int i;
123   double r, th;
124
125   vec = art_new (ArtVpath, n + 2);
126   for (i = 0; i < n; i++)
127     {
128       vec[i].code = i ? ART_LINETO : ART_MOVETO;
129       r = rand () * (250.0 / RAND_MAX);
130       th = i * 2 * M_PI / n;
131       vec[i].x = 250 + r * cos (th);
132       vec[i].y = 250 - r * sin (th);
133     }
134   vec[i].code = ART_LINETO;
135   vec[i].x = vec[0].x;
136   vec[i].y = vec[0].y;
137   i++;
138   vec[i].code = ART_END;
139   vec[i].x = 0;
140   vec[i].y = 0;
141   return vec;
142 }
143
144 int
145 main (int argc, char **argv)
146 {
147   ArtUta *uta;
148   int i;
149   int *rbuf;
150   ArtVpath *vec;
151   ArtIRect *rects;
152   int n_rects;
153
154   if (argc == 2)
155     srand (atoi (argv[1]));
156
157 #ifdef TEST_UTA
158   printf ("%%!PS-Adobe\n");
159   printf ("/Helvetica findfont 12 scalefont setfont\n");
160   printf ("0.5 setlinewidth\n");
161
162   printf ("0.5 setgray\n");
163   for (i = 0; i < 500; i += ART_UTILE_SIZE)
164     {
165       printf ("%d %d moveto %d %d lineto stroke\n",
166               XOFF, YOFF - i, XOFF + 500, YOFF - i);
167       printf ("%d %d moveto %d %d lineto stroke\n",
168               XOFF + i, YOFF, XOFF + i, YOFF - 500);
169     }
170
171   printf ("/a {\n");
172
173 #if 1
174   vec = randstar (50);
175   print_ps_vpath (vec);
176   uta = art_uta_from_vpath (vec);
177 #ifdef TEST_UTA_RECT
178   {
179     ArtIRect bbox = {5, 5, 450, 450};
180     uta = art_uta_from_irect (&bbox);
181   }
182 #endif
183   rbuf = 0;
184 #else
185   uta = art_uta_new_coords (0, 0, 500, 500);
186
187   rbuf = malloc (sizeof(int) * (500 >> ART_UTILE_SHIFT) * (500 >> ART_UTILE_SHIFT));
188   for (i = 0; i < 10; i++)
189     randline (uta, rbuf, 500 >> ART_UTILE_SHIFT);
190 #endif
191
192   printf ("} def 1 0.5 0.5 setrgbcolor\n");
193
194   print_uta_ps (uta);
195
196   printf ("0 0 0.5 setrgbcolor\n");
197
198   if (rbuf)
199     print_rbuf_ps (rbuf, 500 >> ART_UTILE_SHIFT, 500 >> ART_UTILE_SHIFT);
200
201   printf ("0 setgray a\n");
202
203   rects = art_rect_list_from_uta (uta, 256, 64, &n_rects);
204
205   printf ("%% %d rectangles:\n0 0 1 setrgbcolor\n", n_rects);
206
207   for (i = 0; i < n_rects; i++)
208     printf ("%d %d moveto %d %d lineto %d %d lineto %d %d lineto closepath stroke\n",
209             XOFF + rects[i].x0, YOFF - rects[i].y0,
210             XOFF + rects[i].x1, YOFF - rects[i].y0,
211             XOFF + rects[i].x1, YOFF - rects[i].y1,
212             XOFF + rects[i].x0, YOFF - rects[i].y1);
213
214   printf ("showpage\n");
215 #endif
216
217 #ifdef TEST_UTA_SPEED
218   for (i = 0; i < 1000; i++)
219     {
220       vec = randstar (50);
221       uta = art_uta_from_vpath (vec);
222       art_free (vec);
223       art_uta_free (uta);
224     }
225 #endif
226
227   return 0;
228 }