1 | import { Range } from "./Range";
|
2 | export class Circle extends Range {
|
3 | constructor(x, y, radius) {
|
4 | super(x, y);
|
5 | this.radius = radius;
|
6 | }
|
7 | contains(point) {
|
8 | const d = Math.pow(point.x - this.position.x, 2) + Math.pow(point.y - this.position.y, 2);
|
9 | return d <= this.radius * this.radius;
|
10 | }
|
11 | intersects(range) {
|
12 | const rect = range;
|
13 | const circle = range;
|
14 | const pos1 = this.position;
|
15 | const pos2 = range.position;
|
16 | const xDist = Math.abs(pos2.x - pos1.x);
|
17 | const yDist = Math.abs(pos2.y - pos1.y);
|
18 | const r = this.radius;
|
19 | if (circle.radius !== undefined) {
|
20 | const rSum = r + circle.radius;
|
21 | const dist = Math.sqrt(xDist * xDist + yDist + yDist);
|
22 | return rSum > dist;
|
23 | }
|
24 | else if (rect.size !== undefined) {
|
25 | const w = rect.size.width;
|
26 | const h = rect.size.height;
|
27 | const edges = Math.pow(xDist - w, 2) + Math.pow(yDist - h, 2);
|
28 | if (xDist > r + w || yDist > r + h) {
|
29 | return false;
|
30 | }
|
31 | if (xDist <= w || yDist <= h) {
|
32 | return true;
|
33 | }
|
34 | return edges <= r * r;
|
35 | }
|
36 | return false;
|
37 | }
|
38 | }
|