-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflight_path.ts
66 lines (57 loc) · 2.33 KB
/
flight_path.ts
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
export interface Rectangle {
topLeftLat: number;
topLeftLon: number;
topRightLat: number;
topRightLon: number;
bottomLeftLat: number;
bottomLeftLon: number;
bottomRightLat: number;
bottomRightLon: number;
}
export interface Coordinates {
latitude: number;
longitude: number;
heading: number;
}
export function calculateRectangularMovement(
startTime: number,
currentTime: number,
speed: number,
rectangle: Rectangle
): Coordinates {
const topLeft: Coordinates = { latitude: rectangle.topLeftLat, longitude: rectangle.topLeftLon, heading: 0 };
const topRight: Coordinates = { latitude: rectangle.topRightLat, longitude: rectangle.topRightLon, heading: 0 };
const bottomLeft: Coordinates = { latitude: rectangle.bottomLeftLat, longitude: rectangle.bottomLeftLon, heading: 0 };
// const bottomRight: Coordinates = { latitude: rectangle.bottomRightLat, longitude: rectangle.bottomRightLon };
const width: number = Math.abs(topLeft.longitude - topRight.longitude);
const height: number = Math.abs(topLeft.latitude - bottomLeft.latitude);
const perimeter: number = 2 * (width + height);
const elapsedTime: number = currentTime - startTime;
const distance: number = speed * elapsedTime;
const distanceAlongPerimeter: number = distance % perimeter;
let currentLatitude: number;
let currentLongitude: number;
let currentHeading: number;
if (distanceAlongPerimeter <= width) {
// Moving along the top edge
currentHeading = 90;
currentLatitude = topLeft.latitude;
currentLongitude = topLeft.longitude + distanceAlongPerimeter;
} else if (distanceAlongPerimeter <= width + height) {
// Moving along the right edge
currentHeading = 180;
currentLatitude = topLeft.latitude - (distanceAlongPerimeter - width);
currentLongitude = topRight.longitude;
} else if (distanceAlongPerimeter <= 2 * width + height) {
// Moving along the bottom edge
currentHeading = 270;
currentLatitude = bottomLeft.latitude;
currentLongitude = topRight.longitude - (distanceAlongPerimeter - width - height);
} else {
// Moving along the left edge
currentHeading = 0;
currentLatitude = bottomLeft.latitude + (distanceAlongPerimeter - 2 * width - height);
currentLongitude = topLeft.longitude;
}
return { latitude: currentLatitude, longitude: currentLongitude, heading: currentHeading };
}