| 1 |
/* $Id: bresenhm.c,v 1.2 2000/10/27 15:21:40 mholst Exp $ */ |
| 2 |
|
| 3 |
/* |
| 4 |
* Mesa 3-D graphics library |
| 5 |
* Version: 2.0 |
| 6 |
* Copyright (C) 1995-1996 Brian Paul |
| 7 |
* |
| 8 |
* This library is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU Library General Public |
| 10 |
* License as published by the Free Software Foundation; either |
| 11 |
* version 2 of the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This library is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* Library General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Library General Public |
| 19 |
* License along with this library; if not, write to the Free |
| 20 |
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "bresenhm.h" |
| 24 |
#include "types.h" |
| 25 |
|
| 26 |
|
| 27 |
/* |
| 28 |
* Evaluate Bresenham's integer line drawing algorithm. Put each |
| 29 |
* coordinate generated into x[] and y[] arrays. |
| 30 |
* |
| 31 |
* Input: x1,y1 - coordinates of first endpoint |
| 32 |
* x2,y2 - coordinates of second endpoint |
| 33 |
* Output: x, y - array of coordinates generated by the algorithm |
| 34 |
* Return: number of values put into x[] and y[]. |
| 35 |
*/ |
| 36 |
GLuint gl_bresenham( GLcontext* ctx, GLint x1, GLint y1, GLint x2, GLint y2, |
| 37 |
GLint x[], GLint y[] ) |
| 38 |
{ |
| 39 |
register GLint dx, dy, xf, yf, a, b, c, i; |
| 40 |
|
| 41 |
if (x2>x1) { |
| 42 |
dx = x2-x1; |
| 43 |
xf = 1; |
| 44 |
} |
| 45 |
else { |
| 46 |
dx = x1-x2; |
| 47 |
xf = -1; |
| 48 |
} |
| 49 |
|
| 50 |
if (y2>y1) { |
| 51 |
dy = y2-y1; |
| 52 |
yf = 1; |
| 53 |
} |
| 54 |
else { |
| 55 |
dy = y1-y2; |
| 56 |
yf = -1; |
| 57 |
} |
| 58 |
|
| 59 |
#define PLOT( X, Y ) x[i] = X; y[i] = Y; |
| 60 |
|
| 61 |
if (dx>dy) { |
| 62 |
a = dy+dy; |
| 63 |
c = a-dx; |
| 64 |
b = c-dx; |
| 65 |
for (i=0;i<=dx;i++) { |
| 66 |
PLOT( x1, y1 ); |
| 67 |
x1 += xf; |
| 68 |
if (c<0) { |
| 69 |
c += a; |
| 70 |
} |
| 71 |
else { |
| 72 |
c += b; |
| 73 |
y1 += yf; |
| 74 |
} |
| 75 |
} |
| 76 |
return dx+1; |
| 77 |
} |
| 78 |
else { |
| 79 |
a = dx+dx; |
| 80 |
c = a-dy; |
| 81 |
b = c-dy; |
| 82 |
for (i=0;i<=dy;i++) { |
| 83 |
PLOT( x1, y1 ); |
| 84 |
y1 += yf; |
| 85 |
if (c<0) { |
| 86 |
c += a; |
| 87 |
} |
| 88 |
else { |
| 89 |
c += b; |
| 90 |
x1 += xf; |
| 91 |
} |
| 92 |
} |
| 93 |
return dy+1; |
| 94 |
} |
| 95 |
|
| 96 |
#undef PLOT |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
/* |
| 103 |
* Evaluate Bresenham's line algorithm with stippling. |
| 104 |
* Input: x1, y1, x2, y2 - endpoints of line segment |
| 105 |
* Output: x, y - arrays of pixels along the line |
| 106 |
* mask - indicates draw/don't draw for each pixel |
| 107 |
*/ |
| 108 |
GLuint gl_stippled_bresenham( GLcontext* ctx, GLint x1, GLint y1, GLint x2, GLint y2, |
| 109 |
GLint x[], GLint y[], GLubyte mask[] ) |
| 110 |
{ |
| 111 |
GLint dx, dy, xf, yf, a, b, c, i; |
| 112 |
GLushort m; |
| 113 |
|
| 114 |
if (x2>x1) { |
| 115 |
dx = x2-x1; |
| 116 |
xf = 1; |
| 117 |
} |
| 118 |
else { |
| 119 |
dx = x1-x2; |
| 120 |
xf = -1; |
| 121 |
} |
| 122 |
|
| 123 |
if (y2>y1) { |
| 124 |
dy = y2-y1; |
| 125 |
yf = 1; |
| 126 |
} |
| 127 |
else { |
| 128 |
dy = y1-y2; |
| 129 |
yf = -1; |
| 130 |
} |
| 131 |
|
| 132 |
#define PLOT( X, Y ) \ |
| 133 |
m = 1 << ((ctx->StippleCounter/ctx->Line.StippleFactor) & 0xf); \ |
| 134 |
if (ctx->Line.StipplePattern & m) { \ |
| 135 |
mask[i] = 1; \ |
| 136 |
x[i] = X; \ |
| 137 |
y[i] = Y; \ |
| 138 |
} \ |
| 139 |
else { \ |
| 140 |
mask[i] = 0; \ |
| 141 |
} \ |
| 142 |
ctx->StippleCounter++; |
| 143 |
|
| 144 |
if (dx>dy) { |
| 145 |
a = dy+dy; |
| 146 |
c = a-dx; |
| 147 |
b = c-dx; |
| 148 |
for (i=0;i<=dx;i++) { |
| 149 |
PLOT( x1, y1 ); |
| 150 |
x1 += xf; |
| 151 |
if (c<0) { |
| 152 |
c += a; |
| 153 |
} |
| 154 |
else { |
| 155 |
c += b; |
| 156 |
y1 += yf; |
| 157 |
} |
| 158 |
} |
| 159 |
return dx+1; |
| 160 |
} |
| 161 |
else { |
| 162 |
a = dx+dx; |
| 163 |
c = a-dy; |
| 164 |
b = c-dy; |
| 165 |
for (i=0;i<=dy;i++) { |
| 166 |
PLOT( x1, y1 ); |
| 167 |
y1 += yf; |
| 168 |
if (c<0) { |
| 169 |
c += a; |
| 170 |
} |
| 171 |
else { |
| 172 |
c += b; |
| 173 |
x1 += xf; |
| 174 |
} |
| 175 |
} |
| 176 |
return dy+1; |
| 177 |
} |
| 178 |
|
| 179 |
#undef PLOT |
| 180 |
} |