-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyPolygon.java
134 lines (116 loc) · 3.42 KB
/
MyPolygon.java
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
import java.awt.Point;
import java.awt.Color;
import java.awt.Polygon;
import java.awt.Graphics;
import java.util.Collections;
import java.util.Comparator;
import java.util.ArrayList;
public class MyPolygon {
private Color color;
private Point3D[] points;
public MyPolygon(Point3D... points) {
this.points = new Point3D[points.length];
for (int i = 0; i < points.length; i++) {
Point3D a = points[i];
this.points[i] = new Point3D(a.x, a.y, a.z);
}
}
public MyPolygon(Color c, Point3D... points) {
this.color = c;
this.points = new Point3D[points.length];
for (int i = 0; i < points.length; i++) {
Point3D a = points[i];
this.points[i] = new Point3D(a.x, a.y, a.z);
}
}
public void rotate(boolean CW, Point3D axis, double xDegrees, double yDegrees, double zDegrees) {
for (Point3D p : this.points) {
PointConverter.rotateAxisX(p, axis, CW, xDegrees);
PointConverter.rotateAxisY(p, axis, CW, yDegrees);
PointConverter.rotateAxisZ(p, axis, CW, zDegrees);
}
}
public void move(double x, double y, double z) {
for (Point3D p : this.points) {
PointConverter.move(p, x, y, z);
}
}
public void render(Graphics g, Point3D center) {
Polygon polygon = new Polygon();
for (int i = 0; i < points.length; i++) {
Point p1 = PointConverter.convertPoint(points[i]);
polygon.addPoint(p1.x, p1.y);
}
double x = 0;
double y = 0;
double z = 0;
for (Point3D p : points) {
x += p.x;
y += p.y;
z += p.z;
}
x/=points.length;
y/=points.length;
z/=points.length;
x -= center.x;
y -= center.y;
z -= center.z;
//System.out.println("\n" + x + ", " + y + ", " + z );
double dotProd = x/2 + z;
double length = Math.sqrt(x * x + y * y + z * z) * 1.12;
double theta = Math.acos(dotProd / length);
//System.out.println(dotProd / length);
//double f = theta/3.14 * (theta>0?1:-1);
double f = Math.cos(theta);
f = f*0.5 + 0.7;
f *= (f>0?1:-1);
//System.out.println(f);
int r = (int)(color.getRed() * f);
if (r > 255 || r < 0) r = color.getRed();
int g1 = (int)( color.getGreen() * f);
if (g1 > 255 || g1 < 0) g1 = color.getGreen();
int b = (int)(color.getBlue() * f);
if (b > 255 || b < 0) b = color.getBlue();
Color c = new Color(r,g1,b);
g.setColor(c);
g.fillPolygon(polygon);
}
public void render(Graphics g) {
Polygon polygon = new Polygon();
for (int i = 0; i < points.length; i++) {
Point p1 = PointConverter.convertPoint(points[i]);
polygon.addPoint(p1.x, p1.y);
}
g.setColor(color);
g.fillPolygon(polygon);
}
public void setColor(Color c) {
this.color = c;
}
public double getAverageX() {
double sum = 0;
for (Point3D p : this.points) {
sum += p.x;
}
return sum/this.points.length;
}
public static MyPolygon[] sortPolygons(MyPolygon[] polygons) {
ArrayList<MyPolygon> polygonsList = new ArrayList<MyPolygon>();
for (MyPolygon p : polygons) {
polygonsList.add(p);
}
Collections.sort(polygonsList, new Comparator<MyPolygon>() {
public int compare(MyPolygon p1, MyPolygon p2) {
double p1AverageX = p1.getAverageX();
double p2AverageX = p2.getAverageX();
double diff = p2AverageX - p1AverageX;
if (diff == 0) return 0;
return diff < 0 ? 1 : -1;
}
});
for (int i = 0; i < polygons.length; i++) {
polygons[i] = polygonsList.get(i);
}
return polygons;
}
}