Original Post
Hi, I am working on a line algorithm that first clips the line to the screen and then renders it. For accuracy I am doing everything in Integer math (aka dy/dx is never resolved into a fraction) But I notice that C++ integer division doesn't round the answer. I dont need floating point accuracy because at the end of the day it is for pixel rendering and so 1.43 means no more to me than 1. But sometimes the correct answer (using algebra) is 1.998 and then the value stored in c++ is 1, is there any CHEAP way of getting rounding done? Regards and Thanks in Advance. Here is some of my code
int deltaX = x2-x1;
int deltaY = y2-y1;
// The line equation is y = mx+c or
// y = m1/m2 *x + c, now multiply m2 out
// m2y = m1x + m2c
// c = (m2y - m1x) / m2c
// m1 = dy and m2 = dx
// Find the C-Cut
int c = (deltaX * y1 - deltaY * x1)/deltaX;
// CLIP the line using the two x axeses
int sx = x1;
int ex = x2;
if (sx < 0) sx = 0;
if (ex >= g_iWidth) ex = g_iWidth-1;
// Now we must find the corrosponding y values
// y = mx+c
int sy = (deltaY * sx)/deltaX + c;
int ey = (deltaY * ex)/deltaX + c;