Original Post
My friend and I have been working on figuring out how to draw a triangle for the past week and a half or so, and have yet to find a way that works. Some requirements for writing this are that it must be in C and is limited to the C Standard Library and anything else we write ourselves, meaning absolutely NO third party libraries. Thus far we have made three attempts, one is a slightly altered version of the method from Tricks of the Windows Game Programming Gurus, Our own from-scratch method, and converting some Delphi code (Most successful so far, still incorrect). The points I have sent are (0,0), (255,128), (0,255) which has an intended output like: # ### ##### ### # results in: # ## ### ### ### The original triangle delphi code Ported code: the structures of use in this situation we have written so far are: Please take note, this is not a "My code is broken, fix it post," well, it sort of is, but I've actually made attempts at fixing it before posting here. All I need is either someone to point out a stupid mistake (most likely cause) or a better example of how to draw a triangle in 2D (almost all google results are related to OpenGL in some way). Thanks in advance. [Edited by - amnesiasoft on December 10, 2005 11:05:25 PM]
void DrawTriangle(Triangle *t, Image *dst) {
int x, y;
int height;
float dx_r, dx_l;
float xs, xe;
int xl, xr;
int tx, ty;
int minx, maxx, miny, maxy;
//will the triangle even show up?
//...Removed to shorten code...
//guess so...
//rearrange the points
//...Sorting removed as it has been checked to work...
xl = t->p2.x;
/*
if(t->p3.y == t->p1.y) {
xr = 10000;
} else {
xr = round(t->p1.x + (t->p2.y - t->p1.y) * (t->p3.y - t->p1.y) / (t->p3.y - t->p1.y));
} //This code caused a square on the bottom right of the output
*/
xr = min(t->p1.x, t->p3.x); //works better
if(xr < xl) { //make sure the right is really on the right
tx = xr;
xr = xl;
xl = tx;
}
if(t->p1.y != t->p2.y) {
height = t->p2.y - t->p1.y;
xs = t->p1.x;
xe = xs;
dx_l = (xl - t->p1.x) / height;
dx_r = (xr - t->p1.x) / height;
miny = max(0, t->p1.y);
maxy = min(255,t->p2.y);
for(y = miny; y <= maxy; y++) {
minx = max(0, (int)xs);
maxx = min(dst->w, (int)xe);
memset(dst->r + C2to1(minx, y, dst->w), brush.r, maxx - minx); //C2to1 just converts a 2D array index into a 1D index, it works, I've used it everywhere else
memset(dst->g + C2to1(minx, y, dst->w), brush.g, maxx - minx);
memset(dst->b + C2to1(minx, y, dst->w), brush.b, maxx - minx);
xs += dx_l;
xe += dx_r;
}
}
if(t->p2.y != t->p3.y) {
height = t->p3.y - t->p2.y;
dx_l = (t->p3.x - xl) / height;
dx_r = (t->p3.x - xr) / height;
xs = xl;
xe = xr;
if(t->p3.y >= dst->h) {
t->p3.y = dst->h - 1;
}
miny = max(0, t->p2.y + 1);
maxy = min(255, t->p3.y);
for(y = miny; y <= maxy; y++) {
minx = max(0, (int)xs);
maxx = min(255, (int)xe);
memset(dst->r + C2to1(minx, y, dst->w), brush.r, maxx - minx);
memset(dst->g + C2to1(minx, y, dst->w), brush.g, maxx - minx);
memset(dst->b + C2to1(minx, y, dst->w), brush.b, maxx - minx);
}
xs += dx_l;
xe += dx_r;
}
}
typedef struct {
Uint8 *r; //Array containing all the red information about each pixel
Uint8 *g; //Array containing all the green information about each pixel
Uint8 *b; //Array containing all the blue information about each pixel
//Yes I know parallel arrays are bad, but that's not the problem here
int w; //width of the image
int h; //height of the image
} Image;
typedef struct {
int x;
int y;
} Point;
typedef struct {
Point p1, p2, p3;
} Triangle;