-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangle.java
More file actions
237 lines (192 loc) · 6.14 KB
/
Triangle.java
File metadata and controls
237 lines (192 loc) · 6.14 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
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
class Triangle implements Comparable<Triangle> {
public Color color;
Point[] points = new Point[3];
Pixel[] pixels;
public Triangle(Point p1, Point p2, Point p3, ArrayList<Pixel> data) {
pixels = new Pixel[data.size()];
points[0] = new Point(p1.GetExX(), p1.GetExY(), p1.GetExZ());
points[1] = new Point(p2.GetExX(), p2.GetExY(), p2.GetExZ());
points[2] = new Point(p3.GetExX(), p3.GetExY(), p3.GetExZ());
for (int i = 0; i < data.size(); i++)
pixels[i] = Pixel.Copy(data.get(i));
}
public Triangle(Point p1, Point p2, Point p3, Color c) {
pixels = null;
points[0] = new Point(p1.GetExX(), p1.GetExY(), p1.GetExZ());
points[1] = new Point(p2.GetExX(), p2.GetExY(), p2.GetExZ());
points[2] = new Point(p3.GetExX(), p3.GetExY(), p3.GetExZ());
color = c;
}
public Triangle(Triangle triangle) {
for (int i = 0; i < 3; i++) {
this.points[i] = new Point(triangle.points[i].GetExX(), triangle.points[i].GetExY(),
triangle.points[i].GetExZ());
}
if (triangle.pixels != null) {
for (int i = 0; i < triangle.pixels.length; i++) {
this.pixels[i] = Pixel.Copy(triangle.pixels[i]);
}
} else {
color = triangle.color;
pixels = null;
}
}
public void Print_Info() {
for (Point p : points) {
System.out.println(p.GetExX() + " " + p.GetExY() + " " + p.GetExZ());
}
}
public boolean Should_Be_Drawn() {
Point p = Util.GetNormalVector(points[0], points[1], points[2]);
double dotproduct = Util.GetDotProduct(p, World.camera.GetDirection());
boolean draw = dotproduct < 0 ? true : false;
return draw;
}
public void RotateCounterClockwiseAboutYAxis(Point p, float degrees) {
for (Point p1 : points)
p1.RotateCounterClockwiseAboutYAxis(p, degrees);
if (pixels != null)
for (Pixel p2 : pixels)
p2.RotateCounterClockwiseAboutYAxis(p, degrees);
}
public void RotateClockwiseAboutYAxis(Point p, float degrees) {
for (Point p1 : points)
p1.RotateClockwiseAboutYAxis(p, degrees);
if (pixels != null)
for (Pixel p2 : pixels)
p2.RotateClockwiseAboutYAxis(p, degrees);
}
public void RotateCounterClockwiseAboutXAxis(Point p, float degrees) {
for (Point p1 : points)
p1.RotateCounterClockwiseAboutXAxis(p, degrees);
if (pixels != null)
for (Pixel p2 : pixels)
p2.RotateCounterClockwiseAboutXAxis(p, degrees);
}
public void RotateClockwiseAboutXAxis(Point p, float degrees) {
for (Point p1 : points)
p1.RotateClockwiseAboutXAxis(p, degrees);
if (pixels != null)
for (Pixel p2 : pixels)
p2.RotateClockwiseAboutXAxis(p, degrees);
}
public void RotateCounterClockwiseAboutZAxis(Point p, float degrees) {
for (Point p1 : points)
p1.RotateCounterClockwiseAboutZAxis(p, degrees);
if (pixels != null)
for (Pixel p2 : pixels)
p2.RotateCounterClockwiseAboutZAxis(p, degrees);
}
public void RotateClockwiseAboutZAxis(Point p, float degrees) {
for (Point p1 : points)
p1.RotateClockwiseAboutZAxis(p, degrees);
if (pixels != null)
for (Pixel p2 : pixels)
p2.RotateClockwiseAboutZAxis(p, degrees);
}
public void Resize(float scaleFactor) {
for (Point p : points) {
p.SetX(p.GetExX() * scaleFactor);
p.SetY(p.GetExY() * scaleFactor);
p.SetZ(p.GetExZ() * scaleFactor);
}
}
public Point GetCenter() {
double x = (points[0].GetExX() + points[1].GetExX() + points[2].GetExX()) / 3.0;
double y = (points[0].GetExY() + points[1].GetExY() + points[2].GetExY()) / 3.0;
double z = (points[0].GetExZ() + points[1].GetExZ() + points[2].GetExZ()) / 3.0;
return new Point(x, y, z);
}
public void Render(BufferedImage image) {
if (Should_Be_Drawn()) {
if (pixels != null) {
for (int i = 0; i < pixels.length; i++) {
Point p = World.camera.LookAt(pixels[i]);
if (p != null) {
if (p.GetX() > -640 && p.GetX() < 640 && p.GetY() > -500 && p.GetY() < 500) {
if (World.distancefromscreen[(int) Math.round(p.GetExX())
+ 640][(int) Math.round(p.GetExY()) + 500] > p.GetExZ()) {
image.setRGB((int) Math.round(p.GetExX()) + 640, (int) Math.round(p.GetExY()) + 500,
pixels[i].GetColor());
World.distancefromscreen[(int) Math.round(p.GetExX())
+ 640][(int) Math.round(p.GetExY()) + 500] = p.GetExZ();
}
}
}
}
} else {
Graphics2D g = image.createGraphics();
g.setColor(color);
int[] x = new int[3];
int[] y = new int[3];
Triangle t = World.camera.LookAt(this);
if (t != null && t.GetCenter().GetExZ() >= World.camera.GetLocation().GetExZ()) {
for (int i = 0; i < 3; i++) {
x[i] = t.points[i].GetX() + 640;
y[i] = t.points[i].GetY() + 500;
}
Polygon poly = new Polygon(x, y, 3);
g.fillPolygon(poly);
}
}
}
}
public void Draw_Mesh(BufferedImage buf) {
if (Should_Be_Drawn()) {
Graphics2D g = buf.createGraphics();
g.setColor(color);
Triangle t = World.camera.LookAt(this);
if (t != null) {
g.drawLine(t.points[0].GetX() + 640, t.points[0].GetY() + 500, t.points[1].GetX() + 640,
t.points[1].GetY() + 500);
g.drawLine(t.points[1].GetX() + 640, t.points[1].GetY() + 500, t.points[2].GetX() + 640,
t.points[2].GetY() + 500);
g.drawLine(t.points[0].GetX() + 640, t.points[0].GetY() + 500, t.points[2].GetX() + 640,
t.points[2].GetY() + 500);
}
}
}
public int GreatestX() {
int gx = -10000;
for (Point p : points)
if (p.GetExX() > gx)
gx = p.GetX();
return gx;
}
public int LeastX() {
int lx = 10000;
for (Point p : points)
if (p.GetExX() < lx)
lx = p.GetX();
return lx;
}
public int GreatestY() {
int gy = -10000;
for (Point p : points)
if (p.GetExY() > gy)
gy = p.GetY();
return gy;
}
public int LeastY() {
int ly = 10000;
for (Point p : points)
if (p.GetExY() < ly)
ly = p.GetY();
return ly;
}
@Override
public int compareTo(Triangle t) {
if (this.GetCenter().GetExZ() <= t.GetCenter().GetExZ()) {
return 1;
} else if (this.GetCenter().GetExZ() > t.GetCenter().GetExZ()) {
return -1;
} else {
return 0;
}
}
}