470,874 Members | 1,787 Online

# Stuck with Bresenham drawing line algorithm!

Below is my code,it passed the compile and link under TC2.0 correctly. But the
line it draws is obviously wrong. I think it's somewhere I thought wrong. But I
really can't find it out.
Can someone help me point out the wrong place? Thanks very much!

void bres_line(int x0,int y0,int x1,int y1,int color) {
int dx,dy,h,x,y,numx=1,numy=1,tag=1;
if (x0>x1) numx=-1;
if (y0>y1) numy=-1;
if(numx!=numy) tag=-1;
dx=abs(x0-x1);
dy=abs(y0-y1);
x=x0;y=y0;
h=2*dy*tag-dx;
putpixel(x,y,3);
while((numx<0&&x>x1)||(numx>0&&x<x1)) {
if(h<0)
h=h*numy+2*dy*tag;
else {
h=h*numy+2*(dy*tag-dx);
y+=incy;
}
putpixel(x,y,3);
x+=numx;
}
}
May 27 '06 #1
3 6761
Evo wrote:
Below is my code,it passed the compile and link under TC2.0 correctly. But the
line it draws is obviously wrong. I think it's somewhere I thought wrong. But I
really can't find it out.
Can someone help me point out the wrong place? Thanks very much!

void bres_line(int x0,int y0,int x1,int y1,int color) {
int dx,dy,h,x,y,numx=1,numy=1,tag=1;
if (x0>x1) numx=-1;
if (y0>y1) numy=-1;
if(numx!=numy) tag=-1;
dx=abs(x0-x1);
dy=abs(y0-y1);
x=x0;y=y0;
h=2*dy*tag-dx;
putpixel(x,y,3);
while((numx<0&&x>x1)||(numx>0&&x<x1)) {
if(h<0)
h=h*numy+2*dy*tag;
else {
h=h*numy+2*(dy*tag-dx);
y+=incy;
}
putpixel(x,y,3);
x+=numx;
}
}

Hi,

You say it is "obviously wrong". Then perhaps you can obviously see what
is wrong also? That obvious observation should guide you in the right
way. I am sorry, I can not see what is wrong with your code at a
5-seconds glance.
Sincerely,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
May 27 '06 #2
Evo wrote:
Below is my code,it passed the compile and link under TC2.0 correctly. But the
line it draws is obviously wrong. I think it's somewhere I thought wrong. But I
really can't find it out.
Can someone help me point out the wrong place? Thanks very much!

void bres_line(int x0,int y0,int x1,int y1,int color) {
int dx,dy,h,x,y,numx=1,numy=1,tag=1;
if (x0>x1) numx=-1;
if (y0>y1) numy=-1;
if(numx!=numy) tag=-1;
dx=abs(x0-x1);
dy=abs(y0-y1);
x=x0;y=y0;
h=2*dy*tag-dx;
putpixel(x,y,3);
while((numx<0&&x>x1)||(numx>0&&x<x1)) {
if(h<0)
h=h*numy+2*dy*tag;
else {
h=h*numy+2*(dy*tag-dx);
y+=incy;
}
putpixel(x,y,3);
x+=numx;
}

What is "incy" and where is it defined? Please post complete and
compilable code...
Sincerely,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
May 27 '06 #3
Peter Jansson wrote:

What is "incy" and where is it defined? Please post complete and
compilable code...

I have got the problem solved. Actually, I totally changed my algorithm.
I discarded the previous code. Follow is the right code for the problem:

void bresen_line (int x1,int y1,int x2,int y2,int color)
{
int iTag = 0;
int dx, dy;
int tx, ty;
int inc1, inc2;
int d;
int curx, cury;

putpixel(x1, y1, color);

if (x1 == x2 && y1 == y2)
{
return;
}

dx = abs(x2 - x1);
dy = abs(y2 - y1);
if (dx < dy)
{
iTag = 1;
iSwap(&x1, &y1);
iSwap(&x2, &y2);
iSwap(&dx, &dy);
}

tx = (x2 - x1) > 0 ? 1 : -1;
ty = (y2 - y1) > 0 ? 1 : -1;
curx = x1, cury = y1;
inc1 = 2 * dy;
inc2 = 2 * (dy - dx);
d = inc1 - dx;

while (curx != x2)
{
if (d < 0)
{
d += inc1;
}
else
{
cury += ty;
d += inc2;
}
if (iTag)
{
putpixel(cury, curx, color);
}
else
{
putpixel(curx, cury, color);
}
curx += tx;
}
}

May 30 '06 #4

### This discussion thread is closed

Replies have been disabled for this discussion.