The BIG graph update
[rrdtool.git] / libraries / libart_lgpl-2.3.7 / art_vpath.c
1 /* Libart_LGPL - library of basic graphic primitives
2  * Copyright (C) 1998-2000 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 /* Basic constructors and operations for vector paths */
21
22 #include <math.h>
23 #include <stdlib.h>
24
25 #include "art_misc.h"
26
27 #include "art_rect.h"
28 #include "art_vpath.h"
29
30 /**
31  * art_vpath_add_point: Add point to vpath.
32  * @p_vpath: Where the pointer to the #ArtVpath structure is stored.
33  * @pn_points: Pointer to the number of points in *@p_vpath.
34  * @pn_points_max: Pointer to the number of points allocated.
35  * @code: The pathcode for the new point.
36  * @x: The X coordinate of the new point.
37  * @y: The Y coordinate of the new point.
38  *
39  * Adds a new point to *@p_vpath, reallocating and updating *@p_vpath
40  * and *@pn_points_max as necessary. *@pn_points is incremented.
41  *
42  * This routine always adds the point after all points already in the
43  * vpath. Thus, it should be called in the order the points are
44  * desired.
45  **/
46 void
47 art_vpath_add_point (ArtVpath **p_vpath, int *pn_points, int *pn_points_max,
48                      ArtPathcode code, double x, double y)
49 {
50   int i;
51
52   i = (*pn_points)++;
53   if (i == *pn_points_max)
54     art_expand (*p_vpath, ArtVpath, *pn_points_max);
55   (*p_vpath)[i].code = code;
56   (*p_vpath)[i].x = x;
57   (*p_vpath)[i].y = y;
58 }
59
60 /* number of steps should really depend on radius. */
61 #define CIRCLE_STEPS 128
62
63 /**
64  * art_vpath_new_circle: Create a new circle.
65  * @x: X coordinate of center.
66  * @y: Y coordinate of center.
67  * @r: radius.
68  *
69  * Creates a new polygon closely approximating a circle with center
70  * (@x, @y) and radius @r. Currently, the number of points used in the
71  * approximation is fixed, but that will probably change.
72  *
73  * Return value: The newly created #ArtVpath.
74  **/
75 ArtVpath *
76 art_vpath_new_circle (double x, double y, double r)
77 {
78   int i;
79   ArtVpath *vec;
80   double theta;
81
82   vec = art_new (ArtVpath, CIRCLE_STEPS + 2);
83
84   for (i = 0; i < CIRCLE_STEPS + 1; i++)
85     {
86       vec[i].code = i ? ART_LINETO : ART_MOVETO;
87       theta = (i & (CIRCLE_STEPS - 1)) * (M_PI * 2.0 / CIRCLE_STEPS);
88       vec[i].x = x + r * cos (theta);
89       vec[i].y = y - r * sin (theta);
90     }
91   vec[i].code = ART_END;
92
93   return vec;
94 }
95
96 /**
97  * art_vpath_affine_transform: Affine transform a vpath.
98  * @src: Source vpath to transform.
99  * @matrix: Affine transform.
100  *
101  * Computes the affine transform of the vpath, using @matrix as the
102  * transform. @matrix is stored in the same format as PostScript, ie.
103  * x' = @matrix[0] * x + @matrix[2] * y + @matrix[4]
104  * y' = @matrix[1] * x + @matrix[3] * y + @matrix[5]
105  *
106  * Return value: the newly allocated vpath resulting from the transform.
107 **/
108 ArtVpath *
109 art_vpath_affine_transform (const ArtVpath *src, const double matrix[6])
110 {
111   int i;
112   int size;
113   ArtVpath *new;
114   double x, y;
115
116   for (i = 0; src[i].code != ART_END; i++);
117   size = i;
118
119   new = art_new (ArtVpath, size + 1);
120
121   for (i = 0; i < size; i++)
122     {
123       new[i].code = src[i].code;
124       x = src[i].x;
125       y = src[i].y;
126       new[i].x = matrix[0] * x + matrix[2] * y + matrix[4];
127       new[i].y = matrix[1] * x + matrix[3] * y + matrix[5];
128     }
129   new[i].code = ART_END;
130
131   return new;
132 }
133
134 /**
135  * art_vpath_bbox_drect: Determine bounding box of vpath.
136  * @vec: Source vpath.
137  * @drect: Where to store bounding box.
138  *
139  * Determines bounding box of @vec, and stores it in @drect.
140  **/
141 void
142 art_vpath_bbox_drect (const ArtVpath *vec, ArtDRect *drect)
143 {
144   int i;
145   double x0, y0, x1, y1;
146
147   if (vec[0].code == ART_END)
148     {
149       x0 = y0 = x1 = y1 = 0;
150     }
151   else
152     {
153       x0 = x1 = vec[0].x;
154       y0 = y1 = vec[0].y;
155       for (i = 1; vec[i].code != ART_END; i++)
156         {
157           if (vec[i].x < x0) x0 = vec[i].x;
158           if (vec[i].x > x1) x1 = vec[i].x;
159           if (vec[i].y < y0) y0 = vec[i].y;
160           if (vec[i].y > y1) y1 = vec[i].y;
161         }
162     }
163   drect->x0 = x0;
164   drect->y0 = y0;
165   drect->x1 = x1;
166   drect->y1 = y1;
167 }
168
169 /**
170  * art_vpath_bbox_irect: Determine integer bounding box of vpath.
171  * @vec: Source vpath.
172  * idrect: Where to store bounding box.
173  *
174  * Determines integer bounding box of @vec, and stores it in @irect.
175  **/
176 void
177 art_vpath_bbox_irect (const ArtVpath *vec, ArtIRect *irect)
178 {
179   ArtDRect drect;
180
181   art_vpath_bbox_drect (vec, &drect);
182   art_drect_to_irect (irect, &drect);
183 }
184
185 #define PERTURBATION 2e-3
186
187 /**
188  * art_vpath_perturb: Perturb each point in vpath by small random amount.
189  * @src: Source vpath.
190  *
191  * Perturbs each of the points by a small random amount. This is
192  * helpful for cheating in cases when algorithms haven't attained
193  * numerical stability yet.
194  *
195  * Return value: Newly allocated vpath containing perturbed @src.
196  **/ 
197 ArtVpath *
198 art_vpath_perturb (ArtVpath *src)
199 {
200   int i;
201   int size;
202   ArtVpath *new;
203   double x, y;
204   double x_start, y_start;
205   int open;
206
207   for (i = 0; src[i].code != ART_END; i++);
208   size = i;
209
210   new = art_new (ArtVpath, size + 1);
211
212   x_start = 0;
213   y_start = 0;
214   open = 0;
215   for (i = 0; i < size; i++)
216     {
217       new[i].code = src[i].code;
218       x = src[i].x + (PERTURBATION * rand ()) / RAND_MAX - PERTURBATION * 0.5;
219       y = src[i].y + (PERTURBATION * rand ()) / RAND_MAX - PERTURBATION * 0.5;
220       if (src[i].code == ART_MOVETO)
221         {
222           x_start = x;
223           y_start = y;
224           open = 0;
225         }
226       else if (src[i].code == ART_MOVETO_OPEN)
227         open = 1;
228       if (!open && (i + 1 == size || src[i + 1].code != ART_LINETO))
229         {
230           x = x_start;
231           y = y_start;
232         }
233       new[i].x = x;
234       new[i].y = y;
235     }
236   new[i].code = ART_END;
237
238   return new;
239 }