-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathPoint.ts
47 lines (42 loc) · 935 Bytes
/
Point.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
/**
* Define a point in 2D space
*
* @example
* // Create a point
* const pointA = new Point(0, 0);
* // Update point positions
* pointA.update(10, 10)
* // Get a distance to another point
* const distance = pointA.getDistanceTo(pointB);
*
* @category Helpers
*/
class Point {
public x: number;
public y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
/**
* Update the x and y values
*/
update(point: Point): void {
this.x = point.x;
this.y = point.y;
}
/**
* Get the difference for x and y axis to another point
*/
getDifferenceTo(point: Point): Point {
return new Point(this.x - point.x, this.y - point.y);
}
/**
* Calculate distance to another point
*/
getDistanceTo(point: Point): number {
const diff = this.getDifferenceTo(point);
return Math.sqrt(Math.pow(diff.x, 2) + Math.pow(diff.y, 2));
}
}
export default Point;