File size: 3,022 Bytes
2b7aae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Vector2 } from './Vector2.js';

const _vector = /*@__PURE__*/ new Vector2();

class Box2 {
	constructor(min = new Vector2(+Infinity, +Infinity), max = new Vector2(-Infinity, -Infinity)) {
		this.min = min;
		this.max = max;
	}

	set(min, max) {
		this.min.copy(min);
		this.max.copy(max);

		return this;
	}

	setFromPoints(points) {
		this.makeEmpty();

		for (let i = 0, il = points.length; i < il; i++) {
			this.expandByPoint(points[i]);
		}

		return this;
	}

	setFromCenterAndSize(center, size) {
		const halfSize = _vector.copy(size).multiplyScalar(0.5);
		this.min.copy(center).sub(halfSize);
		this.max.copy(center).add(halfSize);

		return this;
	}

	clone() {
		return new this.constructor().copy(this);
	}

	copy(box) {
		this.min.copy(box.min);
		this.max.copy(box.max);

		return this;
	}

	makeEmpty() {
		this.min.x = this.min.y = +Infinity;
		this.max.x = this.max.y = -Infinity;

		return this;
	}

	isEmpty() {
		// this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes

		return this.max.x < this.min.x || this.max.y < this.min.y;
	}

	getCenter(target) {
		return this.isEmpty() ? target.set(0, 0) : target.addVectors(this.min, this.max).multiplyScalar(0.5);
	}

	getSize(target) {
		return this.isEmpty() ? target.set(0, 0) : target.subVectors(this.max, this.min);
	}

	expandByPoint(point) {
		this.min.min(point);
		this.max.max(point);

		return this;
	}

	expandByVector(vector) {
		this.min.sub(vector);
		this.max.add(vector);

		return this;
	}

	expandByScalar(scalar) {
		this.min.addScalar(-scalar);
		this.max.addScalar(scalar);

		return this;
	}

	containsPoint(point) {
		return point.x < this.min.x || point.x > this.max.x || point.y < this.min.y || point.y > this.max.y ? false : true;
	}

	containsBox(box) {
		return this.min.x <= box.min.x && box.max.x <= this.max.x && this.min.y <= box.min.y && box.max.y <= this.max.y;
	}

	getParameter(point, target) {
		// This can potentially have a divide by zero if the box
		// has a size dimension of 0.

		return target.set((point.x - this.min.x) / (this.max.x - this.min.x), (point.y - this.min.y) / (this.max.y - this.min.y));
	}

	intersectsBox(box) {
		// using 4 splitting planes to rule out intersections

		return box.max.x < this.min.x || box.min.x > this.max.x || box.max.y < this.min.y || box.min.y > this.max.y ? false : true;
	}

	clampPoint(point, target) {
		return target.copy(point).clamp(this.min, this.max);
	}

	distanceToPoint(point) {
		const clampedPoint = _vector.copy(point).clamp(this.min, this.max);
		return clampedPoint.sub(point).length();
	}

	intersect(box) {
		this.min.max(box.min);
		this.max.min(box.max);

		return this;
	}

	union(box) {
		this.min.min(box.min);
		this.max.max(box.max);

		return this;
	}

	translate(offset) {
		this.min.add(offset);
		this.max.add(offset);

		return this;
	}

	equals(box) {
		return box.min.equals(this.min) && box.max.equals(this.max);
	}
}

Box2.prototype.isBox2 = true;

export { Box2 };