Stuck with Bresenham drawing line algorithm!

E

Evo

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;
}
}
 
P

Peter Jansson

Evo said:
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/
 
P

Peter Jansson

Evo said:
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/
 
E

Evo

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

Thanks for your reply.
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;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top