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

Linux Cross Reference
Tina5/tina-libs/tina/math/mathDraw_symmline.c

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

  1 /**********
  2  *
  3  * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
  4  * University of Manchester, UK.  All rights reserved.
  5  * 
  6  * Redistribution and use in source and binary forms, with or without modification, 
  7  * are permitted provided that the following conditions are met:
  8  * 
  9  *   . Redistributions of source code must retain the above copyright notice, 
 10  *     this list of conditions and the following disclaimer.
 11  *    
 12  *   . Redistributions in binary form must reproduce the above copyright notice,
 13  *     this list of conditions and the following disclaimer in the documentation 
 14  *     and/or other materials provided with the distribution.
 15  * 
 16  *   . Neither the name of the University of Manchester nor the names of its
 17  *     contributors may be used to endorse or promote products derived from this 
 18  *     software without specific prior written permission.
 19  * 
 20  * 
 21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  * POSSIBILITY OF SUCH DAMAGE.
 32  *
 33  **********
 34  *
 35  * Program :    TINA
 36  * File    :  $Source: /home/tina/cvs/tina-libs/tina/math/mathDraw_symmline.c,v $
 37  * Date    :  $Date: 2003/09/23 11:32:20 $
 38  * Version :  $Revision: 1.4 $
 39  * CVS Id  :  $Id: mathDraw_symmline.c,v 1.4 2003/09/23 11:32:20 matts Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes :@(#)Symmetric Double Step Line Algorithm by Brian Wyvill See
 44  * "Graphics Gems", Academic Press, 1990.
 45  *
 46  *********
 47 */
 48 /** 
 49  *  @file
 50  *  @brief  Symmetric Double Step Line Algorithm.       
 51  *
 52  *  Written by Brian Wyvill See "Graphics Gems", Academic Press, 1990.
 53  * 
 54 */
 55 
 56 
 57 #include "mathDraw_symmline.h"
 58 
 59 #if HAVE_CONFIG_H
 60 #include <config.h>
 61 #endif
 62 
 63 #include <tina/math/math_VecDef.h>
 64 
 65 #define swap(a,b)           {a^=b; b^=a; a^=b;}
 66 #define absolute(i,j,k)     ( (i-j)*(k = ( (i-j)<0 ? -1 : 1)))
 67 
 68 
 69 static void     apply_pix(int x, int y, int flag, void (*func) (), void *data)
 70 {
 71         if (flag)
 72                 func(y, x, data);
 73         else
 74                 func(x, y, data);
 75 }
 76 
 77 void            apply_symmetric_line(Vec2 p1, Vec2 p2, void (*func) (), void *data)
 78 {
 79         int             x1, y1;
 80         int             dx, dy, incr1, incr2, D, x, y, xend, c, pixels_left;
 81         int             sign_x, sign_y, step, reverse, j;
 82         int             a1 = (int) vec2_x(p1);
 83         int             b1 = (int) vec2_y(p1);
 84         int             a2 = (int) vec2_x(p2);
 85         int             b2 = (int) vec2_y(p2);
 86 
 87         dx = absolute(a2, a1, sign_x);
 88         dy = absolute(b2, b1, sign_y);
 89 
 90         if (sign_x == sign_y)
 91                 step = 1;
 92         else
 93                 step = -1;
 94 
 95         if (dy > dx)
 96         {
 97                 swap(a1, b1);
 98                 swap(a2, b2);
 99                 swap(dx, dy);
100                 reverse = 1;
101         } else
102                 reverse = 0;
103 
104         /* note: check for dx==0 should be included here */
105         if (a1 > a2)
106         {
107                 x = a2;
108                 y = b2;
109                 x1 = a1;
110                 y1 = b1;
111         } else
112         {
113                 x = a1;
114                 y = b1;
115                 x1 = a2;
116                 y1 = b2;
117         }
118 
119         xend = (dx - 1) / 4;
120         pixels_left = (dx - 1) % 4;
121         apply_pix(x, y, reverse, func, data);
122 
123         if (pixels_left < 0)
124                 return;
125 
126         apply_pix(x1, y1, reverse, func, data);
127         incr2 = 4 * dy - 2 * dx;
128 
129         if (incr2 < 0)          /* slope less than 1/2 */
130         {
131                 c = 2 * dy;
132                 incr1 = 2 * c;
133                 D = incr1 - dx;
134 
135                 for (j = 0; j < xend; j++)
136                 {
137                         ++x;
138                         --x1;
139                         if (D < 0)
140                         {
141                                 apply_pix(x, y, reverse, func, data);
142                                 apply_pix(++x, y, reverse, func, data);
143                                 apply_pix(x1, y1, reverse, func, data);
144                                 apply_pix(--x1, y1, reverse, func, data);
145                                 D += incr1;
146                         } else
147                         {
148                                 if (D < c)
149                                 {
150                                         apply_pix(x, y, reverse, func, data);
151                                         y += step;
152                                         apply_pix(++x, y, reverse, func, data);
153                                         apply_pix(x1, y1, reverse, func, data);
154                                         y1 -= step;
155                                         apply_pix(--x1, y1, reverse, func, data);
156                                 } else
157                                 {
158                                         y += step;
159                                         apply_pix(x, y, reverse, func, data);
160                                         apply_pix(++x, y, reverse, func, data);
161                                         y1 -= step;
162                                         apply_pix(x1, y1, reverse, func, data);
163                                         apply_pix(--x1, y1, reverse, func, data);
164                                 }
165                                 D += incr2;
166                         }
167                 }
168 
169                 if (pixels_left)
170                 {
171                         if (D < 0)
172                         {
173                                 apply_pix(++x, y, reverse, func, data);
174                                 if (pixels_left > 1)
175                                         apply_pix(++x, y, reverse, func, data);
176                                 if (pixels_left > 2)
177                                 {
178                                         --x1;
179                                         apply_pix(x1, y1, reverse, func, data);
180                                 }
181                         } else
182                         {
183                                 if (D < c)
184                                 {
185                                         apply_pix(++x, y, reverse, func, data);
186                                         if (pixels_left > 1)
187                                         {
188                                                 y += step;
189                                                 apply_pix(++x, y, reverse, func, data);
190                                         }
191                                         if (pixels_left > 2)
192                                                 apply_pix(--x1, y1, reverse, func, data);
193                                 } else
194                                 {
195                                         y += step;
196                                         apply_pix(++x, y, reverse, func, data);
197                                         if (pixels_left > 1)
198                                                 apply_pix(++x, y, reverse, func, data);
199                                         if (pixels_left > 2)
200                                         {
201                                                 y1 -= step;
202                                                 apply_pix(--x1, y1, reverse, func, data);
203                                         }
204                                 }
205                         }
206                 }
207         } else
208         {
209                 c = 2 * (dy - dx);
210                 incr1 = 2 * c;
211                 D = incr1 + dx;
212                 for (j = 0; j < xend; j++)
213                 {
214                         ++x;
215                         --x1;
216                         if (D > 0)
217                         {
218                                 apply_pix(x, y += step, reverse, func, data);
219                                 apply_pix(++x, y += step, reverse, func, data);
220                                 apply_pix(x1, y1 -= step, reverse, func, data);
221                                 apply_pix(--x1, y1 -= step, reverse, func, data);
222 
223                                 D += incr1;
224                         } else
225                         {
226                                 if (D < c)
227                                 {
228                                         apply_pix(x, y, reverse, func, data);
229                                         apply_pix(++x, y += step, reverse, func, data);
230                                         apply_pix(x1, y1, reverse, func, data);
231                                         apply_pix(--x1, y1 -= step, reverse, func, data);
232                                 } else
233                                 {
234                                         y += step;
235                                         apply_pix(x, y, reverse, func, data);
236                                         apply_pix(++x, y, reverse, func, data);
237                                         y1 -= step;
238                                         apply_pix(x1, y1, reverse, func, data);
239                                         apply_pix(--x1, y1, reverse, func, data);
240                                 }
241                                 D += incr2;
242                         }
243                 }
244 
245                 if (pixels_left)
246                 {
247                         if (D > 0)
248                         {
249                                 apply_pix(++x, y += step, reverse, func, data);
250                                 if (pixels_left > 1)
251                                 {
252                                         y += step;
253                                         apply_pix(++x, y, reverse, func, data);
254                                 }
255                                 if (pixels_left > 2)
256                                 {
257                                         y1 -= step;
258                                         apply_pix(--x1, y1, reverse, func, data);
259                                 }
260                         } else
261                         {
262                                 if (D < c)
263                                 {
264                                         apply_pix(++x, y, reverse, func, data);
265                                         if (pixels_left > 1)
266                                                 apply_pix(++x, y += step, reverse, func, data);
267                                         if (pixels_left > 2)
268                                                 apply_pix(--x1, y1, reverse, func, data);
269                                 } else
270                                 {
271                                         apply_pix(++x, y += step, reverse, func, data);
272                                         if (pixels_left > 1)
273                                                 apply_pix(++x, y, reverse, func, data);
274                                         if (pixels_left > 2)
275                                         {
276                                                 if (D > c)
277                                                         apply_pix(--x1, y1 -= step, reverse, func, data);
278                                                 else
279                                                         apply_pix(--x1, y1, reverse, func, data);
280                                         }
281                                 }
282                         }
283                 }
284         }
285 }
286 

~ [ 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.