Original Post
Hello there,
I am doing a 2d game and using DDraw 7. I started implementing a line drawing routine. I could have used Bresenham's Line Algorithm (http://wwwx.cs.unc.edu/~hoff/projec...e/perform0.html) but wondered if I could think a method of my own. I have come up with this method and have no idea if this has been done before. The algorithm is as follows:
PROC Line (P1, P2)
1. P1 [x1,y1] and P2 [x2, y2] are end points of the line.
2. Find Mid point M ((x1+x2)/2, (y1+y2)/2)
3. Plot M
4. if M is either equal to P1 or P2 return
5. Call Line (P1, M)
6. Call Line (M, P2)
7. return
That is, it recursively subdivides a line until the thickness of the line is 1 pixel. It plots the mid-point at each sub-division.
Like Bresenham's line algorithm, this does not use any floating point operations nor multiplication or division. The division in calculation of midpoint can be replaced by a right-shift because its a division by 2.
Here's a C implementation of the same:
void my_line (BITMAP *b, int x1, int y1, int x2, int y2, int col) {
int mx = (x1 + x2) >> 1;
int my = (y1 + y2) >> 1;
_putpixel16 (b, mx, my, col); // allegro routine
if (((mx == x1) && (my == y1)) || ((mx == x2) && (my == y2))) return;
my_line (b, x1, y1, mx, my, col);
my_line (b, mx, my, x2, y2, col);
}
I would like to know what you think about the method, how it compares to Bresenham's algorithm and what are its pros and cons.
Thank you.
[edited by - jaggu on April 18, 2003 3:17:08 PM]