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