-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoad.java
248 lines (217 loc) · 8.9 KB
/
Road.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
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
import java.util.*;
/** A subset of Place, a "road" Provides a way to describe any path from one point to another, with (within place) a length that can be adjusted to account for curves or other difficulties in traversing the road. By design, a road can have only at most two nodes: a beginning node defined upon creation, and an end node defined upon destruction
*/
public class Road extends Place{
private double xInitial;
private double yInitial;
private double xFinal;
private double yFinal;
public boolean addCar(Car car){
for (Car c: this.cars){
if (car.hasCollission(c,length())){
return false;
}
}
this.cars.add(car);
return true;
}
public double[] coors(){
double[] out={xInitial,yInitial,xFinal,yFinal};
return out;
}
public boolean isParentalSide(Node n){
if (this==n.target()){
return true;
}
return false;
}
public Node getNode(boolean orientation){
if (orientation){
return this.links.get(0);
}
else{
return this.links.get(1);
}
}
/**
* Returns the X coordinate xFinal: the open part of a road.
* @return xFinal
*/
public double x(){
return this.xFinal;
}
/**
* Returns the Y coordinate yFinal: the open part of a road.
* @return yFinal
*/
public double y(){
return this.yFinal;
}
/**
* Returns the X coordinate xInitial: the x coordinate of the side of the road that connects the road to its parent.
* @return xInitial
*/
public double xInitial(){
return this.xInitial;
}
/**
* Returns the Y coordinate yInitial: the y coordinate the side of the road that connects the road to its parent.
* @return yInitial
*/
public double yInitial(){
return this.yInitial;
}
/**
* Creates a road from a parent place to a new end point
* @param parent the place whose endpoint defines one end of the new road
* @param xFinal the x coordinate of the other end
* @param yFinal the y coordinate of the other end
* @param length the amount of time it takes to traverse the road
* @throws ParentCannotAdoptException
*/
public Road(Place parent,double xFinal,double yFinal,double length) throws AdoptionException{
super(parent,length);
this.xInitial=parent.x();
this.yInitial=parent.y();
this.xFinal=xFinal;
this.yFinal=yFinal;
}
/**
* Creates a road from a parent place to a new end point
* @param parent the place whose endpoint defines one end of the new road
* @param xFinal the x coordinate of the other end
* @param yFinal the y coordinate of the other end
* @throws ParentCannotAdoptException
*/
public Road(Place parent,double xFinal,double yFinal) throws AdoptionException{
super(parent,Math.pow(Math.pow((xFinal-parent.x()),2)+Math.pow((yFinal-parent.y()),2),0.5));
this.xInitial=parent.x();
this.yInitial=parent.y();
this.xFinal=xFinal;
this.yFinal=yFinal;
}
/**
* Displays the road as a line drawn from the initial point to the final point
*/
public void display(){
for(Node n: links){
n.display();
}
StdDraw.setPenRadius(0.01);
StdDraw.setPenColor(0,0,0);
StdDraw.setPenColor(120,0,0);
StdDraw.line(xInitial,yInitial,xFinal,yFinal);
StdDraw.setPenColor(0,255,255);
for(Car c: this.cars){
c.display(xInitial,yInitial,xFinal,yFinal);
}
}
/**
* Creates a road from a parent place to an existing place
* @param parent the place whose endpoint defines one end of the new road
* @param endPoint the place to which the end of the new road connects
* @param length the amount of time it takes to traverse the road
* @throws ParentCannotAdoptException
*/
public Road(Place parent,Place endPoint,double length) throws AdoptionException{
super(parent,endPoint,length);
this.xInitial=parent.x();
this.yInitial=parent.y();
this.xFinal=endPoint.x();
this.yFinal=endPoint.y();
}
/**
* Creates a road from a parent place to an existing place
* @param parent the place whose endpoint defines one end of the new road
* @param endPoint the place to which the end of the new road connects
* @throws ParentCannotAdoptException
*/
public Road(Place parent,Place endPoint) throws AdoptionException{
super(parent,endPoint,Math.pow(Math.pow((endPoint.x()-parent.x()),2)+Math.pow((endPoint.y()-parent.y()),2),0.5));
//System.out.println(Math.pow(Math.pow((endPoint.x()-parent.x()),2)+Math.pow((endPoint.y()-parent.y()),2),0.5)+" Road 140");
this.xInitial=parent.x();
this.yInitial=parent.y();
this.xFinal=endPoint.x();
this.yFinal=endPoint.y();
}
/**
* Creates a road from a parent Intersection to an existing Intersection
* @param parent the place whose endpoint defines one end of the new road
* @param endPoint the place to which the end of the new road connects
* @throws ParentCannotAdoptException
*/
public Road(Intersection parent,Intersection endPoint) throws AdoptionException{
super((Place)parent,(Place)endPoint,Math.pow(Math.pow((endPoint.x()-parent.x()),2)+Math.pow((endPoint.y()-parent.y()),2),0.5));
//System.out.println(Math.pow(Math.pow((endPoint.x()-parent.x()),2)+Math.pow((endPoint.y()-parent.y()),2),0.5)+" Road 140");
this.xInitial=parent.x();
this.yInitial=parent.y();
this.xFinal=endPoint.x();
this.yFinal=endPoint.y();
}
/**
* Creates a road from a parent road to a new end point
* @param parent the road whose endpoint defines one end of the new road
* @param xFinal the x coordinate of the other end
* @param yFinal the y coordinate of the other end
* @param length the amount of time it takes to traverse the road
* @throws ParentCannotAdoptException
* @deprecated replaced by {@link #Road(Place parent,double xFinal,double yFinal,double length)}
*/
@Deprecated
public Road(Road parent,double xFinal,double yFinal,double length) throws AdoptionException{
super(parent,length);
this.xInitial=parent.x();
this.yInitial=parent.y();
this.xFinal=xFinal;
this.yFinal=yFinal;
}
/**
* Creates a road from a parent road to an existing intersection
* @param parent the road whose endpoint defines one end of the new road
* @param endPoint the intersection to which the end of the new road connects
* @param length the amount of time it takes to traverse the road
* @throws ParentCannotAdoptException
* @deprecated replaced by {@link #Road(Place parent,Intersection endPoint,double length)}
*/
@Deprecated
public Road(Road parent,Intersection endPoint,double length) throws AdoptionException{
super(parent,endPoint,length);
this.xInitial=parent.x();
this.yInitial=parent.y();
this.xFinal=endPoint.x();
this.yFinal=endPoint.y();
}
/**
* Creates a road from an intersection to a new end point
* @param startPoint the starting intersection that defines the initial end of the new road
* @param xFinal the x coordinate of the other end
* @param yFinal the y coordinate of the other end
* @param length the amount of time it takes to traverse the road
* @throws ParentCannotAdoptException
* @deprecated replaced by {@link #Road(Place parent,double xFinal,double yFinal,double length)}
*/
@Deprecated
public Road(Intersection startPoint,double xFinal,double yFinal,double length) throws AdoptionException{
super(startPoint,length);
this.xInitial=startPoint.x();
this.yInitial=startPoint.y();
this.xFinal=xFinal;
this.yFinal=yFinal;
}
/**
* Creates a road from an intersection to an existing intersection
* @param startPoint the starting intersection that defines the initial end of the new road
* @param endPoint the intersection to which the end of the new road connects
* @param length the amount of time it takes to traverse the road
* @throws ParentCannotAdoptException
* @deprecated replaced by {@link #Road(Place parent,Intersection endPoint,double length)}
*/
@Deprecated
public Road(Intersection startPoint,Intersection endPoint,double length) throws AdoptionException{
super(startPoint,endPoint,length);
this.xInitial=startPoint.x();
this.yInitial=startPoint.y();
this.xFinal=endPoint.x();
this.yFinal=endPoint.y();
}
}