1 /* Libart_LGPL - library of basic graphic primitives
2 * Copyright (C) 1998-2000 Raph Levien
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.
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.
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.
20 /* Basic constructors and operations for vector paths */
28 #include "art_vpath.h"
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.
39 * Adds a new point to *@p_vpath, reallocating and updating *@p_vpath
40 * and *@pn_points_max as necessary. *@pn_points is incremented.
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
47 art_vpath_add_point (ArtVpath **p_vpath, int *pn_points, int *pn_points_max,
48 ArtPathcode code, double x, double y)
53 if (i == *pn_points_max)
54 art_expand (*p_vpath, ArtVpath, *pn_points_max);
55 (*p_vpath)[i].code = code;
60 /* number of steps should really depend on radius. */
61 #define CIRCLE_STEPS 128
64 * art_vpath_new_circle: Create a new circle.
65 * @x: X coordinate of center.
66 * @y: Y coordinate of center.
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.
73 * Return value: The newly created #ArtVpath.
76 art_vpath_new_circle (double x, double y, double r)
82 vec = art_new (ArtVpath, CIRCLE_STEPS + 2);
84 for (i = 0; i < CIRCLE_STEPS + 1; i++)
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);
91 vec[i].code = ART_END;
97 * art_vpath_affine_transform: Affine transform a vpath.
98 * @src: Source vpath to transform.
99 * @matrix: Affine transform.
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]
106 * Return value: the newly allocated vpath resulting from the transform.
109 art_vpath_affine_transform (const ArtVpath *src, const double matrix[6])
116 for (i = 0; src[i].code != ART_END; i++);
119 new = art_new (ArtVpath, size + 1);
121 for (i = 0; i < size; i++)
123 new[i].code = src[i].code;
126 new[i].x = matrix[0] * x + matrix[2] * y + matrix[4];
127 new[i].y = matrix[1] * x + matrix[3] * y + matrix[5];
129 new[i].code = ART_END;
135 * art_vpath_bbox_drect: Determine bounding box of vpath.
136 * @vec: Source vpath.
137 * @drect: Where to store bounding box.
139 * Determines bounding box of @vec, and stores it in @drect.
142 art_vpath_bbox_drect (const ArtVpath *vec, ArtDRect *drect)
145 double x0, y0, x1, y1;
147 if (vec[0].code == ART_END)
149 x0 = y0 = x1 = y1 = 0;
155 for (i = 1; vec[i].code != ART_END; i++)
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;
170 * art_vpath_bbox_irect: Determine integer bounding box of vpath.
171 * @vec: Source vpath.
172 * idrect: Where to store bounding box.
174 * Determines integer bounding box of @vec, and stores it in @irect.
177 art_vpath_bbox_irect (const ArtVpath *vec, ArtIRect *irect)
181 art_vpath_bbox_drect (vec, &drect);
182 art_drect_to_irect (irect, &drect);
185 #define PERTURBATION 2e-3
188 * art_vpath_perturb: Perturb each point in vpath by small random amount.
189 * @src: Source vpath.
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.
195 * Return value: Newly allocated vpath containing perturbed @src.
198 art_vpath_perturb (ArtVpath *src)
204 double x_start, y_start;
207 for (i = 0; src[i].code != ART_END; i++);
210 new = art_new (ArtVpath, size + 1);
215 for (i = 0; i < size; i++)
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)
226 else if (src[i].code == ART_MOVETO_OPEN)
228 if (!open && (i + 1 == size || src[i + 1].code != ART_LINETO))
236 new[i].code = ART_END;