Page MenuHomec4science

Ex1.pde
No OneTemporary

File Metadata

Created
Mon, May 6, 00:08
void settings() {
size (400, 400, P2D);
}
void setup() {
}
void draw() {
My3DPoint eye = new My3DPoint(-100, -100, -5000);
My3DPoint origin = new My3DPoint(0, 0, 0); //The first vertex of your cuboid
My3DBox input3DBox = new My3DBox(origin, 100, 150, 300);
projectBox(eye, input3DBox).render();
}
My2DBox projectBox (My3DPoint eye, My3DBox box) {
My3DPoint[] threeDP=box.p;
My2DPoint[] twoDP=new My2DPoint[threeDP.length];
for (int i=0; i<threeDP.length; ++i) {
twoDP[i]=projectPoint(eye, threeDP[i]);
}
return new My2DBox(twoDP);
}
class My2DBox {
My2DPoint[] s;
My2DBox(My2DPoint[] s) {
this.s = s;
}
void render() {
for (int i=0; i<4; i++) {
line(s[i].x, s[i].y, s[(i+1)%4].x, s[(i+1)%4].y);
line(s[i].x, s[i].y, s[i+4].x, s[i+4].y);
line(s[i+4].x, s[i+4].y, s[((i+5)%4)+4].x, s[((i+5)%4)+4].y);
}
}
}
class My3DBox {
My3DPoint[] p;
My3DBox(My3DPoint origin, float dimX, float dimY, float dimZ) {
float x = origin.x;
float y = origin.y;
float z = origin.z;
this.p = new My3DPoint[]{new My3DPoint(x, y+dimY, z+dimZ),
new My3DPoint(x, y, z+dimZ),
new My3DPoint(x+dimX, y, z+dimZ),
new My3DPoint(x+dimX, y+dimY, z+dimZ),
new My3DPoint(x, y+dimY, z),
origin,
new My3DPoint(x+dimX, y, z),
new My3DPoint(x+dimX, y+dimY, z)
};
}
My3DBox(My3DPoint[] p) {
this.p = p;
}
}
My2DPoint projectPoint(My3DPoint e, My3DPoint p) {
return new My2DPoint((p.x-e.x)/(1-p.z/e.z), (p.y-e.y)/(1-p.z/e.z));
}
class My2DPoint {
float x;
float y;
My2DPoint(float x, float y) {
this.x = x;
this.y = y;
}
}
class My3DPoint {
float x;
float y;
float z;
My3DPoint(float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
}
}

Event Timeline