~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

# Linux Cross ReferenceTina4/src/math/draw/symm_line.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

```  1 /* @(#)Symmetric Double Step Line Algorithm by Brian Wyvill See
2  * "Graphics Gems", Academic Press, 1990. */
3
4 #include <tina/math.h>
5 #define swap(a,b)           {a^=b; b^=a; a^=b;}
6 #define absolute(i,j,k)     ( (i-j)*(k = ( (i-j)<0 ? -1 : 1)))
7
8 static void apply_pix(int x, int y, int flag, void (*func) (), void *data)
9 {
10     if (flag)
11         func(y, x, data);
12     else
13         func(x, y, data);
14 }
15
16 void    apply_symmetric_line(Vec2 p1, Vec2 p2, void (*func) (), void *data)
17 {
18     int     x1, y1;
19     int     dx, dy, incr1, incr2, D, x, y, xend, c, pixels_left;
20     int     sign_x, sign_y, step, reverse, j;
21     int     a1 = (int) vec2_x(p1);
22     int     b1 = (int) vec2_y(p1);
23     int     a2 = (int) vec2_x(p2);
24     int     b2 = (int) vec2_y(p2);
25
26     dx = absolute(a2, a1, sign_x);
27     dy = absolute(b2, b1, sign_y);
28
29     if (sign_x == sign_y)
30         step = 1;
31     else
32         step = -1;
33
34     if (dy > dx)
35     {
36         swap(a1, b1);
37         swap(a2, b2);
38         swap(dx, dy);
39         reverse = 1;
40     } else
41         reverse = 0;
42
43     /* note: check for dx==0 should be included here */
44     if (a1 > a2)
45     {
46         x = a2;
47         y = b2;
48         x1 = a1;
49         y1 = b1;
50     } else
51     {
52         x = a1;
53         y = b1;
54         x1 = a2;
55         y1 = b2;
56     }
57
58     xend = (dx - 1) / 4;
59     pixels_left = (dx - 1) % 4;
60     apply_pix(x, y, reverse, func, data);
61
62     if (pixels_left < 0)
63         return;
64
65     apply_pix(x1, y1, reverse, func, data);
66     incr2 = 4 * dy - 2 * dx;
67
68     if (incr2 < 0)              /* slope less than 1/2 */
69     {
70         c = 2 * dy;
71         incr1 = 2 * c;
72         D = incr1 - dx;
73
74         for (j = 0; j < xend; j++)
75         {
76             ++x;
77             --x1;
78             if (D < 0)
79             {
80                 apply_pix(x, y, reverse, func, data);
81                 apply_pix(++x, y, reverse, func, data);
82                 apply_pix(x1, y1, reverse, func, data);
83                 apply_pix(--x1, y1, reverse, func, data);
84                 D += incr1;
85             } else
86             {
87                 if (D < c)
88                 {
89                     apply_pix(x, y, reverse, func, data);
90                     y += step;
91                     apply_pix(++x, y, reverse, func, data);
92                     apply_pix(x1, y1, reverse, func, data);
93                     y1 -= step;
94                     apply_pix(--x1, y1, reverse, func, data);
95                 } else
96                 {
97                     y += step;
98                     apply_pix(x, y, reverse, func, data);
99                     apply_pix(++x, y, reverse, func, data);
100                     y1 -= step;
101                     apply_pix(x1, y1, reverse, func, data);
102                     apply_pix(--x1, y1, reverse, func, data);
103                 }
104                 D += incr2;
105             }
106         }
107
108         if (pixels_left)
109         {
110             if (D < 0)
111             {
112                 apply_pix(++x, y, reverse, func, data);
113                 if (pixels_left > 1)
114                     apply_pix(++x, y, reverse, func, data);
115                 if (pixels_left > 2)
116                 {
117                     --x1;
118                     apply_pix(x1, y1, reverse, func, data);
119                 }
120             } else
121             {
122                 if (D < c)
123                 {
124                     apply_pix(++x, y, reverse, func, data);
125                     if (pixels_left > 1)
126                     {
127                         y += step;
128                         apply_pix(++x, y, reverse, func, data);
129                     }
130                     if (pixels_left > 2)
131                         apply_pix(--x1, y1, reverse, func, data);
132                 } else
133                 {
134                     y += step;
135                     apply_pix(++x, y, reverse, func, data);
136                     if (pixels_left > 1)
137                         apply_pix(++x, y, reverse, func, data);
138                     if (pixels_left > 2)
139                     {
140                         y1 -= step;
141                         apply_pix(--x1, y1, reverse, func, data);
142                     }
143                 }
144             }
145         }
146     } else
147     {
148         c = 2 * (dy - dx);
149         incr1 = 2 * c;
150         D = incr1 + dx;
151         for (j = 0; j < xend; j++)
152         {
153             ++x;
154             --x1;
155             if (D > 0)
156             {
157                 apply_pix(x, y += step, reverse, func, data);
158                 apply_pix(++x, y += step, reverse, func, data);
159                 apply_pix(x1, y1 -= step, reverse, func, data);
160                 apply_pix(--x1, y1 -= step, reverse, func, data);
161
162                 D += incr1;
163             } else
164             {
165                 if (D < c)
166                 {
167                     apply_pix(x, y, reverse, func, data);
168                     apply_pix(++x, y += step, reverse, func, data);
169                     apply_pix(x1, y1, reverse, func, data);
170                     apply_pix(--x1, y1 -= step, reverse, func, data);
171                 } else
172                 {
173                     y += step;
174                     apply_pix(x, y, reverse, func, data);
175                     apply_pix(++x, y, reverse, func, data);
176                     y1 -= step;
177                     apply_pix(x1, y1, reverse, func, data);
178                     apply_pix(--x1, y1, reverse, func, data);
179                 }
180                 D += incr2;
181             }
182         }
183
184         if (pixels_left)
185         {
186             if (D > 0)
187             {
188                 apply_pix(++x, y += step, reverse, func, data);
189                 if (pixels_left > 1)
190                 {
191                     y += step;
192                     apply_pix(++x, y, reverse, func, data);
193                 }
194                 if (pixels_left > 2)
195                 {
196                     y1 -= step;
197                     apply_pix(--x1, y1, reverse, func, data);
198                 }
199             } else
200             {
201                 if (D < c)
202                 {
203                     apply_pix(++x, y, reverse, func, data);
204                     if (pixels_left > 1)
205                         apply_pix(++x, y += step, reverse, func, data);
206                     if (pixels_left > 2)
207                         apply_pix(--x1, y1, reverse, func, data);
208                 } else
209                 {
210                     apply_pix(++x, y += step, reverse, func, data);
211                     if (pixels_left > 1)
212                         apply_pix(++x, y, reverse, func, data);
213                     if (pixels_left > 2)
214                     {
215                         if (D > c)
216                             apply_pix(--x1, y1 -= step, reverse, func, data);
217                         else
218                             apply_pix(--x1, y1, reverse, func, data);
219                     }
220                 }
221             }
222         }
223     }
224 }
225 ```

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.