Rename GP_Context -> GP_Pixmap
[gfxprim.git] / libs / gfx / algo / Arc.algo.h
blobeb2141e4745dc969af79e0ef682135d5cf1e2279
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim 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.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim 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. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
21 * *
22 * Copyright (C) 2009-2012 Cyril Hrubis <metan@ucw.cz> *
23 * *
24 *****************************************************************************/
27 * The elliptical arc algorithm; a generalization of an ellipse that allows
28 * to draw only part of it. The math is exactly the same, only it has
29 * additional tests of what part to draw.
33 * This macro defines a function that draws a segment of an arc within
34 * two horizontal quadrants. For a larger arc, two calls are needed.
36 * The 'direction' parameter specifies which two quadrants to work in:
37 * if <0, the top two quadrants (y < 0) are used, if >0, the bottom two
38 * (y > 0) are used.
40 * Arguments:
41 * PIXMAP_T - user-defined type of drawing pixmap (passed to PUTPIXEL)
42 * PIXVAL_T - user-defined pixel value type (passed to PUTPIXEL)
43 * PUTPIXEL - a pixel drawing function f(pixmap, x, y, pixval)
44 * FN_NAME - name of the function to be defined
46 #define DEF_ARCSEGMENT_FN(FN_NAME, PIXMAP_T, PIXVAL_T, PUTPIXEL) \
47 static void FN_NAME(PIXMAP_T pixmap, int xcenter, int ycenter, \
48 unsigned int a, unsigned int b, int direction, \
49 double start, double end, PIXVAL_T pixval) \
50 { \
51 /* Precompute quadratic terms. */ \
52 int a2 = a*a; \
53 int b2 = b*b; \
55 /* Compute minimum and maximum value of X from the angles. */ \
56 int x1 = (int)(cos(start)*a); \
57 int x2 = (int)(cos(end)*a); \
58 int xmin = GP_MIN(x1, x2); \
59 int xmax = GP_MAX(x1, x2); \
61 int x, y, error; \
62 for (x = 0, error = -b2*a, y = b; y >= 0; y--) { \
63 while (error < 0) { \
65 /* Calculate error(x+1) from error(x). */ \
66 error += 2*x*b2 + b2; \
67 x++; \
69 if (direction < 0) { \
70 if ((-x+1) >= xmin && (-x+1) <= xmax) { \
71 PUTPIXEL(pixmap, xcenter-x+1, ycenter-y, pixval); \
72 } \
73 if ((x-1) >= xmin && (x-1) <= xmax) { \
74 PUTPIXEL(pixmap, xcenter+x-1, ycenter-y, pixval); \
75 } \
76 } \
77 if (direction > 0) { \
78 if ((-x+1) >= xmin && (-x+1) <= xmax) { \
79 PUTPIXEL(pixmap, xcenter-x+1, ycenter+y, pixval); \
80 } \
81 if ((x-1) >= xmin && (x-1) <= xmax) { \
82 PUTPIXEL(pixmap, xcenter+x-1, ycenter+y, pixval); \
83 } \
84 } \
85 } \
87 /* Calculate error(y-1) from error(y). */ \
88 error += -2*y*a2 + a2; \
90 if (direction < 0) { \
91 if ((-x+1) >= xmin && (-x+1) <= xmax) { \
92 PUTPIXEL(pixmap, xcenter-x+1, ycenter-y, pixval); \
93 } \
94 if ((x-1) >= xmin && (x-1) <= xmax) { \
95 PUTPIXEL(pixmap, xcenter+x-1, ycenter-y, pixval); \
96 } \
97 } \
98 if (direction > 0) { \
99 if ((-x+1) >= xmin && (-x+1) <= xmax) { \
100 PUTPIXEL(pixmap, xcenter-x+1, ycenter+y, pixval); \
102 if ((x-1) >= xmin && (x-1) <= xmax) { \
103 PUTPIXEL(pixmap, xcenter+x-1, ycenter+y, pixval); \