📄 book.java
字号:
import java.awt.*;
// a static book model
public class book implements model{
private vector centre;
private vector iDirection, jDirection, kDirection;
private vector[][] vertix;
private paste[] pastes;
private vector[] polygonNormals;
private paste[] boundary;
private int NoOfPolygons;
private vector[][] bound;
private vector realCentre;
public book(vector centre){
this.centre = centre;
realCentre = new vector(centre.x, centre.y, centre.z);
NoOfPolygons = 24;
iDirection = new vector(0.7,0,1);
jDirection = new vector(0,1,0);
kDirection = iDirection.cross(jDirection).unit();
iDirection = iDirection.unit();
jDirection = jDirection.unit();
bound = new vector[6][4];
boundary = new paste[6];
vertix = new vector[NoOfPolygons][];
pastes = new paste[NoOfPolygons];
createVertix();
makePaste();
createBoundary();
}
public void update(vector change, double xzRotate, double yzRotate){
// update the vertix accroding to the camera position and direction
vector[][] tempVertix = new vector[NoOfPolygons][];
for(int i = 0; i < tempVertix.length; i++){
tempVertix[i] = new vector[vertix[i].length];
for(int j = 0; j < tempVertix[i].length; j++){
tempVertix[i][j] = vertix[i][j].add(change).rotate_XZ(xzRotate).rotate_YZ(yzRotate);
}
}
for(int i = 0; i < NoOfPolygons; i++)
pastes[i].update(tempVertix[i]);
//update boundary
vector[][] tempBound = new vector[6][4];
for(int i = 0; i < 6; i++){
for(int j = 0; j < 4; j++){
tempBound[i][j] = bound[i][j].add(change).rotate_XZ(xzRotate).rotate_YZ(yzRotate);
}
}
for(int i = 0; i < 6; i ++)
boundary[i].update(tempBound[i]);
//update realcentre;
realCentre = put(3,0.35,2.4).add(change).rotate_XZ(xzRotate).rotate_YZ(yzRotate);
}
private void createVertix(){
// must add vertix in a clockwise manner
int polygonIndex = 0;
//construct the top part of the book
vertix[polygonIndex] = new vector[]{put(0,0.5,0), put(0,0.5,5), put(3,0.7,5), put(3,0.7,0)};
polygonIndex++;
vertix[polygonIndex] = new vector[]{put(3,0.55,0), put(3,0.55,5),put(0,0.35,5), put(0,0.35,0)};
polygonIndex++;
vertix[polygonIndex] = new vector[]{put(0,0.5,0), put(3,0.7,0),put(3,0.55,0), put(0,0.35,0)};
polygonIndex++;
vertix[polygonIndex] = new vector[]{put(0,0.35,5), put(3,0.55,5), put(3,0.7,5),put(0,0.5,5)};
polygonIndex++;
vertix[polygonIndex] = new vector[]{put(0,0.35,0), put(0,0.35,5), put(0,0.5,5),put(0,0.5,0)};
polygonIndex++;
vertix[polygonIndex] = new vector[]{put(3,0.7,0), put(3,0.7,5), put(3.3,0.7,5),put(3.3,0.7,0)};
polygonIndex++;
vertix[polygonIndex] = new vector[]{put(3.3,0.55,0), put(3.3,0.55,5), put(3,0.55,5),put(3,0.55,0)};
polygonIndex++;
vertix[polygonIndex] = new vector[]{put(3,0.7,0), put(3.3,0.7,0), put(3.3,0.55,0),put(3,0.55,0)};
polygonIndex++;
vertix[polygonIndex] = new vector[]{put(3,0.55,5), put(3.3,0.55,5), put(3.3,0.7,5) ,put(3,0.7,5)};
polygonIndex++;
vertix[polygonIndex] = new vector[]{put(3.3,0.7,0), put(3.3,0.7,5), put(6.3,0.5,5), put(6.3,0.5,0)};
polygonIndex++;
vertix[polygonIndex] = new vector[]{put(6.3,0.35,0), put(6.3,0.35,5), put(3.3,0.55,5), put(3.3,0.55,0)};
polygonIndex++;
vertix[polygonIndex] = new vector[]{put(6.3,0.5,0), put(6.3,0.5,5), put(6.3,0.35,5),put(6.3,0.35,0)};
polygonIndex++;
vertix[polygonIndex] = new vector[]{put(3.3,0.7,0), put(6.3,0.5,0),put(6.3,0.35,0), put(3.3,0.55,0)};
polygonIndex++;
vertix[polygonIndex] = new vector[]{put(3.3,0.55,5), put(6.3,0.35,5), put(6.3,0.5,5),put(3.3,0.7,5)};
polygonIndex++;
//construct lower part of the book
vertix[polygonIndex] = new vector[]{put(0.4,0,0.2), put(0.2,0.35,0.2), put(3.0,0.55,0.2), put(3.15,0.2,0.2)};
polygonIndex++;
vertix[polygonIndex] = new vector[]{put(3.15,0.2,4.8), put(3.0,0.55,4.8), put(0.2,0.35,4.8), put(0.4,0,4.8)};
polygonIndex++;
vertix[polygonIndex] = new vector[]{put(0.2,0.35,0.2), put(0.4,0,0.2), put(0.4,0,4.8), put(0.2,0.35,4.8)};
polygonIndex++;
vertix[polygonIndex] = new vector[]{put(3.15,0.2,0.2), put(3.0,0.55,0.2), put(3.0,0.55,4.8), put(3.15,0.2,4.8)};
polygonIndex++;
vertix[polygonIndex] = new vector[]{put(0.4,0,0.2), put(3.15,0.2,0.2), put(3.15,0.2,4.8), put(0.4,0,4.8)};
polygonIndex++;
vertix[polygonIndex] = new vector[]{put(3.15,0.2,0.2), put(3.3,0.55,0.2), put(6.1,0.35,0.2), put(5.9,0,0.2)};
polygonIndex++;
vertix[polygonIndex] = new vector[]{put(5.9,0,4.8), put(6.1,0.35,4.8), put(3.3,0.55,4.8), put(3.15,0.2,4.8)};
polygonIndex++;
vertix[polygonIndex] = new vector[]{put(3.3,0.55,0.2), put(3.15,0.2,0.2), put(3.15,0.2,4.8), put(3.3,0.55,4.8)};
polygonIndex++;
vertix[polygonIndex] = new vector[]{put(5.9,0,0.2), put(6.1,0.35,0.2), put(6.1, 0.35,4.8), put(5.9,0,4.8)};
polygonIndex++;
vertix[polygonIndex] = new vector[]{put(3.15,0.2,0.2), put(5.9,0,0.2), put(5.9,0,4.8), put(3.15,0.2,4.8)};
polygonIndex++;
}
public void createBoundary(){
bound[0] = new vector[]{put(0,0,0), put(0,0,4.8), put(0,0.7,4.8), put(0,0.7,0)};
boundary[0] = new paste(bound[0], Color.red, 1);
bound[1] = new vector[]{put(0,0.7,4.8), put(6,0.7,4.8), put(6,0.7,0), put(0,0.7,0)};
boundary[1] = new paste(bound[1], Color.blue, 1);
bound[2] = new vector[]{put(0,0,0), put(0,0.7,0), put(6,0.7,0), put(6,0,0)};
boundary[2] = new paste(bound[2], Color.green, 1);
bound[3] = new vector[]{put(6,0.7,4.8), put(0,0.7,4.8), put(0,0,4.8), put(6,0,4.8)};
boundary[3] = new paste(bound[3], Color.green, 1);
bound[4] = new vector[]{put(0,0,0), put(6,0,0), put(6,0,4.8), put(0,0,4.8)};
boundary[4] = new paste(bound[4], Color.green, 1);
bound[5] = new vector[]{put(6,0.7,4.8), put(6,0,4.8), put(6,0,0), put(6,0.7,0)};
boundary[5] = new paste(bound[5], Color.red, 1);
}
private vector put(double x, double y, double z){
return centre.add(iDirection.scale(x)).add(jDirection.scale(y)).add(kDirection.scale(z));
}
public paste[] getPolygon(){
return pastes;
}
public paste[] getBoundary(){
return boundary;
}
public vector getCentre(){
return realCentre;
}
private void makePaste(){
for(int i = 0; i < 14; i++)
pastes[i] =new paste(vertix[i], new Color(194, 133, 250), 1);
for(int i = 14; i < 24; i++)
pastes[i] =new paste(vertix[i], new Color(233, 231, 228), 2);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -