1 /* CPML - Cairo Path Manipulation Library
2 * Copyright (C) 2008, 2009 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 expects
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 directions
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-pair.h"
65 static CpmlPair fallback_pair
= { 0, 0 };
70 * @pair: the destination #CpmlPair
71 * @src: the source #CpmlPair
73 * Copies @src in @pair.
78 cpml_pair_copy(CpmlPair
*pair
, const CpmlPair
*src
)
80 return memcpy(pair
, src
, sizeof(CpmlPair
));
84 * cpml_pair_from_cairo:
85 * @pair: the destination #CpmlPair
86 * @path_data: the original path data point
88 * Gets @pair from a #cairo_path_data_t struct. @path_data should contains
89 * a point data: it is up to the caller to be sure @path_data is valid.
94 cpml_pair_from_cairo(CpmlPair
*pair
, const cairo_path_data_t
*path_data
)
96 pair
->x
= path_data
->point
.x
;
97 pair
->y
= path_data
->point
.y
;
106 * Negates the coordinates of @pair.
109 cpml_pair_negate(CpmlPair
*pair
)
119 * Inverts (1/x) the coordinates of @pair. If @pair cannot be inverted
120 * because one coordinate is 0, 0 is returned and no transformation is
123 * Return value: 1 on success, 0 on errors
126 cpml_pair_invert(CpmlPair
*pair
)
128 if (pair
->x
== 0 || pair
->y
== 0)
131 pair
->x
= 1. / pair
->x
;
132 pair
->y
= 1. / pair
->y
;
138 * @pair: the destination #CpmlPair
139 * @src: the source pair to add
141 * Adds @src to @pair and stores the result in @pair. In other words,
142 * @pair = @pair + @src.
145 cpml_pair_add(CpmlPair
*pair
, const CpmlPair
*src
)
153 * @pair: the destination #CpmlPair
154 * @src: the source pair to subtract
156 * Subtracts @src from @pair and stores the result in @pair. In other words,
157 * @pair = @pair - @src.
160 cpml_pair_sub(CpmlPair
*pair
, const CpmlPair
*src
)
168 * @pair: the destination #CpmlPair
169 * @src: the source pair factor
171 * Multiplies the x coordinate of @pair by the @src x factor and the
172 * y coordinate by the @src y factor.
175 cpml_pair_mul(CpmlPair
*pair
, const CpmlPair
*src
)
183 * @pair: the destination #CpmlPair
184 * @src: the source pair divisor
186 * Divides the x coordinate of @pair by the @src x divisor and the
187 * y coordinate by the @src y divisor. If @pair cannot be divided
188 * because of a division by 0, 0 is returned and no transformation
191 * Return value: 1 on success, 0 on errors
194 cpml_pair_div(CpmlPair
*pair
, const CpmlPair
*src
)
196 if (src
->x
== 0 || src
->y
== 0)
205 * cpml_pair_transform:
206 * @pair: the destination #CpmlPair struct
207 * @matrix: the transformation matrix
209 * Shortcut to apply a specific transformation matrix to @pair.
212 cpml_pair_transform(CpmlPair
*pair
, const cairo_matrix_t
*matrix
)
214 cairo_matrix_transform_point(matrix
, &pair
->x
, &pair
->y
);
219 * cpml_pair_squared_distance:
220 * @from: the first #CpmlPair struct
221 * @to: the second #CpmlPair struct
223 * Gets the squared distance between @from and @to. This value is useful
224 * for comparation purpose: if you need to get the real distance, use
225 * cpml_pair_distance().
227 * @from or @to could be %NULL, in which case the fallback (0, 0) pair
230 * Return value: the squared distance
233 cpml_pair_squared_distance(const CpmlPair
*from
, const CpmlPair
*to
)
238 from
= &fallback_pair
;
245 return x
* x
+ y
* y
;
249 * cpml_pair_distance:
250 * @from: the first #CpmlPair struct
251 * @to: the second #CpmlPair struct
252 * @distance: where to store the result
254 * Gets the distance between @from and @to, storing the result in @distance.
255 * If you need this value only for comparation purpose, you could use
256 * cpm_pair_squared_distance() instead.
258 * @from or @to could be %NULL, in which case the fallback (0, 0) pair
261 * The algorithm used is adapted from:
262 * "Replacing Square Roots by Pythagorean Sums"
263 * by Clave Moler and Donald Morrison (1983)
264 * IBM Journal of Research and Development 27 (6): 577–581
265 * http://www.research.ibm.com/journal/rd/276/ibmrd2706P.pdf
267 * Return value: the distance
270 cpml_pair_distance(const CpmlPair
*from
, const CpmlPair
*to
)
276 from
= &fallback_pair
;
313 * cpml_vector_from_angle:
314 * @vector: the destination #CpmlVector
315 * @angle: angle of direction, in radians
316 * @length: the length of the vector
318 * Calculates the coordinates of the point far @length from the origin
319 * in the @angle direction. The result is stored in @vector.
321 * Return value: @vector
324 cpml_vector_from_angle(CpmlVector
*vector
, double angle
, double length
)
326 /* Check for common conditions */
327 if (angle
== -M_PI_2
) {
332 if (angle
== M_PI_2
) {
337 if (angle
== M_PI
|| angle
== -M_PI
) {
348 vector
->x
= cos(angle
);
349 vector
->y
= sin(angle
);
355 * cpml_vector_set_length:
356 * @vector: a #CpmlVector
357 * @length: the new length
359 * Imposes the specified @length to @vector. If the old length is 0
360 * (and so the direction is not known), nothing happens.
363 cpml_vector_set_length(CpmlVector
*vector
, double length
)
365 double divisor
= cpml_pair_distance(NULL
, vector
);
367 /* Check for valid length (anything other than 0) */
372 vector
->x
/= divisor
;
373 vector
->y
/= divisor
;
378 * @vector: the source #CpmlVector
380 * Gets the angle of @vector, in radians. If @vector is (0, 0),
383 * Return value: the angle in radians, a value between -M_PI and M_PI
386 cpml_vector_angle(const CpmlVector
*vector
)
388 /* Check for common conditions */
390 return vector
->x
>= 0 ? 0 : M_PI
;
392 return vector
->y
> 0 ? M_PI_2
: -M_PI_2
;
393 if (vector
->x
== vector
->y
)
394 return vector
->x
> 0 ? M_PI_4
: -M_PI_4
* 3;
395 if (vector
->x
== -vector
->y
)
396 return vector
->x
> 0 ? -M_PI_4
: M_PI_4
* 3;
398 return atan2(vector
->y
, vector
->x
);
402 * cpml_vector_normal:
403 * @vector: the subject #CpmlVector
405 * Stores in @vector a vector normal to the original @vector.
406 * The length is retained.
408 * The algorithm is really quick because no trigonometry is involved.
411 cpml_vector_normal(CpmlVector
*vector
)
413 double tmp
= vector
->x
;
415 vector
->x
= -vector
->y
;
420 * cpml_pair_to_cairo:
421 * @pair: the destination #CpmlPair
422 * @path_data: the original path data point
424 * Sets a #cairo_path_data_t struct to @pair. This is exactly the reverse
425 * operation of cpml_pair_from_cairo().
428 cpml_pair_to_cairo(const CpmlPair
*pair
, cairo_path_data_t
*path_data
)
430 path_data
->point
.x
= pair
->x
;
431 path_data
->point
.y
= pair
->y
;