1 /* CPML - Cairo Path Manipulation Library
2 * Copyright (C) 2007,2008,2009,2010,2011,2012,2013 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.
37 * @x: the x component of the pair
38 * @y: the y component of the pair
40 * A generic 2D structure.
47 * @x: the x component of the vector
48 * @y: the y component of the vector
50 * Another name for #CpmlPair. It is used to clarify when a function
51 * expects a generic pair (usually coordinates) or a vector.
53 * A vector represents a line starting from the origin (0,0) and ending
54 * to the given coordinates pair. Vectors are useful to define direction
55 * and length at once. Keep in mind the cairo default coordinates system
56 * is not problably what you expect: the x axis increases at right
57 * (as usual) but the y axis increases at down (the reverse of a usual
58 * cartesian plan). An angle of 0 is at V=(1; 0) (middle right).
64 #include "cpml-internal.h"
69 static CpmlPair fallback_pair
= { 0, 0 };
73 * cpml_pair_from_cairo:
74 * @pair: (out): the destination #CpmlPair
75 * @path_data: (in) (type gpointer): the original path data point
77 * Sets @pair from a #cairo_path_data_t struct. @path_data should contains
78 * a point data: it is up to the caller to be sure @path_data is valid.
83 cpml_pair_from_cairo(CpmlPair
*pair
, const cairo_path_data_t
*path_data
)
85 pair
->x
= path_data
->point
.x
;
86 pair
->y
= path_data
->point
.y
;
91 * @pair: (out): the destination #CpmlPair
92 * @src: (in): the source #CpmlPair
94 * Copies @src in @pair. If @src or @pair is %NULL, this function does
100 cpml_pair_copy(CpmlPair
*pair
, const CpmlPair
*src
)
102 if (pair
== NULL
|| src
== NULL
)
104 memcpy(pair
, src
, sizeof(CpmlPair
));
109 * @pair: the first pair to compare
110 * @src: the second pair to compare
112 * Compares @pair to @src and returns 1 if the pairs are equals.
113 * Two %NULL pairs are considered equal.
115 * Returns: (type gboolean): %1 if @pair is equal to @src,
121 cpml_pair_equal(const CpmlPair
*pair
, const CpmlPair
*src
)
123 if (pair
== NULL
&& src
== NULL
)
126 if (pair
== NULL
|| src
== NULL
)
129 return pair
->x
== src
->x
&& pair
->y
== src
->y
? 1 : 0;
133 * cpml_pair_transform:
134 * @pair: (inout): the destination #CpmlPair struct
135 * @matrix: (in): the transformation matrix
137 * Shortcut to apply a specific transformation matrix to @pair.
142 cpml_pair_transform(CpmlPair
*pair
, const cairo_matrix_t
*matrix
)
144 cairo_matrix_transform_point(matrix
, &pair
->x
, &pair
->y
);
148 * cpml_pair_squared_distance:
149 * @from: the first #CpmlPair struct
150 * @to: the second #CpmlPair struct
152 * Gets the squared distance between @from and @to. This value is useful
153 * for comparation purpose: if you need to get the real distance, use
154 * cpml_pair_distance().
156 * @from or @to could be %NULL, in which case the fallback (0, 0) pair
159 * Returns: the squared distance
164 cpml_pair_squared_distance(const CpmlPair
*from
, const CpmlPair
*to
)
169 from
= &fallback_pair
;
176 return x
* x
+ y
* y
;
180 * cpml_pair_distance:
181 * @from: the first #CpmlPair struct
182 * @to: the second #CpmlPair struct
184 * Gets the distance between @from and @to. If you need this value only
185 * for comparation purpose, you could use cpm_pair_squared_distance()
188 * @from or @to could be %NULL, in which case the fallback (0, 0) pair
191 * The algorithm used is adapted from:
192 * "Replacing Square Roots by Pythagorean Sums"
193 * by Clave Moler and Donald Morrison (1983)
194 * IBM Journal of Research and Development 27 (6): 577–581
195 * http://www.research.ibm.com/journal/rd/276/ibmrd2706P.pdf
197 * Returns: the distance
202 cpml_pair_distance(const CpmlPair
*from
, const CpmlPair
*to
)
208 from
= &fallback_pair
;
245 * cpml_pair_to_cairo:
246 * @pair: (in): the source #CpmlPair
247 * @path_data: (out) (type gpointer): the path data point to modify
249 * Sets a #cairo_path_data_t struct to @pair. This is exactly the reverse
250 * operation of cpml_pair_from_cairo().
255 cpml_pair_to_cairo(const CpmlPair
*pair
, cairo_path_data_t
*path_data
)
257 path_data
->point
.x
= pair
->x
;
258 path_data
->point
.y
= pair
->y
;
263 * cpml_vector_from_angle:
264 * @vector: (out): the destination #CpmlVector
265 * @angle: (in): angle of direction, in radians
267 * Calculates the coordinates of the point far %1 from the origin
268 * in the @angle direction. The result is stored in @vector.
273 cpml_vector_from_angle(CpmlVector
*vector
, double angle
)
275 /* Check for common conditions */
276 if (angle
== -M_PI_2
) {
279 } else if (angle
== M_PI_2
) {
282 } else if (angle
== M_PI
|| angle
== -M_PI
) {
285 } else if (angle
== 0) {
289 vector
->x
= cos(angle
);
290 vector
->y
= sin(angle
);
295 * cpml_vector_set_length:
296 * @vector: (inout): a #CpmlVector
297 * @length: (in): the new length
299 * Imposes the specified @length to @vector. If the old length is %0
300 * (and so the direction is not known), nothing happens. If @length
301 * is %0, @vector is set to (0, 0).
303 * The @length parameter can be negative, in which case the vector
309 cpml_vector_set_length(CpmlVector
*vector
, double length
)
319 divisor
= cpml_pair_distance(NULL
, vector
);
321 /* Check for valid length (anything other than 0) */
324 vector
->x
/= divisor
;
325 vector
->y
/= divisor
;
331 * @vector: the source #CpmlVector
333 * Gets the angle of @vector, in radians. If @vector is (0, 0),
336 * Returns: the angle in radians, a value between -M_PI and M_PI
341 cpml_vector_angle(const CpmlVector
*vector
)
343 /* Check for common conditions */
345 return vector
->x
>= 0 ? 0 : M_PI
;
347 return vector
->y
> 0 ? M_PI_2
: -M_PI_2
;
348 if (vector
->x
== vector
->y
)
349 return vector
->x
> 0 ? M_PI_4
: -M_PI_4
* 3;
350 if (vector
->x
== -vector
->y
)
351 return vector
->x
> 0 ? -M_PI_4
: M_PI_4
* 3;
353 return atan2(vector
->y
, vector
->x
);
357 * cpml_vector_normal:
358 * @vector: (inout): the subject #CpmlVector
360 * Stores in @vector a vector normal to the original @vector.
361 * The length is retained.
363 * The algorithm is really quick because no trigonometry is involved.
368 cpml_vector_normal(CpmlVector
*vector
)
370 double tmp
= vector
->x
;
372 vector
->x
= -vector
->y
;
377 * cpml_vector_transform:
378 * @vector: (inout): the destination #CpmlPair struct
379 * @matrix: (in): the transformation matrix
381 * Shortcut to apply a specific transformation matrix to @vector.
382 * It works in a similar way of cpml_pair_transform() but uses
383 * cairo_matrix_transform_distance() instead of
384 * cairo_matrix_transform_point().
389 cpml_vector_transform(CpmlPair
*vector
, const cairo_matrix_t
*matrix
)
391 cairo_matrix_transform_distance(matrix
, &vector
->x
, &vector
->y
);