This repository has been archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint.js
88 lines (78 loc) · 1.94 KB
/
point.js
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
/**
* Represents single point in 2D
* Contains some useful functions
*/
export class Point extends Array {
/**
* Creates Point with given Coords
* @param {Number} x Coord x
* @param {Number} y Coord y
*/
constructor(x = 0, y = 0) {
super();
this.x = x;
this.y = y;
}
/**
* Calculates distance between first and second point
* @param {Point} first
* @param {Point} second
* @returns {Number}
*/
static distance(first, second) {
return Math.hypot(second.x - first.x, second.y - first.y);
}
/**
* Checks equality between given Points
* @param {Point} first
* @param {Point} second
* @returns {Boolean} true if equals to other
*/
static equals(first, second) {
return Math.abs(first.x - second.x) < Number.EPSILON && Math.abs(first.y - second.y) < Number.EPSILON;
}
/**
* https://webglfundamentals.org/webgl/resources/m3.js
* @param {Point} first
* @param {Point} second
* @returns {Number}
*/
static dot(first, second) {
return first.x * second.x + first.y * second.y;
}
/**
* Creates deep copy
* @returns {Point} Deep copy
*/
duplicate() {
return new Point(this.x, this.y);
}
/**
* Calculates distance between this and second point
* @param {Point} other Point to calculate distance to
* @returns {Number} Distance between points
*/
distance(other) {
return Point.distance(this, other);
}
/**
* Checks equality with other Point
* @param {Point} other Point to check equality on
* @returns {Boolean} true if equals to other
*/
equals(other) {
return Point.equals(this, other);
}
set x(value) {
this[0] = value;
}
get x() {
return this[0];
}
set y(value) {
this[1] = value;
}
get y() {
return this[1];
}
}