#### Archived

This topic is now archived and is closed to further replies.

# bresenham clipping problems (solved)

This topic is 5395 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

so i have a bit of a problem that only comes up occasionally but is still a definite problem. basically...lets say that i have a 400x400 drawing space, and whatever line (abs(slope) > 1)im drawing's second point is clipped to (399, 399). the problem is, as the line is stepped through, theres an error somewhere. the y value definitely stops at 399 (399*400 = 159600) but the x value ends up on 400 (159600 + 400 = 160000...ie out of range of our pixel array). can anyone with experience with this take a look at my algorithm? i swear it is right...but it definitely wraps to (what would be) the next line, which doesnt exist. thanks in advance. code:
int deltax = (x2 - x1);
int deltay = (y2 - y1);
if (deltay < 0) { deltay = -deltay;  incy = -clipx; } else { incy = clipx; }
if (deltax < 0) { deltax = -deltax;  incx = -1; } else { incx = 1; }
deltax <<= 1;
deltay <<= 1;
y1 *= clipx;
y2 *= clipx;
pix[y1 + x1] = color;

//more horizontal
if (deltax > deltay) {
e = deltay - (deltax >> 1);
while (x1 != x2) {
if (e >= 0) {
y1 += incy;
e -= deltax;
}
x1 += incx;
e += deltay;

pix[y1 + x1] = color;
}

//more vertical....problem here with clipping
} else {
e = deltax - (deltay >> 1);
while (y1 != y2) {
if (e >= 0) {
x1 += incx;
e -= deltay;
}
y1 += incy;
e += deltax;
pix[y1 + x1] = color;
}
}

[edited by - justo on February 8, 2004 6:22:43 PM] [edited by - justo on February 8, 2004 6:44:51 PM]

##### Share on other sites
let this be a lesson to all you kids out there...always update your dx and dy after you clip a line. the slope might not change, but your bresenham step size will.

hopefully in 6 months someone will be implemementing bresenham and will search and find this code...thats all this thread is really good for.

##### Share on other sites
I had a very similar problem with grid traversal.

My sun was at 10000,10000,10000 and my grid was like 200x200x10 in size. I clamped the rays to the box instead of clipping them, giving me really confusing errors where on part of the map, and at certain sun angles, the lighting was perfect, other times it was totally wrong.

I think the key is to clip ( not clamp ) the ray or line to the valid range before passing it in to your stepping function.

1. 1
Rutin
38
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633350
• Total Posts
3011473
• ### Who's Online (See full list)

There are no registered users currently online

×