Comments are available to members only. Sign up or Log in to view these comments.
Main Topics
Browse All Topicshey guys,
i have a 2 dimensional array that im using as a canvas on which to 'draw' lines and things on. the problem i had was that i wasnt able to construct an algorithm that would actually draw this line. i found Bresenham's line algorithm and implemented it into my solution. the only problem is that the algorithm only works for lines that are across the X axis, if you have a line that is vertical (or close enough to it) the resulting line will only have as many points in the y axis as it does in the x axis.
heres the algorithm:
// Start
void Bresenham(int x1, int y1, int x2, int y2)
{
int slope;
int dx, dy, incE, incNE, d, x, y;
// Reverse lines where x1 > x2
if (x1 > x2)
{
Bresenham(x2, y2, x1, y1);
return;
}
dx = x2 - x1;
dy = y2 - y1;
// Adjust y-increment for negatively sloped lines
if (dy < 0)
{
slope = -1;
dy = -dy;
}
else
{
slope = 1;
}
// Bresenham constants
incE = 2 * dy;
incNE = 2 * dy - 2 * dx;
d = 2 * dy - dx;
y = y1;
// Blit
for (x = x1; x <= x2; x++)
{
putpixel(x, y); // <- being the function that actually sets the pixel
if (d <= 0)
{
d += incE;
}
else
{
d += incNE;
y += slope;
}
}
}
// End
now im guessing the problem is in the loop, its checking through the x values, however if its a vertical line, well then there will only be one x value, therefore only one point will be drawn.
i've thrown in the following:
//Start
// Check to see if we have a vertical line
if (dx == 0)
{
// Line is vertical, draw it
for (y = y1; y <= y2; y++)
{
// Start drawing
putpixel(x, y);
}
// Exit Function
return;
}
// End
...up near the top of the function that caters for if the line is exactly vertical, however it doesnt cater if the lines a couple of degrees off.
i've been trying to modify the code, however so far i havnt had much luck. has anyone ever used something like this before? or would know how i can change it to allow me to draw vertical?
thanks
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: ikeworkPosted on 2007-10-25 at 23:41:05ID: 20153622
Comments are available to members only. Sign up or Log in to view these comments.