-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntersection.java
68 lines (63 loc) · 2.13 KB
/
Intersection.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
import java.util.*;
public class Intersection extends Place{
double x;
double y;
/**
* Returns the x coordinate of the intersection
* @return x the x coordinate
*/
public double x(){
return this.x;
}
/**
* Returns the y coordinate of the intersection
* @return y the y coordinate
*/
public double y(){
return this.y;
}
public boolean addCar(Car car){
if (this.cars.size()==0){
this.cars.add(car);
return true;
}
return false;
}
/**
* Used only once for the creation of the central square which starts the entire grid. Since it's the first part of the grid being added, it will not be connected to anything upon initialization
* @param length the distance across the intersection (to be used in calculating how long it takes to cross the intersection)
* @param x the x coordinate of the intersection
* @param y the y coordinate of the intersection
*/
protected Intersection(double length,double x,double y){
super(length);
this.x=x;
this.y=y;
}
/**
* All intersections except for the first one will be added on to the end of a road. This is the road-based constructor for Intersections.
* @param parent the parent road whose open end the intersection will be connected to
* @param length the distance across the intersection (to be used in calculating how long it takes to cross the intersection)
* @throws ParentCannotAdoptException
*/
public Intersection(Road parent, double length) throws AdoptionException{
super(parent,length);
this.x=parent.x();
this.y=parent.y();
}
/**
* Displays the Intersection as a filled circle drawn around the location
*/
public void display(){
for(Node n: this.links){
n.display();
}
StdDraw.setPenRadius(0.01);
StdDraw.setPenColor(50,150,0);
StdDraw.filledCircle(x, y,10);
StdDraw.setPenColor(0,255,255);
for(Car c: this.cars){
c.display(x,y);
}
}
}