LibGd 2.4.0-dev
GD Graphics library
Loading...
Searching...
No Matches
gd_ft_math.h
1#ifndef GD_FT_MATH_H
2#define GD_FT_MATH_H
3
4/***************************************************************************/
5/* */
6/* fttrigon.h */
7/* */
8/* FreeType trigonometric functions (specification). */
9/* */
10/* Copyright 2001, 2003, 2005, 2007, 2013 by */
11/* David Turner, Robert Wilhelm, and Werner Lemberg. */
12/* */
13/* This file is part of the FreeType project, and may only be used, */
14/* modified, and distributed under the terms of the FreeType project */
15/* license, LICENSE.TXT. By continuing to use, modify, or distribute */
16/* this file you indicate that you have read the license and */
17/* understand and accept it fully. */
18/* */
19/***************************************************************************/
20
21#include "gd_ft_types.h"
22
23/*************************************************************************/
24/* */
25/* The min and max functions missing in C. As usual, be careful not to */
26/* write things like GD_FT_MIN( a++, b++ ) to avoid side effects. */
27/* */
28#define GD_FT_MIN(a, b) ((a) < (b) ? (a) : (b))
29#define GD_FT_MAX(a, b) ((a) > (b) ? (a) : (b))
30
31#define GD_FT_ABS(a) ((a) < 0 ? -(a) : (a))
32
33/*
34 * Approximate sqrt(x*x+y*y) using the `alpha max plus beta min'
35 * algorithm. We use alpha = 1, beta = 3/8, giving us results with a
36 * largest error less than 7% compared to the exact value.
37 */
38#define GD_FT_HYPOT(x, y) \
39 (x = GD_FT_ABS(x), y = GD_FT_ABS(y), x > y ? x + (3 * y >> 3) : y + (3 * x >> 3))
40
41/*************************************************************************/
42/* */
43/* <Function> */
44/* GD_FT_MulFix */
45/* */
46/* <Description> */
47/* A very simple function used to perform the computation */
48/* `(a*b)/0x10000' with maximum accuracy. Most of the time this is */
49/* used to multiply a given value by a 16.16 fixed-point factor. */
50/* */
51/* <Input> */
52/* a :: The first multiplier. */
53/* b :: The second multiplier. Use a 16.16 factor here whenever */
54/* possible (see note below). */
55/* */
56/* <Return> */
57/* The result of `(a*b)/0x10000'. */
58/* */
59/* <Note> */
60/* This function has been optimized for the case where the absolute */
61/* value of `a' is less than 2048, and `b' is a 16.16 scaling factor. */
62/* As this happens mainly when scaling from notional units to */
63/* fractional pixels in FreeType, it resulted in noticeable speed */
64/* improvements between versions 2.x and 1.x. */
65/* */
66/* As a conclusion, always try to place a 16.16 factor as the */
67/* _second_ argument of this function; this can make a great */
68/* difference. */
69/* */
70GD_FT_Long GD_FT_MulFix(GD_FT_Long a, GD_FT_Long b);
71
72/*************************************************************************/
73/* */
74/* <Function> */
75/* GD_FT_MulDiv */
76/* */
77/* <Description> */
78/* A very simple function used to perform the computation `(a*b)/c' */
79/* with maximum accuracy (it uses a 64-bit intermediate integer */
80/* whenever necessary). */
81/* */
82/* This function isn't necessarily as fast as some processor specific */
83/* operations, but is at least completely portable. */
84/* */
85/* <Input> */
86/* a :: The first multiplier. */
87/* b :: The second multiplier. */
88/* c :: The divisor. */
89/* */
90/* <Return> */
91/* The result of `(a*b)/c'. This function never traps when trying to */
92/* divide by zero; it simply returns `MaxInt' or `MinInt' depending */
93/* on the signs of `a' and `b'. */
94/* */
95GD_FT_Long GD_FT_MulDiv(GD_FT_Long a, GD_FT_Long b, GD_FT_Long c);
96
97/*************************************************************************/
98/* */
99/* <Function> */
100/* GD_FT_DivFix */
101/* */
102/* <Description> */
103/* A very simple function used to perform the computation */
104/* `(a*0x10000)/b' with maximum accuracy. Most of the time, this is */
105/* used to divide a given value by a 16.16 fixed-point factor. */
106/* */
107/* <Input> */
108/* a :: The numerator. */
109/* b :: The denominator. Use a 16.16 factor here. */
110/* */
111/* <Return> */
112/* The result of `(a*0x10000)/b'. */
113/* */
114GD_FT_Long GD_FT_DivFix(GD_FT_Long a, GD_FT_Long b);
115
116/*************************************************************************/
117/* */
118/* <Section> */
119/* computations */
120/* */
121/*************************************************************************/
122
123/*************************************************************************
124 *
125 * @type:
126 * GD_FT_Angle
127 *
128 * @description:
129 * This type is used to model angle values in FreeType. Note that the
130 * angle is a 16.16 fixed-point value expressed in degrees.
131 *
132 */
133typedef GD_FT_Fixed GD_FT_Angle;
134
135/*************************************************************************
136 *
137 * @macro:
138 * GD_FT_ANGLE_PI
139 *
140 * @description:
141 * The angle pi expressed in @GD_FT_Angle units.
142 *
143 */
144#define GD_FT_ANGLE_PI (180L << 16)
145
146/*************************************************************************
147 *
148 * @macro:
149 * GD_FT_ANGLE_2PI
150 *
151 * @description:
152 * The angle 2*pi expressed in @GD_FT_Angle units.
153 *
154 */
155#define GD_FT_ANGLE_2PI (GD_FT_ANGLE_PI * 2)
156
157/*************************************************************************
158 *
159 * @macro:
160 * GD_FT_ANGLE_PI2
161 *
162 * @description:
163 * The angle pi/2 expressed in @GD_FT_Angle units.
164 *
165 */
166#define GD_FT_ANGLE_PI2 (GD_FT_ANGLE_PI / 2)
167
168/*************************************************************************
169 *
170 * @macro:
171 * GD_FT_ANGLE_PI4
172 *
173 * @description:
174 * The angle pi/4 expressed in @GD_FT_Angle units.
175 *
176 */
177#define GD_FT_ANGLE_PI4 (GD_FT_ANGLE_PI / 4)
178
179/*************************************************************************
180 *
181 * @function:
182 * GD_FT_Sin
183 *
184 * @description:
185 * Return the sinus of a given angle in fixed-point format.
186 *
187 * @input:
188 * angle ::
189 * The input angle.
190 *
191 * @return:
192 * The sinus value.
193 *
194 * @note:
195 * If you need both the sinus and cosinus for a given angle, use the
196 * function @GD_FT_Vector_Unit.
197 *
198 */
199GD_FT_Fixed GD_FT_Sin(GD_FT_Angle angle);
200
201/*************************************************************************
202 *
203 * @function:
204 * GD_FT_Cos
205 *
206 * @description:
207 * Return the cosinus of a given angle in fixed-point format.
208 *
209 * @input:
210 * angle ::
211 * The input angle.
212 *
213 * @return:
214 * The cosinus value.
215 *
216 * @note:
217 * If you need both the sinus and cosinus for a given angle, use the
218 * function @GD_FT_Vector_Unit.
219 *
220 */
221GD_FT_Fixed GD_FT_Cos(GD_FT_Angle angle);
222
223/*************************************************************************
224 *
225 * @function:
226 * GD_FT_Tan
227 *
228 * @description:
229 * Return the tangent of a given angle in fixed-point format.
230 *
231 * @input:
232 * angle ::
233 * The input angle.
234 *
235 * @return:
236 * The tangent value.
237 *
238 */
239GD_FT_Fixed GD_FT_Tan(GD_FT_Angle angle);
240
241/*************************************************************************
242 *
243 * @function:
244 * GD_FT_Atan2
245 *
246 * @description:
247 * Return the arc-tangent corresponding to a given vector (x,y) in
248 * the 2d plane.
249 *
250 * @input:
251 * x ::
252 * The horizontal vector coordinate.
253 *
254 * y ::
255 * The vertical vector coordinate.
256 *
257 * @return:
258 * The arc-tangent value (i.e. angle).
259 *
260 */
261GD_FT_Angle GD_FT_Atan2(GD_FT_Fixed x, GD_FT_Fixed y);
262
263/*************************************************************************
264 *
265 * @function:
266 * GD_FT_Angle_Diff
267 *
268 * @description:
269 * Return the difference between two angles. The result is always
270 * constrained to the ]-PI..PI] interval.
271 *
272 * @input:
273 * angle1 ::
274 * First angle.
275 *
276 * angle2 ::
277 * Second angle.
278 *
279 * @return:
280 * Constrained value of `value2-value1'.
281 *
282 */
283GD_FT_Angle GD_FT_Angle_Diff(GD_FT_Angle angle1, GD_FT_Angle angle2);
284
285/*************************************************************************
286 *
287 * @function:
288 * GD_FT_Vector_Unit
289 *
290 * @description:
291 * Return the unit vector corresponding to a given angle. After the
292 * call, the value of `vec.x' will be `sin(angle)', and the value of
293 * `vec.y' will be `cos(angle)'.
294 *
295 * This function is useful to retrieve both the sinus and cosinus of a
296 * given angle quickly.
297 *
298 * @output:
299 * vec ::
300 * The address of target vector.
301 *
302 * @input:
303 * angle ::
304 * The input angle.
305 *
306 */
307void GD_FT_Vector_Unit(GD_FT_Vector *vec, GD_FT_Angle angle);
308
309/*************************************************************************
310 *
311 * @function:
312 * GD_FT_Vector_Rotate
313 *
314 * @description:
315 * Rotate a vector by a given angle.
316 *
317 * @inout:
318 * vec ::
319 * The address of target vector.
320 *
321 * @input:
322 * angle ::
323 * The input angle.
324 *
325 */
326void GD_FT_Vector_Rotate(GD_FT_Vector *vec, GD_FT_Angle angle);
327
328/*************************************************************************
329 *
330 * @function:
331 * GD_FT_Vector_Length
332 *
333 * @description:
334 * Return the length of a given vector.
335 *
336 * @input:
337 * vec ::
338 * The address of target vector.
339 *
340 * @return:
341 * The vector length, expressed in the same units that the original
342 * vector coordinates.
343 *
344 */
345GD_FT_Fixed GD_FT_Vector_Length(GD_FT_Vector *vec);
346
347/*************************************************************************
348 *
349 * @function:
350 * GD_FT_Vector_Polarize
351 *
352 * @description:
353 * Compute both the length and angle of a given vector.
354 *
355 * @input:
356 * vec ::
357 * The address of source vector.
358 *
359 * @output:
360 * length ::
361 * The vector length.
362 *
363 * angle ::
364 * The vector angle.
365 *
366 */
367void GD_FT_Vector_Polarize(GD_FT_Vector *vec, GD_FT_Fixed *length, GD_FT_Angle *angle);
368
369/*************************************************************************
370 *
371 * @function:
372 * GD_FT_Vector_From_Polar
373 *
374 * @description:
375 * Compute vector coordinates from a length and angle.
376 *
377 * @output:
378 * vec ::
379 * The address of source vector.
380 *
381 * @input:
382 * length ::
383 * The vector length.
384 *
385 * angle ::
386 * The vector angle.
387 *
388 */
389void GD_FT_Vector_From_Polar(GD_FT_Vector *vec, GD_FT_Fixed length, GD_FT_Angle angle);
390
391#endif // GD_FT_MATH_H