Skip to content

Commit 1d2d84e

Browse files
author
Marc Bickel
committed
Week2 of the project
Everything works fine
1 parent e1c4bdc commit 1d2d84e

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed

Week2_Sketch/Week2_Sketch.pde

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
void settings() {
2+
size (1000, 1000, P2D);
3+
}
4+
void setup() {
5+
}
6+
void draw() {
7+
background(255, 255, 255);
8+
My3DPoint eye = new My3DPoint(0, 0, -5000);
9+
My3DPoint origin = new My3DPoint(0, 0, 0);
10+
My3DBox input3DBox = new My3DBox(origin, 100, 150, 300);
11+
//rotated around x
12+
float[][] transform1 = rotateXMatrix(PI/8);
13+
input3DBox = transformBox(input3DBox, transform1);
14+
projectBox(eye, input3DBox).render();
15+
//rotated and translated
16+
float[][] transform2 = translationMatrix(200, 200, 0);
17+
input3DBox = transformBox(input3DBox, transform2);
18+
projectBox(eye, input3DBox).render();
19+
//rotated, translated, and scaled
20+
float[][] transform3 = scaleMatrix(2, 2, 2);
21+
input3DBox = transformBox(input3DBox, transform3);
22+
projectBox(eye, input3DBox).render();
23+
}
24+
25+
class My2DPoint {
26+
float x;
27+
float y;
28+
My2DPoint(float x, float y) {
29+
this.x = x;
30+
this.y = y;
31+
}
32+
}
33+
34+
class My3DPoint {
35+
float x;
36+
float y;
37+
float z;
38+
My3DPoint(float x, float y, float z) {
39+
this.x = x;
40+
this.y = y;
41+
this.z = z;
42+
}
43+
}
44+
45+
My2DPoint projectPoint(My3DPoint eye, My3DPoint p) {
46+
float c = eye.z / (eye.z - p.z);
47+
return new My2DPoint(c * (p.x - eye.x), c * (p.y - eye.y));
48+
}
49+
50+
class My2DBox {
51+
My2DPoint[] s;
52+
My2DBox(My2DPoint[] s) {
53+
this.s = s;
54+
}
55+
void render(){
56+
line(s[0].x, s[0].y, s[1].x, s[1].y);
57+
line(s[0].x, s[0].y, s[4].x, s[4].y);
58+
line(s[0].x, s[0].y, s[3].x, s[3].y);
59+
line(s[1].x, s[1].y, s[2].x, s[2].y);
60+
line(s[1].x, s[1].y, s[5].x, s[5].y);
61+
line(s[4].x, s[4].y, s[5].x, s[5].y);
62+
line(s[4].x, s[4].y, s[7].x, s[7].y);
63+
line(s[5].x, s[5].y, s[6].x, s[6].y);
64+
line(s[3].x, s[3].y, s[7].x, s[7].y);
65+
line(s[3].x, s[3].y, s[2].x, s[2].y);
66+
line(s[7].x, s[7].y, s[6].x, s[6].y);
67+
line(s[6].x, s[6].y, s[2].x, s[2].y);
68+
}
69+
}
70+
71+
class My3DBox {
72+
My3DPoint[] p;
73+
My3DBox(My3DPoint origin, float dimX, float dimY, float dimZ){
74+
float x = origin.x;
75+
float y = origin.y;
76+
float z = origin.z;
77+
this.p = new My3DPoint[]{new My3DPoint(x,y+dimY,z+dimZ),
78+
new My3DPoint(x,y,z+dimZ),
79+
new My3DPoint(x+dimX,y,z+dimZ),
80+
new My3DPoint(x+dimX,y+dimY,z+dimZ),
81+
new My3DPoint(x,y+dimY,z),
82+
origin,
83+
new My3DPoint(x+dimX,y,z),
84+
new My3DPoint(x+dimX,y+dimY,z)
85+
};
86+
}
87+
My3DBox(My3DPoint[] p) {
88+
this.p = p;
89+
}
90+
}
91+
92+
My2DBox projectBox (My3DPoint eye, My3DBox box) {
93+
My2DPoint[] tabel = new My2DPoint[8];
94+
for (int i = 0; i < box.p.length; ++i) {
95+
tabel[i] = projectPoint(eye, box.p[i]);
96+
}
97+
return new My2DBox(tabel);
98+
}
99+
100+
float[] homogeneous3DPoint (My3DPoint p) {
101+
float[] result = {p.x, p.y, p.z , 1};
102+
return result;
103+
}
104+
105+
float[][] rotateXMatrix(float angle) {
106+
return(new float[][] {{1, 0 , 0 , 0},
107+
{0, cos(angle), sin(angle) , 0},
108+
{0, -sin(angle) , cos(angle) , 0},
109+
{0, 0 , 0 , 1}});
110+
}
111+
float[][] rotateYMatrix(float angle) {
112+
return(new float[][] {{cos(angle), 0 , sin(angle), 0},
113+
{0, 1, 0 , 0},
114+
{ -sin(angle), 0, cos(angle) , 0},
115+
{0, 0 , 0 , 1}});
116+
}
117+
float[][] rotateZMatrix(float angle) {
118+
return(new float[][] {{cos(angle), -sin(angle), 0, 0},
119+
{sin(angle), cos(angle), 0 , 0},
120+
{0, 0, 1, 0},
121+
{0, 0 , 0 , 1}});
122+
}
123+
float[][] scaleMatrix(float x, float y, float z) {
124+
return(new float[][] {{x, 0, 0, 0},
125+
{0, y, 0 , 0},
126+
{0, 0, z, 0},
127+
{0, 0 , 0 , 1}});
128+
}
129+
float[][] translationMatrix(float x, float y, float z) {
130+
return(new float[][] {{1, 0, 0, x},
131+
{0, 1, 0 , y},
132+
{0, 0, 1, z},
133+
{0, 0 , 0 , 1}});
134+
}
135+
136+
float[] matrixProduct(float[][] a, float[] b) {
137+
float[] result = new float[4];
138+
float sum = 0;
139+
for (int i = 0; i < 4; ++i) {
140+
sum = 0;
141+
for (int j = 0; j < 4; ++j) {
142+
sum += a[i][j] * b[j];
143+
}
144+
result[i] = sum;
145+
}
146+
return result;
147+
}
148+
149+
My3DBox transformBox(My3DBox box, float[][] transformMatrix) {
150+
My3DPoint[] result = new My3DPoint[8];
151+
for (int i = 0; i < box.p.length; ++i) {
152+
result[i] = euclidian3DPoint(matrixProduct(transformMatrix, homogeneous3DPoint(box.p[i])));
153+
}
154+
return new My3DBox(result);
155+
156+
}
157+
158+
My3DPoint euclidian3DPoint (float[] a) {
159+
My3DPoint result = new My3DPoint(a[0]/a[3], a[1]/a[3], a[2]/a[3]);
160+
return result;
161+
}

0 commit comments

Comments
 (0)