[cpml] Implemented cpml_path_dump()
[adg.git] / cpml / cpml-pair.c
blob457078c6e74869ea31979d219b582c1f0d773468
1 /* CPML - Cairo Path Manipulation Library
2 * Copyright (C) 2008, 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.
21 /**
22 * SECTION:cpmlpair
23 * @title: CpmlPair
24 * @short_description: A structure holding a couple of values
26 * The CpmlPair is a generic 2D structure. It can be used to represent points,
27 * sizes, offsets or whatever have two components.
29 * The name comes from MetaFont.
32 /**
33 * CpmlPair:
34 * @x: the x component of the pair
35 * @y: the y component of the pair
37 * A generic 2D structure.
40 /**
41 * CpmlVector:
42 * @x: the x component of the vector
43 * @y: the y component of the vector
45 * A subclass of a #CpmlPair. A vector represents the coordinates of a point
46 * distant 1 from the origin (0, 0). The vectors are useful to define a
47 * direction and are better suited than angles for simplifying interpolations.
50 #include "cpml-pair.h"
52 #include <stdlib.h>
55 static CpmlPair fallback_pair = { 0., 0. };
58 /**
59 * cpml_pair_copy:
60 * @pair: the destination #CpmlPair struct
61 * @src: the source #CpmlPair struct
63 * Assign @src to @pair.
65 * Return value: 1 if @pair was set, 0 on errors
67 cairo_bool_t
68 cpml_pair_copy(CpmlPair *pair, const CpmlPair *src)
70 pair->x = src->x;
71 pair->y = src->y;
72 return 1;
75 /**
76 * cpml_pair_transform:
77 * @pair: the destination #CpmlPair struct
78 * @matrix: the transformation matrix
80 * Shortcut to apply a specific transformation matrix to @pair.
82 * Return value: 1 if @pair was transformed, 0 on errors
83 **/
84 cairo_bool_t
85 cpml_pair_transform(CpmlPair *pair, const cairo_matrix_t *matrix)
87 cairo_matrix_transform_point(matrix, &pair->x, &pair->y);
88 return 1;
91 /**
92 * cpml_pair_distance:
93 * @from: the first #CpmlPair struct
94 * @to: the second #CpmlPair struct
95 * @distance: where to store the result
97 * Gets the distance between @from and @to, storing the result in @distance.
98 * If you need this value only for comparation purpose, you could use
99 * cpm_pair_squared_distance() instead that is a lot faster, since no square
100 * root operation is involved.
102 * @from or @to could be %NULL, in which case the fallback (0, 0) pair
103 * will be used.
105 * Return value: 1 if @distance was properly set, 0 on errors
107 cairo_bool_t
108 cpml_pair_distance(double *distance, const CpmlPair *from, const CpmlPair *to)
110 if (!cpml_pair_square_distance(distance, from, to))
111 return 0;
113 *distance = sqrt(*distance);
114 return 1;
118 * cpml_pair_square_distance:
119 * @from: the first #CpmlPair struct
120 * @to: the second #CpmlPair struct
121 * @distance: where to store the result
123 * Gets the square distance between @from and @to, storing the result in
124 * @distance. This value is useful for comparation purpose: if you need to
125 * get the real distance, use cpml_pair_distance().
127 * @from or @to could be %NULL, in which case the fallback (0, 0) pair
128 * will be used.
130 * Return value: 1 if @distance was properly set, 0 on errors
132 cairo_bool_t
133 cpml_pair_square_distance(double *distance,
134 const CpmlPair *from, const CpmlPair *to)
136 double x, y;
138 if (from == NULL)
139 from = &fallback_pair;
140 if (to == NULL)
141 to = &fallback_pair;
143 x = to->x - from->x;
144 y = to->y - from->y;
145 *distance = x * x + y * y;
146 return 1;
150 * cpml_pair_angle:
151 * @from: the first #CpmlPair struct
152 * @to: the second #CpmlPair struct
153 * @angle: where to store the result
155 * Gets the angle between @from and @to, storing the result in @angle.
157 * @from or @to could be %NULL, in which case the fallback (0, 0) pair
158 * will be used.
160 * Return value: 1 if @angle was properly set, 0 on errors
162 cairo_bool_t
163 cpml_pair_angle(double *angle, const CpmlPair *from, const CpmlPair *to)
165 static CpmlPair cached_pair = { 1., 0. };
166 static double cached_angle = 0.;
167 CpmlPair pair;
169 if (from == NULL)
170 from = &fallback_pair;
171 if (to == NULL)
172 to = &fallback_pair;
174 pair.x = to->x - from->x;
175 pair.y = to->y - from->y;
177 /* Check for cached result */
178 if (pair.x == cached_pair.x && pair.y == cached_pair.y) {
179 *angle = cached_angle;
180 } else if (pair.y == 0.) {
181 *angle = pair.x >= 0. ? CPML_DIR_RIGHT : CPML_DIR_LEFT;
182 } else if (pair.x == 0.) {
183 *angle = pair.y > 0. ? CPML_DIR_UP : CPML_DIR_DOWN;
184 } else if (pair.x == pair.y) {
185 *angle = pair.x > 0. ? M_PI / 4. : 5. * M_PI / 4.;
186 } else if (pair.x == -pair.y) {
187 *angle = pair.x > 0. ? 7. * M_PI / 4. : 3. * M_PI / 4.;
188 } else {
189 *angle = atan(pair.y / pair.x);
191 if (pair.x < 0.0)
192 *angle += M_PI;
193 else if (pair.y < 0.0)
194 *angle += 2.0 * M_PI;
196 /* Cache registration */
197 cached_angle = *angle;
198 cpml_pair_copy(&cached_pair, &pair);
201 return 1;
205 * cpml_vector_from_pair:
206 * @vector: an allocated #CpmlPair struct
207 * @pair: the source pair
209 * Unitizes @pair, that is given the line L passing throught the origin and
210 * @pair, gets the coordinate of the point on this line far 1.0 from
211 * the origin, and store the result in @vector.
213 * @pair and @vector can be the same struct.
215 * Return value: 1 if @vector was properly set, 0 on errors
217 cairo_bool_t
218 cpml_vector_from_pair(CpmlPair *vector, const CpmlPair *pair)
220 double length;
222 if (!cpml_pair_distance(&length, pair, NULL) || length == 0.)
223 return 0;
225 vector->x = pair->x / length;
226 vector->y = pair->y / length;
227 return 1;
231 * cpml_vector_from_angle:
232 * @vector: an allocated #CpmlPair struct
233 * @angle: angle of direction, in radians
235 * Calculates the coordinates of the point far 1.0 from the origin in the
236 * @angle direction. The result is stored in @vector.
238 * Return value: 1 if @vector was properly set, 0 on errors
240 cairo_bool_t
241 cpml_vector_from_angle(CpmlPair *vector, double angle)
243 static double cached_angle = 0.;
244 static CpmlPair cached_vector = { 1., 0. };
246 /* Check for cached result */
247 if (angle == cached_angle) {
248 vector->x = cached_vector.x;
249 vector->y = cached_vector.y;
250 } else if (angle == CPML_DIR_RIGHT) {
251 vector->x = +1.;
252 vector->y = 0.;
253 } else if (angle == CPML_DIR_UP) {
254 vector->x = 0.;
255 vector->y = -1.;
256 } else if (angle == CPML_DIR_LEFT) {
257 vector->x = -1.;
258 vector->y = 0.;
259 } else if (angle == CPML_DIR_DOWN) {
260 vector->x = 0.;
261 vector->y = +1.;
262 } else {
263 vector->x = cos(angle);
264 vector->y = sin(angle);
266 /* Cache registration */
267 cached_angle = angle;
268 cpml_pair_copy(&cached_vector, vector);
271 return 1;