Line data Source code
1 : /**
2 : * @file An Area is a set of Vector2D points and a cache to lookup membership quickly.
3 : */
4 :
5 : function Area(points)
6 : {
7 10 : this.points = deepfreeze(points);
8 :
9 10 : const mapSize = g_Map.getSize();
10 :
11 4544 : this.cache = new Array(mapSize).fill(0).map(() => new Uint8Array(mapSize));
12 10 : for (const point of points)
13 23956 : this.cache[point.x][point.y] = 1;
14 : }
15 :
16 7 : Area.prototype.getPoints = function()
17 : {
18 3 : return this.points;
19 : };
20 :
21 7 : Area.prototype.contains = function(point)
22 : {
23 1276 : return g_Map.inMapBounds(point) && this.cache[point.x][point.y] == 1;
24 : };
25 :
26 7 : Area.prototype.getClosestPointTo = function(position)
27 : {
28 0 : const points = this.points;
29 0 : if (!points.length)
30 0 : return undefined;
31 :
32 0 : let closestPoint = points[0];
33 0 : let shortestDistance = Infinity;
34 :
35 0 : for (let i = 0; i < points.length; ++i)
36 : {
37 0 : const point = points[i];
38 0 : const currentDistance = point.distanceToSquared(position);
39 0 : if (currentDistance < shortestDistance)
40 : {
41 0 : if (currentDistance === 0)
42 0 : return point;
43 :
44 0 : shortestDistance = currentDistance;
45 0 : closestPoint = point;
46 : }
47 : }
48 :
49 0 : return closestPoint;
50 : };
|