[AdgPositionable] Implemented adg_positionable_set_org_explicit()
[adg.git] / cpml / cpml-pair.c
blobb062817077fa67a5878234598505cbe8f1e8af23
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_distance:
77 * @from: the first #CpmlPair struct
78 * @to: the second #CpmlPair struct
79 * @distance: where to store the result
81 * Gets the distance between @from and @to, storing the result in @distance.
82 * If you need this value only for comparation purpose, you could use
83 * cpm_pair_squared_distance() instead that is a lot faster, since no square
84 * root operation is involved.
86 * @from or @to could be %NULL, in which case the fallback (0, 0) pair
87 * will be used.
89 * Return value: 1 if @distance was properly set, 0 on errors
91 cairo_bool_t
92 cpml_pair_distance(double *distance, const CpmlPair *from, const CpmlPair *to)
94 if (!cpml_pair_square_distance(distance, from, to))
95 return 0;
97 *distance = sqrt(*distance);
98 return 1;
102 * cpml_pair_square_distance:
103 * @from: the first #CpmlPair struct
104 * @to: the second #CpmlPair struct
105 * @distance: where to store the result
107 * Gets the square distance between @from and @to, storing the result in
108 * @distance. This value is useful for comparation purpose: if you need to
109 * get the real distance, use cpml_pair_distance().
111 * @from or @to could be %NULL, in which case the fallback (0, 0) pair
112 * will be used.
114 * Return value: 1 if @distance was properly set, 0 on errors
116 cairo_bool_t
117 cpml_pair_square_distance(double *distance,
118 const CpmlPair *from, const CpmlPair *to)
120 double x, y;
122 if (from == NULL)
123 from = &fallback_pair;
124 if (to == NULL)
125 to = &fallback_pair;
127 x = to->x - from->x;
128 y = to->y - from->y;
129 *distance = x * x + y * y;
130 return 1;
134 * cpml_pair_angle:
135 * @from: the first #CpmlPair struct
136 * @to: the second #CpmlPair struct
137 * @angle: where to store the result
139 * Gets the angle between @from and @to, storing the result in @angle.
141 * @from or @to could be %NULL, in which case the fallback (0, 0) pair
142 * will be used.
144 * Return value: 1 if @angle was properly set, 0 on errors
146 cairo_bool_t
147 cpml_pair_angle(double *angle, const CpmlPair *from, const CpmlPair *to)
149 static CpmlPair cached_pair = { 1., 0. };
150 static double cached_angle = 0.;
151 CpmlPair pair;
153 if (from == NULL)
154 from = &fallback_pair;
155 if (to == NULL)
156 to = &fallback_pair;
158 pair.x = to->x - from->x;
159 pair.y = to->y - from->y;
161 /* Check for cached result */
162 if (pair.x == cached_pair.x && pair.y == cached_pair.y) {
163 *angle = cached_angle;
164 } else if (pair.y == 0.) {
165 *angle = pair.x >= 0. ? CPML_DIR_RIGHT : CPML_DIR_LEFT;
166 } else if (pair.x == 0.) {
167 *angle = pair.y > 0. ? CPML_DIR_UP : CPML_DIR_DOWN;
168 } else if (pair.x == pair.y) {
169 *angle = pair.x > 0. ? M_PI / 4. : 5. * M_PI / 4.;
170 } else if (pair.x == -pair.y) {
171 *angle = pair.x > 0. ? 7. * M_PI / 4. : 3. * M_PI / 4.;
172 } else {
173 *angle = atan(pair.y / pair.x);
175 if (pair.x < 0.0)
176 *angle += M_PI;
177 else if (pair.y < 0.0)
178 *angle += 2.0 * M_PI;
180 /* Cache registration */
181 cached_angle = *angle;
182 cpml_pair_copy(&cached_pair, &pair);
185 return 1;
189 * cpml_vector_from_pair:
190 * @vector: an allocated #CpmlPair struct
191 * @pair: the source pair
193 * Unitizes @pair, that is given the line L passing throught the origin and
194 * @pair, gets the coordinate of the point on this line far 1.0 from
195 * the origin, and store the result in @vector.
197 * @pair and @vector can be the same struct.
199 * Return value: 1 if @vector was properly set, 0 on errors
201 cairo_bool_t
202 cpml_vector_from_pair(CpmlPair *vector, const CpmlPair *pair)
204 double length;
206 if (!cpml_pair_distance(&length, pair, NULL) || length == 0.)
207 return 0;
209 vector->x = pair->x / length;
210 vector->y = pair->y / length;
211 return 1;
215 * cpml_vector_from_angle:
216 * @vector: an allocated #CpmlPair struct
217 * @angle: angle of direction, in radians
219 * Calculates the coordinates of the point far 1.0 from the origin in the
220 * @angle direction. The result is stored in @vector.
222 * Return value: 1 if @vector was properly set, 0 on errors
224 cairo_bool_t
225 cpml_vector_from_angle(CpmlPair *vector, double angle)
227 static double cached_angle = 0.;
228 static CpmlPair cached_vector = { 1., 0. };
230 /* Check for cached result */
231 if (angle == cached_angle) {
232 vector->x = cached_vector.x;
233 vector->y = cached_vector.y;
234 } else if (angle == CPML_DIR_RIGHT) {
235 vector->x = +1.;
236 vector->y = 0.;
237 } else if (angle == CPML_DIR_UP) {
238 vector->x = 0.;
239 vector->y = -1.;
240 } else if (angle == CPML_DIR_LEFT) {
241 vector->x = -1.;
242 vector->y = 0.;
243 } else if (angle == CPML_DIR_DOWN) {
244 vector->x = 0.;
245 vector->y = +1.;
246 } else {
247 vector->x = cos(angle);
248 vector->y = sin(angle);
250 /* Cache registration */
251 cached_angle = angle;
252 cpml_pair_copy(&cached_vector, vector);
255 return 1;