-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWall.java
139 lines (121 loc) · 4.16 KB
/
Wall.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
135
136
137
138
139
/**
* A wall is just a line segment that the user cannot walk through
*/
public class Wall implements Renderable{
/**
* The first point of the wall.
* If the wall is vertical, the y value is less than the y value of {@link #end}. (Top of)
* If the wall is horizontal, the x value is less than the x value of {@link #end}. (Left of)
*/
private Point start;
/**
* The second point of the wall.
*/
private Point end;
private final Direction direction;
/**
* Constructor for Wall.
* @param start Start point of the wall.
* @param end End point of the wall.
*/
public Wall(Point start, Point end){
if(start.getX() == end.getX()){
direction = Direction.VERTICAL;
// order points such that start is above the end
if(start.getY() > end.getY()){
this.start = end;
this.end = start;
}else{
this.start = start;
this.end = end;
}
}else if(start.getY() == end.getY()){
direction = Direction.HORIZONTAL;
if(start.getX() > end.getX()){
this.start = end;
this.end = start;
}else{
this.start = start;
this.end = end;
}
}else{
throw new IllegalStateException(String.format("Cannot draw this line.\nStart: %s\nEnd: %s", start, end));
}
}
@Override
public void render(){
Main.getInstance().line(start.getX(), start.getY(), end.getX(), end.getY());
}
@Override
public int hashCode(){
// pretty bad, i should learn how to make a effective one
return (int) start.getX() * 1000 + (int) start.getY() * 100 + (int) end.getX() * 10 + (int) end.getY();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Wall wall = (Wall) o;
return start.equals(wall.start) &&
end.equals(wall.end);
}
@Override
public String toString() {
return "Wall{" +
"start=" + start +
", end=" + end +
'}';
}
public boolean shareCommonEnd(Wall other){
return other.end.equals(end) || other.end.equals(start) ||
other.start.equals(end) || other.start.equals(start);
}
public boolean areDistinct(Wall other){
return !other.end.equals(end) && !other.end.equals(start) &&
!other.start.equals(end) && !other.start.equals(start);
}
/**
* Determines if the point is part of the line segment.
* @param p Point to check if it is part of this wall
* @return True if the point is an endpoint, false otherwise
*/
public boolean isAEndPoint(Point p){
return start.equals(p) || end.equals(p);
}
public Point getStart() {
return start;
}
public Point getEnd() {
return end;
}
public boolean isBetweenTwoPoints(Point a, Point b){
switch(direction){
case VERTICAL:
return start.getX() == a.getX() && start.getX() == b.getX();
case HORIZONTAL:
return start.getY() == a.getY() && start.getY() == b.getY();
}
throw new IllegalStateException("Direction is invalid");
}
public Direction getDirection(){
return this.direction;
}
/**
* Determines if the point can be found on the wall.
* @param p Point to check if it lies on this wall
* @return True if the point is one the wall, false otherwise
*/
public boolean isPointOnWall(Point p){
switch(direction){
case VERTICAL:
return p.getY() >= start.getY() && p.getY() <= end.getY() && Math.abs(p.getX() - this.getStart().getX()) <= .1;
case HORIZONTAL:
return p.getX() >= start.getX() && p.getX() <= end.getX() && Math.abs(p.getY() - this.getStart().getY()) <= .1;
}
throw new IllegalStateException("Direction is invalid");
}
public enum Direction{
HORIZONTAL,
VERTICAL
}
}