You asked for it blahblahblah!
My triangle code is quite simple
I’m concerned about the code that draws scan lines since this is where the worst performance hit is.
Heres my scan line drawing code …
public void setScanLine(int x1, int x2, int y, int color)
{
// begining and end of the array's indexes that are to be set
int start, end;
start = width*((height-1)-y);
end = start;
start += x1;
end += x2;
for(int i = start; i <= end; i++)
colorBuffer[i] = color;
}
And here’s the triangle code …
public void drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int color)
{
// primary and secondary lines
RasterLine pl, sl;
// setup a raster triangle
triangle.set(x1, y1, x2, y2, x3, y3);
// perform sorting of points wrt y components
triangle.sortPoinsByY();
// update lines of the triangle
triangle.updateLines();
// now we can begin working out the scan line intersections
// first get the priamry and secondary lines
pl = triangle.l1;
sl = triangle.l2;
// check if the primary line is horizontal
// (remember that the primary line has the gratest dy)
if(pl.getType() == RasterLine.HORIZONTAL ||
pl.getType() == RasterLine.POINT)
{
// then the whole triangle is a horizontal line
drawScanLine(pl.x1, pl.x2, pl.y1, color);
}
else // we have a non-degenerate (normal) triangle
{
// now traverse the y component of the longest line wrt y-axis
for(int y = triangle.l1.y1; y <= triangle.l1.y2; y++)
{
// see if the secondary line is horizontal or if it's end was reached
if(sl.getType() == RasterLine.HORIZONTAL ||
( y == triangle.l2.y2 && triangle.l3.getType() != RasterLine.HORIZONTAL ))
sl = triangle.l3;
// get the x intersection from the primary and secondary lines
drawScanLine(pl.getX(y), sl.getX(y), y, color);
}
}
}
Note that I don’t create any new objects in my triangle code.