I'm in somewhat unfamiliar territory here... I know C decent enough, but not math, and that's my problem here... (=
I'm attempting to create a drawLine function using the SDL stuff (which really isn't the important point). I've already got a drawPixel function (taken from a tutorial here:
http://cone3d.gamedev.net/cgi-bin/in...ls/gfxsdl/tut1 )
I've written out a drawLine function, but it's not working correctly, and I'm a little frustrated. (=
FYI: struct XYcoord only contains two doubles x and y. (doubles for precision).
Code:
void drawLine(SDL_Surface *screen, const struct XYcoord *pos1,
const struct XYcoord *pos2, Uint8 r, Uint8 g, Uint8 b)
{
struct XYcoord add,
start,
end;
// Add contains the actual increment to each coordinate to move
// from start to end. first we divide end by start to determine
// a ratio of how much to move each pixel of the line
add.x = end.x / start.x;
add.y = end.y / start.y;
// Next, we need to make sure that at least one of the add's is
// 1, so that we don't write several pixels to the same place on
// the screen. We do this by taking the largest value, and
// divide by it's positive value (to preserve sign)
if(add.x > add.y) {
add.y /= abs(add.x); // should be between 0.0 and 1.0
add.x /= abs(add.x); // should be 1.0
}
else {
add.x /= abs(add.y); // should be between 0.0 and 1.0
add.y /= abs(add.y); // should be 1.0
}
// Lastly, draw a pixel at start and increment it by add. We
// test for the (int) equality, since matching two doubles in this
// manner would probably never be true
while((int)start.x != (int)end.x && (int)start.y != (int)end.y) {
drawPixel(screen, (int)start.x, (int)start.y, r, g, b);
start.x += add.x;
start.y += add.y;
}
}
There really isn't alot to this, but for some reason, it's not working, and my brain is telling me to stop trying to figure it out and ask for help. (=
So, if anyone can help me find the problem, I'd be very happy and smile and grin and do other weird things that people do when happy.