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

Linux Cross Reference
Tina6/tina-libs/tina/math/mathDraw_bresline.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_bresline.c,v $
 37  * Date    :  $Date: 2003/09/23 11:32:20 $
 38  * Version :  $Revision: 1.4 $
 39  * CVS Id  :  $Id: mathDraw_bresline.c,v 1.4 2003/09/23 11:32:20 matts Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes :  @(#)Bresenham's line-drawing algorithm - blame Jim Ivins for any
 44  * bugs. Apply func to data at integer coordinates on a straight line
 45  * between p1 and p2 inclusive.  Plot an 8-way connected line if flag is
 46  * zero; else use 4-way connections.
 47  *
 48  *********
 49 */
 50 
 51 /** 
 52  *  @file
 53  *  @brief  Bresenham's line drawing algorithm. 
 54  *
 55  *  Written by Jim Ivins
 56  * 
 57  * Apply func to data at integer coordinates on a straight line
 58  * between p1 and p2 inclusive.  Plot an 8-way connected line if flag is
 59  * zero; else use 4-way connections.
 60 */
 61 
 62 
 63 #include "mathDraw_bresline.h"
 64 
 65 #if HAVE_CONFIG_H
 66 #include <config.h>
 67 #endif
 68 
 69 #include <tina/math/math_VecDef.h>      /* or something for the Vec2's */
 70 
 71 void            apply_bresenham_line(Vec2 p1, Vec2 p2, int flag,
 72                                                 void (*func) (), void *data)
 73 {
 74         int             temp, delta, pos_e, neg_e, tot_e;
 75 
 76         int             x1 = (int) vec2_x(p1);
 77         int             y1 = (int) vec2_y(p1);
 78         int             x2 = (int) vec2_x(p2);
 79         int             y2 = (int) vec2_y(p2);
 80 
 81         int             dx = (x1 > x2) ? (x1 - x2) : (x2 - x1);
 82         int             dy = (y1 > y2) ? (y1 - y2) : (y2 - y1);
 83 
 84         if (dy <= dx)
 85         {
 86                 tot_e = 2 * dy - dx;
 87                 pos_e = 2 * dy;
 88                 neg_e = 2 * (dy - dx);
 89 
 90                 if (x1 > x2)
 91                 {
 92                         temp = x1;
 93                         x1 = x2;
 94                         x2 = temp;
 95                         temp = y1;
 96                         y1 = y2;
 97                         y2 = temp;
 98                 }
 99                 func(x1, y1, data);
100 
101                 delta = (y1 < y2) ? +1 : -1;
102 
103                 while (x1++ < x2)
104                 {
105                         if (tot_e < 0)
106                                 tot_e += pos_e;
107                         else
108                         {
109                                 tot_e += neg_e;
110                                 if (flag)
111                                         func(x1, y1, data);
112                                 y1 += delta;
113                         }
114                         func(x1, y1, data);
115                 }
116         } else
117         {
118                 tot_e = 2 * dx - dy;
119                 pos_e = 2 * dx;
120                 neg_e = 2 * (dx - dy);
121 
122                 if (y1 > y2)
123                 {
124                         temp = y1;
125                         y1 = y2;
126                         y2 = temp;
127                         temp = x1;
128                         x1 = x2;
129                         x2 = temp;
130                 }
131                 func(x1, y1, data);
132 
133                 delta = (x1 < x2) ? +1 : -1;
134 
135                 while (y1++ < y2)
136                 {
137                         if (tot_e < 0)
138                                 tot_e += pos_e;
139                         else
140                         {
141                                 tot_e += neg_e;
142                                 if (flag)
143                                         func(x1, y1, data);
144                                 x1 += delta;
145                         }
146                         func(x1, y1, data);
147                 }
148         }
149 }
150 

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