📄 lamp.java
字号:
import java.awt.*;
// a static lamp model
public class lamp 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 lamp(vector centre){
this.centre = centre;
realCentre = centre;
NoOfPolygons = 37;
iDirection = new vector(1,0,0);
jDirection = new vector(0,1,0);
kDirection = new vector(0,0,1);
vertix = new vector[NoOfPolygons][];
pastes = new paste[NoOfPolygons];
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(-1.9,3,-1.9).add(change).rotate_XZ(xzRotate).rotate_YZ(yzRotate);
}
private void createVertix(){
// must add vertix in a clockwise manner
int polygonIndex = 0;
//construct the buttom of the lamp
double r = 1.9;
double theta = Math.PI/8;
for(int i = 0; i < 16; i++){
vertix[polygonIndex] = new vector[]{put(r*Math.cos(i*theta), 0.5, r*Math.sin(i*theta)),
put(r*Math.cos((i+1)*theta), 0.5,r*Math.sin((i+1)*theta)),
put(1.1*r*Math.cos((i+1)*theta), 0.05, 1.1*r*Math.sin((i+1)*theta)),
put(1.1*r*Math.cos(i*theta), 0.05, 1.1*r*Math.sin(i*theta))};
polygonIndex++;
}
vertix[polygonIndex] = new vector[16];
for(int i = 0; i < 16; i++)
vertix[polygonIndex][15 - i] = put(r*Math.cos(i*theta), 0.5, r*Math.sin(i*theta));
polygonIndex++;
//construct the middle part of the lamp
r = 0.25;
theta = Math.PI/3;
for(int i = 0; i < 6; i++){
vertix[polygonIndex] = new vector[]{put(r*Math.cos(i*theta), 5, r*Math.sin(i*theta)),
put(r*Math.cos((i+1)*theta), 5,r*Math.sin((i+1)*theta)),
put(r*Math.cos((i+1)*theta), 0.55, r*Math.sin((i+1)*theta)),
put(r*Math.cos(i*theta), 0.55, r*Math.sin(i*theta))};
polygonIndex++;
}
//construct the top of the lamp
r = 1.7;
for(int i = 0; i < 6; i++){
vertix[polygonIndex] = new vector[]{put(r*Math.cos(i*theta), 6, r*Math.sin(i*theta)),
put(r*Math.cos((i+1)*theta), 6,r*Math.sin((i+1)*theta)),
put(1.3*r*Math.cos((i+1)*theta), 4, 1.3*r*Math.sin((i+1)*theta)),
put(1.3*r*Math.cos(i*theta), 4, 1.3*r*Math.sin(i*theta))};
polygonIndex++;
}
for(int i = 0; i < 6; i++){
vertix[polygonIndex] = new vector[]{
put(1.3*r*Math.cos(i*theta), 4, 1.3*r*Math.sin(i*theta)),
put(1.3*r*Math.cos((i+1)*theta), 4, 1.3*r*Math.sin((i+1)*theta)),
put(r*Math.cos((i+1)*theta), 6,r*Math.sin((i+1)*theta)),
put(r*Math.cos(i*theta), 6, r*Math.sin(i*theta))
};
polygonIndex++;
}
vertix[polygonIndex] = new vector[6];
for(int i = 0; i < 6; i++)
vertix[polygonIndex][5 - i] = put(r*Math.cos(i*theta), 6, r*Math.sin(i*theta));
polygonIndex++;
vertix[polygonIndex] = new vector[6];
for(int i = 0; i < 6; i++)
vertix[polygonIndex][i] = put(r*Math.cos(i*theta), 6, r*Math.sin(i*theta));
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 paste[] getPolygon(){
return pastes;
}
private void makePaste(){
for(int i = 0; i < 16; i++)
pastes[i] = new paste(vertix[i], new Color(137, 171, 252), 1);
pastes[16] = new paste(vertix[16], new Color(137, 171, 252), 1);
for(int i = 0; i < 6; i++)
pastes[17+i] = new paste(vertix[17+i], new Color(137, 171, 252), 2);
for(int i = 0; i < 6; i++)
pastes[23+i] = new paste(vertix[23+i], new Color(227, 249, 81), 1);
for(int i = 0; i < 6; i++)
pastes[29+i] = new paste(vertix[29+i], new Color(255-i*2, 255 -i*2, 255 - i*2), 0);
pastes[35] = new paste(vertix[35], new Color(227, 249, 81), 1);
pastes[36] = new paste(vertix[35], new Color(220, 220, 220), 0);
}
public void createBoundary(){
vector temp = centre;
centre = put(-1.9,0,-1.9);
bound[0] = new vector[]{put(0,0,0), put(0,0,3.8), put(0,6,3.8), put(0,6,0)};
boundary[0] = new paste(bound[0], Color.green, 1);
bound[1] = new vector[]{put(0,6,3.8), put(3.8,6,3.8), put(3.8,6,0), put(0,6,0)};
boundary[1] = new paste(bound[1], Color.green, 1);
bound[2] = new vector[]{put(0,0,0), put(0,6,0), put(3.8,6,0), put(3.8,0,0)};
boundary[2] = new paste(bound[2], Color.green, 1);
bound[3] = new vector[]{put(3.8,6,3.8), put(0,6,3.8), put(0,0,3.8), put(3.8,0,3.8)};
boundary[3] = new paste(bound[3], Color.green, 1);
bound[4] = new vector[]{put(0,0,0), put(3.8,0,0), put(3.8,0,3.8), put(0,0,3.8)};
boundary[4] = new paste(bound[4], Color.green, 1);
bound[5] = new vector[]{put(3.8,6,3.8), put(3.8,0,3.8), put(3.8,0,0), put(3.8,6,0)};
boundary[5] = new paste(bound[5], Color.green, 1);
centre = temp;
}
public paste[] getBoundary(){
return boundary;
}
public vector getCentre(){
return realCentre;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -