📄 pencil.java
字号:
import java.awt.*;
// a static pencil model
public class pencil 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 pencil(vector centre){
this.centre = centre;
realCentre = new vector(centre.x, centre.y, centre.z);
NoOfPolygons = 25;
iDirection = new vector(2,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(-0.07,-0.07,0).add(change).rotate_XZ(xzRotate).rotate_YZ(yzRotate);
}
private void createVertix(){
// must add vertix in a clockwise manner
int polygonIndex = 0;
// pencil rubble
double r = 0.075;
double theta = Math.PI/3;
for(int i = 0; i < 6; i++){
vertix[polygonIndex] = new vector[]{put(r*Math.cos(i*theta), r*Math.sin(i*theta), 0),
put(r*Math.cos((i+1)*theta), r*Math.sin((i+1)*theta), 0),
put(r*Math.cos((i+1)*theta), r*Math.sin((i+1)*theta), 0.15),
put(r*Math.cos(i*theta), r*Math.sin(i*theta), 0.15)};
polygonIndex++;
}
vertix[polygonIndex] = new vector[6];
for(int i = 1; i < 7; i++)
vertix[polygonIndex][6 - i] = put(r*Math.cos(i*theta), r*Math.sin(i*theta), 0);
polygonIndex++;
for(int i = 0; i < 6; i++){
vertix[polygonIndex] = new vector[]{put(r*Math.cos(i*theta), r*Math.sin(i*theta), 0.15),
put(r*Math.cos((i+1)*theta), r*Math.sin((i+1)*theta), 0.15),
put(r*Math.cos((i+1)*theta), r*Math.sin((i+1)*theta), 3.5),
put(r*Math.cos(i*theta), r*Math.sin(i*theta), 3.5)};
polygonIndex++;
}
for(int i = 0; i < 6; i++){
vertix[polygonIndex] = new vector[]{put(r*Math.cos(i*theta), r*Math.sin(i*theta), 3.5),
put(r*Math.cos((i+1)*theta), r*Math.sin((i+1)*theta), 3.5),
put(0.2*r*Math.cos((i+1)*theta), 0.2*r*Math.sin((i+1)*theta), 3.8),
put(0.2*r*Math.cos(i*theta), 0.2*r*Math.sin(i*theta), 3.8)};
polygonIndex++;
}
for(int i = 0; i < 6; i++){
vertix[polygonIndex] = new vector[]{put(0.2*r*Math.cos(i*theta), 0.2*r*Math.sin(i*theta), 3.8),
put(0.2*r*Math.cos((i+1)*theta), 0.2*r*Math.sin((i+1)*theta), 3.8),
put(0.01*r*Math.cos((i+1)*theta), 0.01*r*Math.sin((i+1)*theta), 3.9),
put(0.01*r*Math.cos(i*theta), 0.01*r*Math.sin(i*theta), 3.9)};
polygonIndex++;
}
}
private vector put(double x, double y, double z){
return centre.add(iDirection.scale(x)).add(jDirection.scale(y)).add(kDirection.scale(z));
}
public void createBoundary(){
bound[0] = new vector[]{put(0,0,0), put(0,0,3.8), put(0,0.14,3.8), put(0,0.14,0)};
boundary[0] = new paste(bound[0], Color.red, 1);
bound[1] = new vector[]{put(0,0.14,3.8), put(0.14,0.14,3.8), put(0.14,0.14,0), put(0,0.14,0)};
boundary[1] = new paste(bound[1], Color.blue, 1);
bound[2] = new vector[]{put(0,0,0), put(0,0.14,0), put(0.14,0.14,0), put(0.14,0,0)};
boundary[2] = new paste(bound[2], Color.green, 1);
bound[3] = new vector[]{put(0.14,0.14,3.8), put(0,0.14,3.8), put(0,0,3.8), put(0.14,0,3.8)};
boundary[3] = new paste(bound[3], Color.green, 1);
bound[4] = new vector[]{put(0,0,0), put(0.14,0,0), put(0.14,0,3.8), put(0,0,3.8)};
boundary[4] = new paste(bound[4], Color.green, 1);
bound[5] = new vector[]{put(0.14,0.14,3.8), put(0.14,0,3.8), put(0.14,0,0), put(0.14,0.14,0)};
boundary[5] = new paste(bound[5], Color.red, 1);
}
public paste[] getBoundary(){
return boundary;
}
public vector getCentre(){
return realCentre;
}
private void makePaste(){
for(int i = 0; i < 7; i++)
pastes[i] = new paste(vertix[i], new Color(254,102,129), 1);
for(int i = 7; i < 13; i++)
pastes[i] = new paste(vertix[i], Color.orange, 1);
for(int i = 13; i < 19; i++)
pastes[i] = new paste(vertix[i], new Color(253, 204, 138), 1);
for(int i = 19; i < 25; i++)
pastes[i] = new paste(vertix[i], Color.darkGray, 1);
}
public paste[] getPolygon(){
return pastes;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -