   bresenham clipping problems (solved)
  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 horizontalif (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]
  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. ;  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.