-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm.js
More file actions
91 lines (79 loc) · 1.79 KB
/
algorithm.js
File metadata and controls
91 lines (79 loc) · 1.79 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
80
81
82
83
84
85
86
87
88
89
90
91
// speed = m/s
const carsLength = 10;
const spaceBetweenCars = 20;
let isStarted = false;
let acrossCar = {
positionX: 450,
positionY: 100,
speed: 10,
acceleration: 0.3,
};
let frontCar = {
positionX: 300,
positionY: 120,
speed: 12,
acceleration: 0.1,
};
let myCar = {
positionX: frontCar.positionX - spaceBetweenCars,
positionY: 120,
speed: 20,
acceleration: 3,
};
let isOvertaked;
let time;
canOvertake = () => {
time = 0;
let spaceForOvertake;
let wayForFrontCar;
let wayForMyCar;
while (time <= 1000) {
time += 0.0001;
spaceForOvertake =
acrossCar.positionX -
((acrossCar.speed * time +
acrossCar.acceleration * time +
acrossCar.speed) /
2) *
time -
(frontCar.positionX +
((frontCar.speed + frontCar.acceleration * time + frontCar.speed) / 2) *
time) -
carsLength;
wayForMyCar =
((myCar.speed + myCar.acceleration * time + myCar.speed) / 2) * time;
wayForFrontCar =
((frontCar.speed + frontCar.acceleration * time + frontCar.speed) / 2) *
time;
if (
acrossCar.positionX > frontCar.positionX &&
wayForMyCar - wayForFrontCar >= frontCar.positionX - myCar.positionX
) {
break;
}
}
function dataText(canOvertake) {
let dataText = `
Can Overtake: ${canOvertake}
Way For My Car: ${wayForMyCar.toFixed(3)}
Space For Overtake: ${spaceForOvertake.toFixed(3)}
Necessary Time: ${time.toFixed(3)}
`;
return dataText;
}
if (spaceForOvertake >= wayForMyCar) {
return [
true,
wayForMyCar.toFixed(2),
spaceForOvertake.toFixed(2),
time.toFixed(2),
];
} else {
return [
false,
wayForMyCar.toFixed(2),
spaceForOvertake.toFixed(2),
time.toFixed(2),
];
}
};