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 */
23 #include "art_vpath.h"
33 * art_vpath_add_point: Add point to vpath.
34 * @p_vpath: Where the pointer to the #ArtVpath structure is stored.
35 * @pn_points: Pointer to the number of points in *@p_vpath.
36 * @pn_points_max: Pointer to the number of points allocated.
37 * @code: The pathcode for the new point.
38 * @x: The X coordinate of the new point.
39 * @y: The Y coordinate of the new point.
41 * Adds a new point to *@p_vpath, reallocating and updating *@p_vpath
42 * and *@pn_points_max as necessary. *@pn_points is incremented.
44 * This routine always adds the point after all points already in the
45 * vpath. Thus, it should be called in the order the points are
49 art_vpath_add_point (ArtVpath
**p_vpath
, int *pn_points
, int *pn_points_max
,
50 ArtPathcode code
, double x
, double y
)
55 if (i
== *pn_points_max
)
56 art_expand (*p_vpath
, ArtVpath
, *pn_points_max
);
57 (*p_vpath
)[i
].code
= code
;
62 /* number of steps should really depend on radius. */
63 #define CIRCLE_STEPS 128
66 * art_vpath_new_circle: Create a new circle.
67 * @x: X coordinate of center.
68 * @y: Y coordinate of center.
71 * Creates a new polygon closely approximating a circle with center
72 * (@x, @y) and radius @r. Currently, the number of points used in the
73 * approximation is fixed, but that will probably change.
75 * Return value: The newly created #ArtVpath.
78 art_vpath_new_circle (double x
, double y
, double r
)
84 vec
= (ArtVpath
*)art_new (ArtVpath
, CIRCLE_STEPS
+ 2);
86 for (i
= 0; i
< CIRCLE_STEPS
+ 1; i
++)
88 vec
[i
].code
= i
? ART_LINETO
: ART_MOVETO
;
89 theta
= (i
& (CIRCLE_STEPS
- 1)) * (M_PI
* 2.0 / CIRCLE_STEPS
);
90 vec
[i
].x
= x
+ r
* cos (theta
);
91 vec
[i
].y
= y
- r
* sin (theta
);
93 vec
[i
].code
= ART_END
;
99 * art_vpath_affine_transform: Affine transform a vpath.
100 * @src: Source vpath to transform.
101 * @matrix: Affine transform.
103 * Computes the affine transform of the vpath, using @matrix as the
104 * transform. @matrix is stored in the same format as PostScript, ie.
105 * x' = @matrix[0] * x + @matrix[2] * y + @matrix[4]
106 * y' = @matrix[1] * x + @matrix[3] * y + @matrix[5]
108 * Return value: the newly allocated vpath resulting from the transform.
111 art_vpath_affine_transform (const ArtVpath
*src
, const double matrix
[6])
118 for (i
= 0; src
[i
].code
!= ART_END
; i
++);
121 xnew
= (ArtVpath
*)art_new (ArtVpath
, size
+ 1);
123 for (i
= 0; i
< size
; i
++)
125 xnew
[i
].code
= src
[i
].code
;
128 xnew
[i
].x
= matrix
[0] * x
+ matrix
[2] * y
+ matrix
[4];
129 xnew
[i
].y
= matrix
[1] * x
+ matrix
[3] * y
+ matrix
[5];
131 xnew
[i
].code
= ART_END
;
137 * art_vpath_bbox_drect: Determine bounding box of vpath.
138 * @vec: Source vpath.
139 * @drect: Where to store bounding box.
141 * Determines bounding box of @vec, and stores it in @drect.
144 art_vpath_bbox_drect (const ArtVpath
*vec
, ArtDRect
*drect
)
147 double x0
, y0
, x1
, y1
;
149 if (vec
[0].code
== ART_END
)
151 x0
= y0
= x1
= y1
= 0;
157 for (i
= 1; vec
[i
].code
!= ART_END
; i
++)
159 if (vec
[i
].x
< x0
) x0
= vec
[i
].x
;
160 if (vec
[i
].x
> x1
) x1
= vec
[i
].x
;
161 if (vec
[i
].y
< y0
) y0
= vec
[i
].y
;
162 if (vec
[i
].y
> y1
) y1
= vec
[i
].y
;
172 * art_vpath_bbox_irect: Determine integer bounding box of vpath.
173 * @vec: Source vpath.
174 * idrect: Where to store bounding box.
176 * Determines integer bounding box of @vec, and stores it in @irect.
179 art_vpath_bbox_irect (const ArtVpath
*vec
, ArtIRect
*irect
)
183 art_vpath_bbox_drect (vec
, &drect
);
184 art_drect_to_irect (irect
, &drect
);
187 #define PERTURBATION 2e-3
190 * art_vpath_perturb: Perturb each point in vpath by small random amount.
191 * @src: Source vpath.
193 * Perturbs each of the points by a small random amount. This is
194 * helpful for cheating in cases when algorithms haven't attained
195 * numerical stability yet.
197 * Return value: Newly allocated vpath containing perturbed @src.
200 art_vpath_perturb (ArtVpath
*src
)
206 double x_start
, y_start
;
209 for (i
= 0; src
[i
].code
!= ART_END
; i
++);
212 xnew
= (ArtVpath
*)art_new (ArtVpath
, size
+ 1);
217 for (i
= 0; i
< size
; i
++)
219 xnew
[i
].code
= src
[i
].code
;
220 x
= src
[i
].x
+ (PERTURBATION
* rand ()) / RAND_MAX
- PERTURBATION
* 0.5;
221 y
= src
[i
].y
+ (PERTURBATION
* rand ()) / RAND_MAX
- PERTURBATION
* 0.5;
222 if (src
[i
].code
== ART_MOVETO
)
228 else if (src
[i
].code
== ART_MOVETO_OPEN
)
230 if (!open
&& (i
+ 1 == size
|| src
[i
+ 1].code
!= ART_LINETO
))
238 xnew
[i
].code
= ART_END
;