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.
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.
34 * @x: the x component of the pair
35 * @y: the y component of the pair
37 * A generic 2D structure.
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"
55 static CpmlPair fallback_pair
= { 0., 0. };
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
68 cpml_pair_copy(CpmlPair
*pair
, const CpmlPair
*src
)
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
89 * Return value: 1 if @distance was properly set, 0 on errors
92 cpml_pair_distance(double *distance
, const CpmlPair
*from
, const CpmlPair
*to
)
94 if (!cpml_pair_square_distance(distance
, from
, to
))
97 *distance
= sqrt(*distance
);
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
114 * Return value: 1 if @distance was properly set, 0 on errors
117 cpml_pair_square_distance(double *distance
,
118 const CpmlPair
*from
, const CpmlPair
*to
)
123 from
= &fallback_pair
;
129 *distance
= x
* x
+ y
* y
;
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
144 * Return value: 1 if @angle was properly set, 0 on errors
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.;
154 from
= &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.;
173 *angle
= atan(pair
.y
/ pair
.x
);
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
);
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
202 cpml_vector_from_pair(CpmlPair
*vector
, const CpmlPair
*pair
)
206 if (!cpml_pair_distance(&length
, pair
, NULL
) || length
== 0.)
209 vector
->x
= pair
->x
/ length
;
210 vector
->y
= pair
->y
/ length
;
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
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
) {
237 } else if (angle
== CPML_DIR_UP
) {
240 } else if (angle
== CPML_DIR_LEFT
) {
243 } else if (angle
== CPML_DIR_DOWN
) {
247 vector
->x
= cos(angle
);
248 vector
->y
= sin(angle
);
250 /* Cache registration */
251 cached_angle
= angle
;
252 cpml_pair_copy(&cached_vector
, vector
);