Triangle SetUp Method
protected boolean triangleSetup(int width, int height) {
if (edge == null) edge = new EdgeEqn[3];
// Compute the three edge equations
edge[0] = new EdgeEqn(v[0], v[1]);
edge[1] = new EdgeEqn(v[1], v[2]);
edge[2] = new EdgeEqn(v[2], v[0]);
// Trick #1: Orient edges so that the triangle's interior lies within all of their
// positive half-spaces. Assuring that the area is positive accomplishes this
area = edge[0].C + edge[1].C + edge[2].C;
if (area == 0) {
return false; // degenerate triangle
}
if (area < 0) {
edge[0].flip();
edge[1].flip();
edge[2].flip();
area = -area;
}
// Trick #2: compute bounding box
int xflag = edge[0].flag + 2*edge[1].flag + 4*edge[2].flag;
int yflag = (xflag >> 3) - 1;
xflag = (xflag & 7) - 1;
xMin = (int) (v[sort[xflag][0]].x);
xMax = (int) (v[sort[xflag][1]].x + 1);
yMin = (int) (v[sort[yflag][1]].y);
yMax = (int) (v[sort[yflag][0]].y + 1);
// clip triangle's bounding box to raster
xMin = (xMin < 0) ? 0 : xMin;
xMax = (xMax >= width) ? width - 1 : xMax;
yMin = (yMin < 0) ? 0 : yMin;
yMax = (yMax >= height) ? height - 1 : yMax;
return true;
}
|
|