Refactor distributePointsOnCircle into distributePointsOnCircularSegment, refs #5061.
[0ad.git] / binaries / data / mods / public / maps / random / rmgen / math.js
blob97779229250c3dd78ce76c7e8cc4b0f7a987e9c0
1 const g_TileVertices = deepfreeze([
2         new Vector2D(0, 0),
3         new Vector2D(0, 1),
4         new Vector2D(1, 0),
5         new Vector2D(1, 1)
6 ]);
8 const g_AdjacentCoordinates = deepfreeze([
9         new Vector2D(1, 0),
10         new Vector2D(1, 1),
11         new Vector2D(0, 1),
12         new Vector2D(-1, 1),
13         new Vector2D(-1, 0),
14         new Vector2D(-1, -1),
15         new Vector2D(0, -1),
16         new Vector2D(1, -1)
17 ]);
19 function diskArea(radius)
21         return Math.PI * Math.square(radius);
24 /**
25  * Returns the angle of the vector between point 1 and point 2.
26  * The angle is counterclockwise from the positive x axis.
27  */
28 function getAngle(x1, z1, x2, z2)
30         return Math.atan2(z2 - z1, x2 - x1);
33 /**
34  * Get pointCount points equidistantly located on a circle.
35  * @param {Vector2D} center
36  */
37 function distributePointsOnCircle(pointCount, startAngle, radius, center)
39         return distributePointsOnCircularSegment(pointCount, Math. PI * 2, startAngle, radius, center);
42 function distributePointsOnCircularSegment(pointCount, maxAngle, startAngle, radius, center)
44         let points = [];
45         let angle = [];
47         for (let i = 0; i < pointCount; ++i)
48         {
49                 angle[i] = startAngle + maxAngle * i / pointCount;
50                 points[i] = Vector2D.add(center, new Vector2D(radius, 0).rotate(-angle[i]));
51         }
53         return [points, angle];
56 /**
57  * Returns the shortest distance from a point to a line.
58  * The sign of the return value determines the direction!
59  *
60  * @param {Vector2D} - lineStart, lineEnd, point
61  */
62 function distanceOfPointFromLine(lineStart, lineEnd, point)
64         // Since the cross product is the area of the parallelogram with the vectors for sides and
65         // one of the two vectors having length one, that area equals the distance between the points.
66         return Vector2D.sub(lineStart, lineEnd).normalize().cross(Vector2D.sub(point, lineEnd));
69 /**
70  * Returns whether the two lines of the given width going through the given Vector2D intersect.
71  */
72 function testLineIntersection(start1, end1, start2, end2, width)
74         let start1end1 = Vector2D.sub(start1, end1);
75         let start2end2 = Vector2D.sub(start2, end2);
76         let start1start2 = Vector2D.sub(start1, start2);
78         return (
79                 Math.abs(distanceOfPointFromLine(start1, end1, start2)) < width ||
80                 Math.abs(distanceOfPointFromLine(start1, end1, end2)) < width ||
81                 Math.abs(distanceOfPointFromLine(start2, end2, start1)) < width ||
82                 Math.abs(distanceOfPointFromLine(start2, end2, end1)) < width ||
83                 start1end1.cross(start1start2) * start1end1.cross(Vector2D.sub(start1, end2)) <= 0 &&
84                 start2end2.cross(start1start2) * start2end2.cross(Vector2D.sub(start2, end1)) >= 0);
87 /**
88  * Returns the topleft and bottomright coordinate of the given two points.
89  */
90 function getBoundingBox(points)
92         let min = points[0].clone();
93         let max = points[0].clone();
95         for (let point of points)
96         {
97                 min.set(Math.min(min.x, point.x), Math.min(min.y, point.y));
98                 max.set(Math.max(max.x, point.x), Math.max(max.y, point.y));
99         }
101         return {
102                 "min": min,
103                 "max": max
104         };
107 function getPointsInBoundingBox(boundingBox)
109         let points = [];
110         for (let x = boundingBox.min.x; x <= boundingBox.max.x; ++x)
111                 for (let y = boundingBox.min.y; y <= boundingBox.max.y; ++y)
112                         points.push(new Vector2D(x, y));
113         return points;
117  * Sorts the given (x, y) points so that the distance between neighboring points becomes minimal (similar to the traveling salesman problem).
118  */
119 function sortPointsShortestCycle(points)
121         let order = [];
122         let distances = [];
123         if (points.length <= 3)
124         {
125                 for (let i = 0; i < points.length; ++i)
126                         order.push(i);
128                 return order;
129         }
131         // Just add the first 3 points
132         let pointsToAdd = points.map(p => p.clone());
133         for (let i = 0; i < 3; ++i)
134         {
135                 order.push(i);
136                 pointsToAdd.shift(i);
137                 if (i)
138                         distances.push(Math.euclidDistance2D(points[order[i]].x, points[order[i]].y, points[order[i - 1]].x, points[order[i - 1]].y));
139         }
141         distances.push(points[order[0]].distanceTo(points[order[order.length - 1]]));
143         // Add remaining points so the path lengthens the least
144         let numPointsToAdd = pointsToAdd.length;
145         for (let i = 0; i < numPointsToAdd; ++i)
146         {
147                 let indexToAddTo;
148                 let minEnlengthen = Infinity;
149                 let minDist1 = 0;
150                 let minDist2 = 0;
151                 for (let k = 0; k < order.length; ++k)
152                 {
153                         let dist1 = pointsToAdd[0].distanceTo(points[order[k]]);
154                         let dist2 = pointsToAdd[0].distanceTo(points[order[(k + 1) % order.length]]);
156                         let enlengthen = dist1 + dist2 - distances[k];
157                         if (enlengthen < minEnlengthen)
158                         {
159                                 indexToAddTo = k;
160                                 minEnlengthen = enlengthen;
161                                 minDist1 = dist1;
162                                 minDist2 = dist2;
163                         }
164                 }
165                 order.splice(indexToAddTo + 1, 0, i + 3);
166                 distances.splice(indexToAddTo, 1, minDist1, minDist2);
167                 pointsToAdd.shift();
168         }
170         return order;