topical media & game development
graphic-processing-algorithm-Ch09-p221-MyFace.pde / pde
class MyFace {
int npoints = 0; //the number of points
MyPoint [] points; //array of points
MyFace (){
points = new MyPoint[0];
}
MyFace(MyPoint[] inPoints){
points = new MyPoint[inPoints.length];
npoints = inPoints.length;
for(int i=0; i<inPoints.length; i++)
points[i] = new
MyPoint(inPoints[i].x, inPoints[i].y, inPoints[i].z);
}
void addPoint(float addX, float addY, float addZ){
npoints++;
points=(MyPoint[])append(points, new MyPoint(addX,addY,addZ));
}
void move(float xoff, float yoff, float zoff){
for(int i=0; i<npoints; i++)
points[i].move(xoff, yoff, zoff);
}
void rotatex (float angle, MyPoint ref) {
for(int i=0; i<npoints; i++)
points[i].rotatex(angle, ref);
}
void rotatey (float angle, MyPoint ref) {
for(int i=0; i<npoints; i++)
points[i].rotatey(angle, ref);
}
void rotatez (float angle, MyPoint ref) {
for(int i=0; i<npoints; i++)
points[i].rotatez(angle, ref);
}
void scale(float xs, float ys, float zs, MyPoint ref){
for(int i=0; i<npoints; i++)
points[i].scale(xs, ys, zs, ref);
}
void draw(){
beginShape(QUADS);
for(int i = 0; i < npoints; i++){
vertex(points[i].x,points[i].y, points[i].z);
}
endShape(CLOSE);
}
boolean isVisible() {
float x1, y1, x2, y2, norm=0;
int ahead1, ahead2;
for (int i=0; i<npoints; i++) {
ahead1 = i+1;
ahead2 = i+2;
if(i == (npoints-2)) ahead2 = 0;
if(i == (npoints-1)) {
ahead2=1;
ahead1=0;
}
//make vector 1
x1 = points[ahead1].xscreen() - points[i].xscreen();
y1 = points[ahead1].yscreen() - points[i].yscreen();
//make vector 2
x2 = points[ahead1].xscreen() - points[ahead2].xscreen();
y2 = points[ahead1].yscreen() - points[ahead2].yscreen();
//cross product
norm += (x1*y2 - y1*x2);
}
if(norm > 0.0) return false; //if clockwise
else return true; //else ccw
}
}
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.