-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCar.java
279 lines (256 loc) · 9.79 KB
/
Car.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import java.util.*;
import java.io.*;
public class Car{
double maxSpeed;
double distFromParent;
boolean orientation;//true if directed away from parent
Node awayFrom;
Place loc;
double size=8;
ArrayList<Node>route;
Node currentDest;
boolean isTurning;
public Car(double speed, Road r, double distFromParent,boolean orientation) throws NoMoreSpaceException{
if (distFromParent<0){
throw new NoMoreSpaceException();
}
loc=(Place)(r);
this.maxSpeed=speed;
this.orientation=orientation;
this.awayFrom=r.getNode(orientation);
this.distFromParent=distFromParent;
if(r.addCar(this)){
System.out.println("car added");
}
else{
throw new NoMoreSpaceException();
}
}
public boolean move(){
//System.out.println("pre: "+distFromParent);
double p=distFromParent;
if (orientation){
p+=this.maxSpeed;
if(p>loc.length()){
if(route.indexOf(currentDest)==route.size()-1){
return false;
}
this.loc.removeCar(this);
//System.out.println("here: "+currentDest.getOther(this.loc));
this.loc=(Place)(currentDest.getOther(this.loc));
//System.out.println(this.loc);
//System.out.println(this.loc.length());
this.loc.addCar(this);
awayFrom=currentDest;
//System.out.println(route.indexOf(currentDest));
currentDest=route.get(route.indexOf(currentDest)+1);
//System.out.println("node val: "+ currentDest.nodeValue());
if(loc instanceof Intersection){
orientation=true;
//p=p-loc.length();
p=0;
}
else if(loc instanceof Road){
if(((Road)this.loc).isParentalSide(awayFrom)){
orientation=true;
//p=p-loc.length();
p=0;
}
else{
orientation=false;
//p=this.loc.length()-(p-this.loc.length());
p=loc.length();
}
}
}
}
else{
p-=this.maxSpeed;
if(p<0){
if(route.indexOf(currentDest)==route.size()-1){
return false;
}
this.loc.removeCar(this);
this.loc=(Place)(currentDest.getOther(this.loc));
this.loc.addCar(this);
awayFrom=currentDest;
currentDest=route.get(route.indexOf(currentDest)+1);
//System.out.println("node val: "+currentDest.nodeValue());
if(loc instanceof Intersection){
orientation=true;
p=-p;
}
else if(loc instanceof Road){
if(((Road)this.loc).isParentalSide(awayFrom)){
orientation=true;
p=-p;
}
else{
orientation=false;
p=this.loc.length()+p;
}
}
}
}
distFromParent=p;
return true;
//System.out.println("after: "+distFromParent);
}
//returns false if it fails
public boolean setDestination(Node n){
this.inputNodeValue();
route=n.getPath();
//System.out.println("start");
for(int i=0; i<route.size();i++){
//System.out.print(route.get(i).nodeValue()+" ");
}
//System.out.println("end");
if(route.size()!=0){
currentDest=route.get(0);
for(int i=0; i<route.size();i++){
}
return true;
}
return false;
}
public void inputNodeValue(){
ArrayList<Node>myPath=new ArrayList<Node>();
double length;
if (orientation){length=distFromParent;}
else{length=loc.length()-distFromParent;}
this.loc.inputNodeValue(-length,awayFrom,myPath,this.maxSpeed);
}
public boolean hasCollission(Car c, double roadLength){
if (this.distFromParent>c.distFromParent()){
if (c.Orientation()==true&&this.orientation==false){
return true;//we are bound to have a collision since c is moving away from the parent but "this" is blocking the way
}
if( ( (this.size+c.Size()) /2 ) > (this.distFromParent-c.distFromParent() ) ){
return true;
}
}
else{
if (c.Orientation()==false&&this.orientation==true){
return true;//we are bound to have a collision since this is moving away from the parent but c is blocking the way
}
if( ( (this.size+c.Size()) /2 ) > (c.distFromParent()-this.distFromParent ) ){
return true;
}
}
return false;
}
public void display(double x,double y){
Road previousRoad=((Road)this.awayFrom.getOther(this.loc));
double[] arr1=previousRoad.coors();
unitVector previous;
if (previousRoad.isParentalSide(awayFrom)){
previous=new unitVector(arr1[0]-arr1[2],arr1[1]-arr1[3]);
}
else{
previous=new unitVector(arr1[2]-arr1[0],arr1[3]-arr1[1]);
}
Road nextRoad=((Road)this.currentDest.getOther(this.loc));
double[] arr2=nextRoad.coors();
unitVector next;
if (!nextRoad.isParentalSide(currentDest)){
next=new unitVector(arr2[0]-arr2[2],arr2[1]-arr2[3]);
}
else{
next=new unitVector(arr2[2]-arr2[0],arr2[3]-arr2[1]);
}
unitVector forward=new unitVector(this.distFromParent*next.x()+(this.loc.length()-this.distFromParent)*previous.x(),this.distFromParent*next.y()+(this.loc.length()-this.distFromParent)*previous.y());
unitVector backward=forward.opp();
unitVector perp=forward.perp();
double width =5;
double arrowWidth=12;
double backl=2.6;
double forwardl=1.5;
double arrowTip=5;
double[] a = {x+backl*size*backward.x()+width*perp.x(),
x+backl*size*backward.x()-width*perp.x(),
x+forwardl*size*forward.x()-width*perp.x(),
x+forwardl*size*forward.x()-arrowWidth*perp.x(),
x+arrowTip*size*forward.x(),
x+forwardl*size*forward.x()+arrowWidth*perp.x(),
x+forwardl*size*forward.x()+width*perp.x()
};
double[] b = {y+backl*size*backward.y()+width*perp.y(),
y+backl*size*backward.y()-width*perp.y(),
y+forwardl*size*forward.y()-width*perp.y(),
y+forwardl*size*forward.y()-arrowWidth*perp.y(),
y+arrowTip*size*forward.y(),
y+forwardl*size*forward.y()+arrowWidth*perp.y(),
y+forwardl*size*forward.y()+width*perp.y()
};
StdDraw.filledPolygon(a, b);
}
public void display(double xi, double yi, double xf,double yf){
double x=xi+(xf-xi)*this.distFromParent/this.loc.length();
double y=yi+(yf-yi)*this.distFromParent/this.loc.length();
//StdDraw.filledEllipse(x,y, 3*size,3*size);
unitVector forward;
if (this.orientation){
forward=new unitVector(xf-xi, yf-yi);
}
else{
forward=new unitVector(xi-xf, yi-yf);
}
unitVector backward=forward.opp();
unitVector perp=forward.perp();
double width =5;
double arrowWidth=12;
double backl=2.6;
double forwardl=1.5;
double arrowTip=5;
double[] a = {x+backl*size*backward.x()+width*perp.x(),
x+backl*size*backward.x()-width*perp.x(),
x+forwardl*size*forward.x()-width*perp.x(),
x+forwardl*size*forward.x()-arrowWidth*perp.x(),
x+arrowTip*size*forward.x(),
x+forwardl*size*forward.x()+arrowWidth*perp.x(),
x+forwardl*size*forward.x()+width*perp.x()
};
double[] b = {y+backl*size*backward.y()+width*perp.y(),
y+backl*size*backward.y()-width*perp.y(),
y+forwardl*size*forward.y()-width*perp.y(),
y+forwardl*size*forward.y()-arrowWidth*perp.y(),
y+arrowTip*size*forward.y(),
y+forwardl*size*forward.y()+arrowWidth*perp.y(),
y+forwardl*size*forward.y()+width*perp.y()
};
StdDraw.filledPolygon(a, b);
}
public double Size(){
return this.size;
}
public boolean Orientation(){
return this.orientation;
}
public double distFromParent(){
return this.distFromParent;
}
public class unitVector{
private double x;
private double y;
public unitVector(double dx, double dy){
double c=Math.pow(dx*dx+dy*dy,0.5);
this.x=dx/c;
this.y=dy/c;
}
public unitVector perp(){
/*unitVector[] a=new unitVector[2];
a[0]=;
a[1]=new unitVector(y,-x);*/
return new unitVector(-y,x);
}
public unitVector opp(){
return new unitVector(-x,-y);
}
public double x(){
return x;
}
public double y(){
return y;
}
}
}