-
Notifications
You must be signed in to change notification settings - Fork 2
/
area.js
211 lines (178 loc) · 4.15 KB
/
area.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import Point from './point.js'
export default class Area {
static offset (b, a) {
return {
begin: point.offset(b.begin, a.begin),
end: point.offset(b.end, a.end)
}
}
static offsetX (x, a) {
return {
begin: point.offsetX(x, a.begin),
end: point.offsetX(x, a.end)
}
}
static offsetY (y, a) {
return {
begin: point.offsetY(y, a.begin),
end: point.offsetY(y, a.end)
}
}
static sort (a, b) {
return a.begin.y === b.begin.y
? a.begin.x - b.begin.x
: a.begin.y - b.begin.y
}
static toPointSort (a, b) {
return a.begin.y <= b.y && a.end.y >= b.y
? a.begin.y === b.y
? a.begin.x - b.x
: a.end.y === b.y
? a.end.x - b.x
: 0
: a.begin.y - b.y;
}
constructor (a) {
if (a) {
this.begin = new Point(a.begin);
this.end = new Point(a.end);
} else {
this.begin = new Point;
this.end = new Point;
}
}
copy () {
return new Area(this)
}
get () {
var s = [this.begin, this.end].sort(Point.sort)
return new Area({
begin: new Point(s[0]),
end: new Point(s[1])
})
}
set (area) {
this.begin.set(area.begin)
this.end.set(area.end)
}
get height () {
const { begin, end } = this.get()
return end.y - begin.y
}
setLeft (bx, ex) {
this.begin.x = bx;
if (ex != null) this.end.x = ex;
return this;
}
addRight (x) {
this.begin.x += x;
this.end.x += x;
return this;
}
addBottom (y) {
this.end.y += y;
return this;
}
shiftByLines (y) {
this.begin.y += y;
this.end.y += y;
return this
}
normalizeY () {
return this.shiftByLines(-this.begin.y)
}
greaterThan (a) {
return this.begin.y === a.end.y
? this.begin.x > a.end.x
: this.begin.y > a.end.y;
}
greaterThanOrEqual (a) {
return this.begin.y === a.begin.y
? this.begin.x >= a.begin.x
: this.begin.y > a.begin.y;
}
lessThan (a) {
return this.end.y === a.begin.y
? this.end.x < a.begin.x
: this.end.y < a.begin.y;
}
lessThanOrEqual (a) {
return this.end.y === a.end.y
? this.end.x <= a.end.x
: this.end.y < a.end.y;
}
isEmpty () {
return this.begin.equal(this.end)
}
inside (a) {
return this['>'](a) && this['<'](a);
}
outside (a) {
return this['<'](a) || this['>'](a);
}
insideEqual (a) {
return this['>='](a) && this['<='](a);
}
outsideEqual (a) {
return this['<='](a) || this['>='](a);
}
equal (a) {
return this.begin.x === a.begin.x && this.begin.y === a.begin.y
&& this.end.x === a.end.x && this.end.y === a.end.y;
}
beginLineEqual (a) {
return this.begin.y === a.begin.y;
}
endLineEqual (a) {
return this.end.y === a.end.y;
}
linesEqual (a) {
return this['|='](a) && this['=|'](a);
}
sameLine (a) {
return this.begin.y === this.end.y && this.begin.y === a.begin.y;
}
shortenByX (x) {
return new Area({
begin: {
x: this.begin.x + x,
y: this.begin.y
},
end: {
x: this.end.x - x,
y: this.end.y
}
});
}
widenByX (x) {
return new Area({
begin: {
x: this.begin.x - x,
y: this.begin.y
},
end: {
x: this.end.x + x,
y: this.end.y
}
});
}
toString () {
let area = this.get()
return '' + area.begin + '|' + area.end;
}
}
Area.prototype['>'] = Area.prototype.greaterThan
Area.prototype['>='] = Area.prototype.greaterThanOrEqual
Area.prototype['<'] = Area.prototype.lessThan
Area.prototype['<='] = Area.prototype.lessThanOrEqual
Area.prototype['><'] = Area.prototype.inside
Area.prototype['<>'] = Area.prototype.outside
Area.prototype['>=<'] = Area.prototype.insideEqual
Area.prototype['<=>'] = Area.prototype.outsideEqual
Area.prototype['==='] = Area.prototype.equal
Area.prototype['|='] = Area.prototype.beginLineEqual
Area.prototype['=|'] = Area.prototype.endLineEqual
Area.prototype['|=|'] = Area.prototype.linesEqual
Area.prototype['=|='] = Area.prototype.sameLine
Area.prototype['-x-'] = Area.prototype.shortenByX
Area.prototype['+x+'] = Area.prototype.widenByX