473,385 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Stuck with Bresenham drawing line algorithm!

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;
}
}
May 27 '06 #1
3 6915
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
Evo
Peter Jansson wrote:

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

May 30 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: geoffblanduk_nospam | last post by:
Given a set of intervals {i1, i2, i3, ...} a list is produced; the base items (0) are placed one at a time into the stream and after the ix one a interval item (1, 2, 3, etc) of the correct type is...
4
by: Peter Oliphant | last post by:
There doesn't seem to be any documentation on how to create and/or use an instance of System::Drawing::Graphics. On-line MSDN talks about this class, and says: " The Graphics class provides...
2
by: George | last post by:
Dear colleagues, I refer to your help with specific graphic problem. It is necessary to create a viewfinder in graphic application. It seems that the algorithm is simple: just draw lines in...
4
by: rakesh.Mysore | last post by:
Can any one please suggest me a algorithm to draw horizontal and vertical line between two points (a right angled line, not a slant line).
11
by: Tom C | last post by:
We have a window named FormApplication which just happens to be our mid parent window. When I open it in the designer, it is stuck in a loop redisplaying a c1flexgrid. maxing out our cpu's. By...
2
z0z0
by: z0z0 | last post by:
Can anyone please tell me the code to implement DDA and Bresenham's algorithm for line and circle drawing in C- Language ? ? ?
1
by: priyankadv | last post by:
how do i code for bresenham's algo for circle in anticlockwise dir???
12
by: Petra Rohmer | last post by:
Hello, I want to ake following 0,0 (900mm,900mm) -------------------------------------- |....................................| |....................................|...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.