-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCube.java
More file actions
279 lines (242 loc) · 10.1 KB
/
Cube.java
File metadata and controls
279 lines (242 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
class Cube {
Point[] points = new Point[8];
Square[] squares = new Square[6];
BufferedImage img;
Pixel[][][] pixels;
Color color;
public Cube() {
}
public Cube(Point center, int radius, Color c) {
color = c;
pixels = null;
for (int i = 0; i < 8; i++)
points[i] = new Point(0, 0, 0);
points[0].SetX(center.GetExX() - radius);
points[0].SetY(center.GetExY() + radius);
points[0].SetZ(center.GetExZ() - radius);
points[1].SetX(center.GetExX() + radius);
points[1].SetY(center.GetExY() + radius);
points[1].SetZ(center.GetExZ() - radius);
points[2].SetX(center.GetExX() - radius);
points[2].SetY(center.GetExY() - radius);
points[2].SetZ(center.GetExZ() - radius);
points[3].SetX(center.GetExX() + radius);
points[3].SetY(center.GetExY() - radius);
points[3].SetZ(center.GetExZ() - radius);
points[4].SetX(center.GetExX() - radius);
points[4].SetY(center.GetExY() + radius);
points[4].SetZ(center.GetExZ() + radius);
points[5].SetX(center.GetExX() + radius);
points[5].SetY(center.GetExY() + radius);
points[5].SetZ(center.GetExZ() + radius);
points[6].SetX(center.GetExX() - radius);
points[6].SetY(center.GetExY() - radius);
points[6].SetZ(center.GetExZ() + radius);
points[7].SetX(center.GetExX() + radius);
points[7].SetY(center.GetExY() - radius);
points[7].SetZ(center.GetExZ() + radius);
squares[0] = new Square(points[0], points[1], points[3], points[2], c);
squares[1] = new Square(points[5], points[4], points[6], points[7], c);
squares[2] = new Square(points[4], points[5], points[1], points[0], c);
squares[3] = new Square(points[2], points[3], points[7], points[6], c);
squares[4] = new Square(points[0], points[2], points[6], points[4], c);
squares[5] = new Square(points[1], points[5], points[7], points[3], c);
}
public Cube(Point center, int radius, String texture) {
for (int i = 0; i < 8; i++)
points[i] = new Point(0, 0, 0);
points[0].SetX(center.GetExX() - radius);
points[0].SetY(center.GetExY() + radius);
points[0].SetZ(center.GetExZ() - radius);
points[1].SetX(center.GetExX() + radius);
points[1].SetY(center.GetExY() + radius);
points[1].SetZ(center.GetExZ() - radius);
points[2].SetX(center.GetExX() - radius);
points[2].SetY(center.GetExY() - radius);
points[2].SetZ(center.GetExZ() - radius);
points[3].SetX(center.GetExX() + radius);
points[3].SetY(center.GetExY() - radius);
points[3].SetZ(center.GetExZ() - radius);
points[4].SetX(center.GetExX() - radius);
points[4].SetY(center.GetExY() + radius);
points[4].SetZ(center.GetExZ() + radius);
points[5].SetX(center.GetExX() + radius);
points[5].SetY(center.GetExY() + radius);
points[5].SetZ(center.GetExZ() + radius);
points[6].SetX(center.GetExX() - radius);
points[6].SetY(center.GetExY() - radius);
points[6].SetZ(center.GetExZ() + radius);
points[7].SetX(center.GetExX() + radius);
points[7].SetY(center.GetExY() - radius);
points[7].SetZ(center.GetExZ() + radius);
/*
* triangles[0] = new Triangle(points[0], points[1], points[2]); triangles[1] =
* new Triangle(points[1], points[3], points[2]); triangles[2] = new
* Triangle(points[5], points[4], points[7]); triangles[3] = new
* Triangle(points[4], points[6], points[7]); triangles[4] = new
* Triangle(points[4], points[5], points[0]); triangles[5] = new
* Triangle(points[5], points[1], points[0]); triangles[6] = new
* Triangle(points[2], points[3], points[6]); triangles[7] = new
* Triangle(points[3], points[7], points[6]); triangles[8] = new
* Triangle(points[4], points[0], points[6]); triangles[9] = new
* Triangle(points[0], points[2], points[6]); triangles[10] = new
* Triangle(points[1], points[5], points[3]); triangles[11] = new
* Triangle(points[5], points[7], points[3]);
*/
try {
img = ImageIO.read(new File(texture));
} catch (IOException ex) {
}
pixels = new Pixel[6][img.getWidth() + 1][img.getHeight() + 1];
double unitsperpixel = (double) ((radius * 2) / (double) (img.getWidth()));
// Front
for (int i = 0; i < img.getWidth() + 1; i++) {
for (int j = 0; j < img.getHeight() + 1; j++) {
pixels[0][i][j] = new Pixel(((unitsperpixel * (i - (img.getWidth() / 2))) + center.GetExX()),
((unitsperpixel * (j - (img.getHeight() / 2))) + center.GetExY()), center.GetExZ() - radius,
new Color(img.getRGB((i == img.getWidth() ? i - 1 : i), (j == img.getWidth() ? j - 1 : j))));
}
}
// Back
for (int i = 0; i < img.getWidth() + 1; i++) {
for (int j = 0; j < img.getHeight() + 1; j++) {
pixels[1][i][j] = new Pixel(((unitsperpixel * (i - (img.getWidth() / 2))) + center.GetExX()),
((unitsperpixel * (j - (img.getHeight() / 2))) + center.GetExY()), center.GetExZ() + radius,
new Color(img.getRGB((i == img.getWidth() ? i - 1 : i), (j == img.getWidth() ? j - 1 : j))));
}
}
// Top
for (int i = 0; i < img.getWidth() + 1; i++) {
for (int j = 0; j < img.getHeight() + 1; j++) {
pixels[2][i][j] = new Pixel(((unitsperpixel * (i - (img.getWidth() / 2))) + center.GetExX()),
center.GetExY() + radius, ((unitsperpixel * (j - (img.getHeight() / 2))) + center.GetExZ()),
new Color(img.getRGB((i == img.getWidth() ? i - 1 : i), (j == img.getWidth() ? j - 1 : j))));
}
}
// Bottom
for (int i = 0; i < img.getWidth() + 1; i++) {
for (int j = 0; j < img.getHeight() + 1; j++) {
pixels[3][i][j] = new Pixel(((unitsperpixel * (i - (img.getWidth() / 2))) + center.GetExX()),
center.GetExY() - radius, ((unitsperpixel * (j - (img.getHeight() / 2))) + center.GetExZ()),
new Color(img.getRGB((i == img.getWidth() ? i - 1 : i), (j == img.getWidth() ? j - 1 : j))));
}
}
// Left
for (int i = 0; i < img.getWidth() + 1; i++) {
for (int j = 0; j < img.getHeight() + 1; j++) {
pixels[4][i][j] = new Pixel(center.GetExX() - radius,
((unitsperpixel * (j - (img.getWidth() / 2))) + center.GetExY()),
((unitsperpixel * (i - (img.getHeight() / 2))) + center.GetExZ()),
new Color(img.getRGB((i == img.getWidth() ? i - 1 : i), (j == img.getWidth() ? j - 1 : j))));
}
}
// Right
for (int i = 0; i < img.getWidth() + 1; i++) {
for (int j = 0; j < img.getHeight() + 1; j++) {
pixels[5][i][j] = new Pixel(center.GetExX() + radius,
((unitsperpixel * (j - (img.getWidth() / 2))) + center.GetExY()),
((unitsperpixel * (i - (img.getHeight() / 2))) + center.GetExZ()),
new Color(img.getRGB((i == img.getWidth() ? i - 1 : i), (j == img.getWidth() ? j - 1 : j))));
}
}
int inc = img.getWidth();
squares[0] = new Square(points[0], points[1], points[3], points[2], pixels[0], inc);
squares[1] = new Square(points[5], points[4], points[6], points[7], pixels[1], inc);
squares[2] = new Square(points[4], points[5], points[1], points[0], pixels[2], inc);
squares[3] = new Square(points[2], points[3], points[7], points[6], pixels[3], inc);
squares[4] = new Square(points[0], points[2], points[6], points[4], pixels[4], inc);
squares[5] = new Square(points[1], points[5], points[7], points[3], pixels[5], inc);
}
public void RotateCounterClockwiseAboutYAxis(Point p, float degrees) {
for (Square t : squares)
t.RotateCounterClockwiseAboutYAxis(p, degrees);
}
public void RotateClockwiseAboutYAxis(Point p, float degrees) {
for (Square t : squares)
t.RotateClockwiseAboutYAxis(p, degrees);
}
public void RotateCounterClockwiseAboutXAxis(Point p, float degrees) {
for (Square t : squares)
t.RotateCounterClockwiseAboutXAxis(p, degrees);
}
public void RotateClockwiseAboutXAxis(Point p, float degrees) {
for (Square t : squares)
t.RotateClockwiseAboutXAxis(p, degrees);
}
public void RotateCounterClockwiseAboutZAxis(Point p, float degrees) {
for (Square t : squares)
t.RotateCounterClockwiseAboutZAxis(p, degrees);
}
public void RotateClockwiseAboutZAxis(Point p, float degrees) {
for (Square t : squares)
t.RotateClockwiseAboutZAxis(p, degrees);
}
public void Resize(float scaleFactor) {
for (Square t : squares)
t.Resize(scaleFactor);
}
public Point GetCenter() {
double totx = 0;
double toty = 0;
double totz = 0;
for (Square t : squares) {
totx += t.points[0].GetExX();
totx += t.points[1].GetExX();
totx += t.points[2].GetExX();
totx += t.points[3].GetExX();
toty += t.points[0].GetExY();
toty += t.points[1].GetExY();
toty += t.points[2].GetExY();
toty += t.points[3].GetExY();
totz += t.points[0].GetExZ();
totz += t.points[1].GetExZ();
totz += t.points[2].GetExZ();
totz += t.points[3].GetExZ();
}
totx /= 24.0d;
toty /= 24.0d;
totz /= 24.0d;
return new Point(totx, toty, totz);
}
public void Render(BufferedImage buf) {
for (Square t : squares) {
t.Render(buf);
}
}
public void Draw_Mesh(BufferedImage buf) {
for (Square t : squares) {
t.Draw_Mesh(buf);
}
}
public void Move(Point target, float distance) {
Point center = GetCenter();
Point orig = GetCenter();
Util.MovePointTowardsAnotherPoint(center, target, distance);
for (Square t : squares) {
t.points[0].SetX(t.points[0].GetExX() + (center.GetExX() - orig.GetExX()));
t.points[0].SetY(t.points[0].GetExY() + (center.GetExY() - orig.GetExY()));
t.points[0].SetZ(t.points[0].GetExZ() + (center.GetExZ() - orig.GetExZ()));
t.points[1].SetX(t.points[1].GetExX() + (center.GetExX() - orig.GetExX()));
t.points[1].SetY(t.points[1].GetExY() + (center.GetExY() - orig.GetExY()));
t.points[1].SetZ(t.points[1].GetExZ() + (center.GetExZ() - orig.GetExZ()));
t.points[2].SetX(t.points[2].GetExX() + (center.GetExX() - orig.GetExX()));
t.points[2].SetY(t.points[2].GetExY() + (center.GetExY() - orig.GetExY()));
t.points[2].SetZ(t.points[2].GetExZ() + (center.GetExZ() - orig.GetExZ()));
t.points[3].SetX(t.points[3].GetExX() + (center.GetExX() - orig.GetExX()));
t.points[3].SetY(t.points[3].GetExY() + (center.GetExY() - orig.GetExY()));
t.points[3].SetZ(t.points[3].GetExZ() + (center.GetExZ() - orig.GetExZ()));
}
}
public void SetTexture(String file) {
try {
img = ImageIO.read(new File(file));
} catch (IOException ex) {
}
}
}