-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTri.java
More file actions
79 lines (65 loc) · 2.16 KB
/
Tri.java
File metadata and controls
79 lines (65 loc) · 2.16 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
import java.util.*;
import java.util.stream.Stream;
import java.awt.*;
import javax.swing.ImageIcon;
public class Tri {
private Poly containingPoly = null;
private Line[] lines;
private Point[] points;
//Correctly initializes using Line and list of points to pull from
public Tri(Line start, ArrayList<Point> pts, ArrayDeque<Line> lstack) throws IndexOutOfBoundsException {
Point pt = start.getNewPoint(pts, lstack);
if (pt == null)
throw new IndexOutOfBoundsException();
Line l2 = new Line(start.getQ(), pt);
l2.extend(!l2.isOnExtendedSide(start.getP()));
Line l3 = new Line(pt, start.getP());
l3.extend(!l3.isOnExtendedSide(start.getQ()));
this.lines = new Line[] {start, l2, l3};
this.points = new Point[] {pt, start.getP(), start.getQ()};
this.addTrisToLines();
}
public Line[] getLines() {
return this.lines;
}
// Adds references to Polys in Lines
public void addTrisToLines() {
for (Line l : this.lines)
l.addTri(this);
}
public void addPoly(Poly p) {
this.containingPoly = p;
}
public Poly getPoly() {
return this.containingPoly;
}
public Point getPoint(int index) {
return this.points[index];
}
public Point[] getPoints() {
return this.points;
}
public boolean isCorner() {
return Stream.of(this.lines).filter(Line::isInBounds).count() == 1;
}
// Gets the point in this Tri that isn't on the line
public Point getThirdPoint(Line l) {
return Stream.of(this.points).filter(p -> !l.hasPoint(p)).findFirst().orElse(null);
}
// Returns the area of this tri
public double area() {
return this.lines[0].areaWith(this.points[0]);
}
//Faster than any iteration, conversion to ArrayList, etc.
public boolean hasLine(Line line) {
return lines[0] == line || lines[1] == line || lines[2] == line;
}
// Generates a hash for the tri for use in HashSets/Maps
public int hashCode() {
int hash = 0;
for (Point p : this.points) {
hash += (int) (512 * p.getX()) << 16 + (int) (512 * p.getY());
}
return hash;
}
}