-
Notifications
You must be signed in to change notification settings - Fork 1
/
point2.js
125 lines (99 loc) · 2.57 KB
/
point2.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
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
"use strict";
import { Point } from "./point.js";
export function Point2(x, y) {
this.x = x;
this.y = y;
}
Point2.prototype = Object.create(Point.prototype);
Point2.prototype.constructor = Point2;
Point2.zero = new Point2(0, 0);
Point2.one = new Point2(1, 1);
Point2.prototype.addThis = function (that) {
this.x = this.x + that.x;
this.y = this.y + that.y;
return this;
};
Point2.prototype.subThis = function (that) {
this.x = this.x - that.x;
this.y = this.y - that.y;
return this;
};
Point2.prototype.mulThis = function (that) {
this.x = this.x * that.x;
this.y = this.y * that.y;
return this;
};
Point2.prototype.scaleThis = function (n) {
this.x = this.x * n;
this.y = this.y * n;
return this;
};
Point2.prototype.distance = function () {
return Math.sqrt(this.x * this.x + this.y * this.y);
};
Point2.prototype.bitwiseAndThis = function (n) {
this.x = this.x & n;
this.y = this.y & n;
return this;
};
Point2.prototype.bitwiseOrThis = function (n) {
this.x = this.x | n;
this.y = this.y | n;
return this;
};
Point2.prototype.dot = function (that) {
return this.x * that.x + this.y * that.y;
};
Point2.prototype.roundThis = function () {
this.x = Math.round(this.x);
this.y = Math.round(this.y);
return this;
};
Point2.prototype.floorThis = function () {
this.x = Math.floor(this.x);
this.y = Math.floor(this.y);
return this;
};
Point2.prototype.ceilThis = function () {
this.x = Math.ceil(this.x);
this.y = Math.ceil(this.y);
return this;
};
Point2.prototype.absThis = function () {
this.x = Math.abs(this.x);
this.y = Math.abs(this.y);
};
Point2.prototype.minThis = function (that) {
this.x = Math.min(this.x, that.x);
this.y = Math.min(this.y, that.y);
};
Point2.prototype.maxThis = function (that) {
this.x = Math.max(this.x, that.x);
this.y = Math.max(this.y, that.y);
};
Point2.prototype.transpose = function () {
return this.clone().transposeThis();
};
Point2.prototype.transposeThis = function () {
var temp = this.x;
this.x = this.y;
this.y = temp;
return this;
};
Point2.prototype.clone = function () {
return new Point2(this.x, this.y);
};
Point2.prototype.become = function (that) {
this.x = that.x;
this.y = that.y;
return this;
};
Point2.prototype.hash = function () {
return this.x + "," + this.y;
};
Point2.prototype.equals = function (that) {
return this.x === that.x && this.y === that.y;
};
Point2.prototype.toString = function () {
return 'Point2(' + this.x + ', ' + this.y + ')';
};