1 const g_TileVertices = deepfreeze([
8 const g_AdjacentCoordinates = deepfreeze([
19 function diskArea(radius)
21 return Math.PI * Math.square(radius);
25 * Returns the angle of the vector between point 1 and point 2.
26 * The angle is counterclockwise from the positive x axis.
28 function getAngle(x1, z1, x2, z2)
30 return Math.atan2(z2 - z1, x2 - x1);
34 * Get pointCount points equidistantly located on a circle.
35 * @param {Vector2D} center
37 function distributePointsOnCircle(pointCount, startAngle, radius, center)
39 return distributePointsOnCircularSegment(pointCount, 2 * Math.PI * (pointCount - 1) / pointCount, startAngle, radius, center);
43 * Get pointCount points equidistantly located on a circular segment, including both endpoints.
45 function distributePointsOnCircularSegment(pointCount, maxAngle, startAngle, radius, center)
49 pointCount = Math.round(pointCount);
51 for (let i = 0; i < pointCount; ++i)
53 angle[i] = startAngle + maxAngle * i / Math.max(1, pointCount - 1);
54 points[i] = Vector2D.add(center, new Vector2D(radius, 0).rotate(-angle[i]));
57 return [points, angle];
61 * Returns the shortest distance from a point to a line.
62 * The sign of the return value determines the direction!
64 * @param {Vector2D} - lineStart, lineEnd, point
66 function distanceOfPointFromLine(lineStart, lineEnd, point)
68 // Since the cross product is the area of the parallelogram with the vectors for sides and
69 // one of the two vectors having length one, that area equals the distance between the points.
70 return Vector2D.sub(lineStart, lineEnd).normalize().cross(Vector2D.sub(point, lineEnd));
74 * Returns whether the two lines of the given width going through the given Vector2D intersect.
76 function testLineIntersection(start1, end1, start2, end2, width)
78 const start1end1 = Vector2D.sub(start1, end1);
79 const start2end2 = Vector2D.sub(start2, end2);
80 const start1start2 = Vector2D.sub(start1, start2);
83 Math.abs(distanceOfPointFromLine(start1, end1, start2)) < width ||
84 Math.abs(distanceOfPointFromLine(start1, end1, end2)) < width ||
85 Math.abs(distanceOfPointFromLine(start2, end2, start1)) < width ||
86 Math.abs(distanceOfPointFromLine(start2, end2, end1)) < width ||
87 start1end1.cross(start1start2) * start1end1.cross(Vector2D.sub(start1, end2)) <= 0 &&
88 start2end2.cross(start1start2) * start2end2.cross(Vector2D.sub(start2, end1)) >= 0);
92 * Returns the topleft and bottomright coordinate of the given two points.
94 function getBoundingBox(points)
96 const min = points[0].clone();
97 const max = points[0].clone();
99 for (const point of points)
101 min.set(Math.min(min.x, point.x), Math.min(min.y, point.y));
102 max.set(Math.max(max.x, point.x), Math.max(max.y, point.y));
111 function getPointsInBoundingBox(boundingBox)
114 for (let x = boundingBox.min.x; x <= boundingBox.max.x; ++x)
115 for (let y = boundingBox.min.y; y <= boundingBox.max.y; ++y)
116 points.push(new Vector2D(x, y));
121 * Get the order of the given points to get the shortest closed path (similar to the traveling salesman problem).
122 * @param {Vectro2D[]} points - Points the path should go through
123 * @returns {number[]} Ordered indices, same length as points
125 function sortPointsShortestCycle(points)
128 const distances = [];
129 if (points.length <= 3)
131 for (let i = 0; i < points.length; ++i)
137 // Just add the first 3 points
138 const pointsToAdd = points.map(p => p.clone());
139 for (let i = 0; i < 3; ++i)
144 distances.push(points[order[i]].distanceTo(points[order[i - 1]]));
147 distances.push(points[order[0]].distanceTo(points[order[order.length - 1]]));
149 // Add remaining points so the path lengthens the least
150 const numPointsToAdd = pointsToAdd.length;
151 for (let i = 0; i < numPointsToAdd; ++i)
154 let minEnlengthen = Infinity;
157 for (let k = 0; k < order.length; ++k)
159 const dist1 = pointsToAdd[0].distanceTo(points[order[k]]);
160 const dist2 = pointsToAdd[0].distanceTo(points[order[(k + 1) % order.length]]);
162 const enlengthen = dist1 + dist2 - distances[k];
163 if (enlengthen < minEnlengthen)
166 minEnlengthen = enlengthen;
171 order.splice(indexToAddTo + 1, 0, i + 3);
172 distances.splice(indexToAddTo, 1, minDist1, minDist2);