1 /* CPML - Cairo Path Manipulation Library
2 * Copyright (C) 2008,2009,2010 Nicola Fontana <ntd at entidi.it>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser 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 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
23 * @Section_Id:CpmlPair
25 * @short_description: Basic struct holding a couple of values
27 * The #CpmlPair is a generic 2D structure. It can be used to represent
28 * coordinates, sizes, offsets or whatever have two components.
30 * The name comes from MetaFont.
35 * @x: the x component of the pair
36 * @y: the y component of the pair
38 * A generic 2D structure.
43 * @x: the x component of the vector
44 * @y: the y component of the vector
46 * Another name for #CpmlPair. It is used to clarify when a function
47 * expects a generic pair (usually coordinates) or a vector.
49 * A vector represents a line starting from the origin (0,0) and ending
50 * to the given coordinates pair. Vectors are useful to define direction
51 * and length at once. Keep in mind the cairo default coordinates system
52 * is not problably what you expect: the x axis increases at right
53 * (as usual) but the y axis increases at down (the reverse of a usual
54 * cartesian plan). An angle of 0 is at V=(1; 0) (middle right).
58 #include "cpml-internal.h"
63 static CpmlPair fallback_pair
= { 0, 0 };
67 * cpml_pair_from_cairo:
68 * @pair: the destination #CpmlPair
69 * @path_data: the original path data point
71 * Sets @pair from a #cairo_path_data_t struct. @path_data should contains
72 * a point data: it is up to the caller to be sure @path_data is valid.
75 cpml_pair_from_cairo(CpmlPair
*pair
, const cairo_path_data_t
*path_data
)
77 pair
->x
= path_data
->point
.x
;
78 pair
->y
= path_data
->point
.y
;
83 * @pair: the destination #CpmlPair
84 * @src: the source #CpmlPair
86 * Copies @src in @pair.
89 cpml_pair_copy(CpmlPair
*pair
, const CpmlPair
*src
)
91 memcpy(pair
, src
, sizeof(CpmlPair
));
95 * cpml_pair_transform:
96 * @pair: the destination #CpmlPair struct
97 * @matrix: the transformation matrix
99 * Shortcut to apply a specific transformation matrix to @pair.
102 cpml_pair_transform(CpmlPair
*pair
, const cairo_matrix_t
*matrix
)
104 cairo_matrix_transform_point(matrix
, &pair
->x
, &pair
->y
);
108 * cpml_pair_squared_distance:
109 * @from: the first #CpmlPair struct
110 * @to: the second #CpmlPair struct
112 * Gets the squared distance between @from and @to. This value is useful
113 * for comparation purpose: if you need to get the real distance, use
114 * cpml_pair_distance().
116 * @from or @to could be %NULL, in which case the fallback (0, 0) pair
119 * Returns: the squared distance
122 cpml_pair_squared_distance(const CpmlPair
*from
, const CpmlPair
*to
)
127 from
= &fallback_pair
;
134 return x
* x
+ y
* y
;
138 * cpml_pair_distance:
139 * @from: the first #CpmlPair struct
140 * @to: the second #CpmlPair struct
141 * @distance: where to store the result
143 * Gets the distance between @from and @to, storing the result in @distance.
144 * If you need this value only for comparation purpose, you could use
145 * cpm_pair_squared_distance() instead.
147 * @from or @to could be %NULL, in which case the fallback (0, 0) pair
150 * The algorithm used is adapted from:
151 * "Replacing Square Roots by Pythagorean Sums"
152 * by Clave Moler and Donald Morrison (1983)
153 * IBM Journal of Research and Development 27 (6): 577–581
154 * http://www.research.ibm.com/journal/rd/276/ibmrd2706P.pdf
156 * Returns: the distance
159 cpml_pair_distance(const CpmlPair
*from
, const CpmlPair
*to
)
165 from
= &fallback_pair
;
202 * cpml_pair_to_cairo:
203 * @pair: the destination #CpmlPair
204 * @path_data: the original path data point
206 * Sets a #cairo_path_data_t struct to @pair. This is exactly the reverse
207 * operation of cpml_pair_from_cairo().
210 cpml_pair_to_cairo(const CpmlPair
*pair
, cairo_path_data_t
*path_data
)
212 path_data
->point
.x
= pair
->x
;
213 path_data
->point
.y
= pair
->y
;
218 * cpml_vector_from_angle:
219 * @vector: the destination #CpmlVector
220 * @angle: angle of direction, in radians
222 * Calculates the coordinates of the point far %1 from the origin
223 * in the @angle direction. The result is stored in @vector.
226 cpml_vector_from_angle(CpmlVector
*vector
, double angle
)
228 /* Check for common conditions */
229 if (angle
== -M_PI_2
) {
232 } else if (angle
== M_PI_2
) {
235 } else if (angle
== M_PI
|| angle
== -M_PI
) {
238 } else if (angle
== 0) {
242 vector
->x
= cos(angle
);
243 vector
->y
= sin(angle
);
248 * cpml_vector_set_length:
249 * @vector: a #CpmlVector
250 * @length: the new length
252 * Imposes the specified @length to @vector. If the old length is %0
253 * (and so the direction is not known), nothing happens. If @length
254 * is %0, @vector is set to (0, 0).
256 * The @length parameter can be negative, in which case the vector
260 cpml_vector_set_length(CpmlVector
*vector
, double length
)
270 divisor
= cpml_pair_distance(NULL
, vector
);
272 /* Check for valid length (anything other than 0) */
275 vector
->x
/= divisor
;
276 vector
->y
/= divisor
;
282 * @vector: the source #CpmlVector
284 * Gets the angle of @vector, in radians. If @vector is (0, 0),
287 * Returns: the angle in radians, a value between -M_PI and M_PI
290 cpml_vector_angle(const CpmlVector
*vector
)
292 /* Check for common conditions */
294 return vector
->x
>= 0 ? 0 : M_PI
;
296 return vector
->y
> 0 ? M_PI_2
: -M_PI_2
;
297 if (vector
->x
== vector
->y
)
298 return vector
->x
> 0 ? M_PI_4
: -M_PI_4
* 3;
299 if (vector
->x
== -vector
->y
)
300 return vector
->x
> 0 ? -M_PI_4
: M_PI_4
* 3;
302 return atan2(vector
->y
, vector
->x
);
306 * cpml_vector_normal:
307 * @vector: the subject #CpmlVector
309 * Stores in @vector a vector normal to the original @vector.
310 * The length is retained.
312 * The algorithm is really quick because no trigonometry is involved.
315 cpml_vector_normal(CpmlVector
*vector
)
317 double tmp
= vector
->x
;
319 vector
->x
= -vector
->y
;
324 * cpml_vector_transform:
325 * @vector: the destination #CpmlPair struct
326 * @matrix: the transformation matrix
328 * Shortcut to apply a specific transformation matrix to @vector.
329 * It works in a similar way of cpml_pair_transform() but uses
330 * cairo_matrix_transform_distance() instead of
331 * cairo_matrix_transform_point().
334 cpml_vector_transform(CpmlPair
*vector
, const cairo_matrix_t
*matrix
)
336 cairo_matrix_transform_distance(matrix
, &vector
->x
, &vector
->y
);