This Trac instance is not used for development anymore!

We migrated our development workflow to git and Gitea.
To test the future redirection, replace trac by ariadne in the page URL.

source: ps/trunk/binaries/data/mods/public/maps/random/rmgen/math.js

Last change on this file was 28036, checked in by marder, 11 months ago

rmgen: var -> let -> const

One should always use the variable declaration with the least possible scope.
This patch cleans up many (but not all) of the var in rmgen and replaces them with let or const.
The same is done for let -> const.

comments by: @sera @Stan

Differential revision: https://code.wildfiregames.com/D5214

  • Property svn:eol-style set to native
File size: 4.8 KB
RevLine 
[21175]1const g_TileVertices = deepfreeze([
2 new Vector2D(0, 0),
3 new Vector2D(0, 1),
4 new Vector2D(1, 0),
5 new Vector2D(1, 1)
6]);
[21132]7
[21175]8const 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]);
18
[20332]19function diskArea(radius)
20{
21 return Math.PI * Math.square(radius);
22}
23
[20395]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 */
28function getAngle(x1, z1, x2, z2)
29{
30 return Math.atan2(z2 - z1, x2 - x1);
31}
32
[20416]33/**
34 * Get pointCount points equidistantly located on a circle.
[20787]35 * @param {Vector2D} center
[20416]36 */
[20787]37function distributePointsOnCircle(pointCount, startAngle, radius, center)
[20416]38{
[21634]39 return distributePointsOnCircularSegment(pointCount, 2 * Math.PI * (pointCount - 1) / pointCount, startAngle, radius, center);
[21472]40}
41
[21634]42/**
43 * Get pointCount points equidistantly located on a circular segment, including both endpoints.
44 */
[21472]45function distributePointsOnCircularSegment(pointCount, maxAngle, startAngle, radius, center)
46{
[28036]47 const points = [];
48 const angle = [];
[21634]49 pointCount = Math.round(pointCount);
[20416]50
51 for (let i = 0; i < pointCount; ++i)
52 {
[21634]53 angle[i] = startAngle + maxAngle * i / Math.max(1, pointCount - 1);
[20787]54 points[i] = Vector2D.add(center, new Vector2D(radius, 0).rotate(-angle[i]));
[20416]55 }
56
[20787]57 return [points, angle];
[20416]58}
59
60/**
[20758]61 * Returns the shortest distance from a point to a line.
62 * The sign of the return value determines the direction!
63 *
64 * @param {Vector2D} - lineStart, lineEnd, point
[20395]65 */
[20758]66function distanceOfPointFromLine(lineStart, lineEnd, point)
[20395]67{
[20758]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));
[20395]71}
72
73/**
[20758]74 * Returns whether the two lines of the given width going through the given Vector2D intersect.
[20395]75 */
[20758]76function testLineIntersection(start1, end1, start2, end2, width)
[20395]77{
[28036]78 const start1end1 = Vector2D.sub(start1, end1);
79 const start2end2 = Vector2D.sub(start2, end2);
80 const start1start2 = Vector2D.sub(start1, start2);
[20395]81
[20758]82 return (
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);
[20395]89}
[20417]90
91/**
[21080]92 * Returns the topleft and bottomright coordinate of the given two points.
93 */
94function getBoundingBox(points)
95{
[28036]96 const min = points[0].clone();
97 const max = points[0].clone();
[21080]98
[28036]99 for (const point of points)
[21080]100 {
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));
103 }
104
105 return {
106 "min": min,
107 "max": max
108 };
109}
110
111function getPointsInBoundingBox(boundingBox)
112{
[28036]113 const points = [];
[21080]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));
117 return points;
118}
119
120/**
[22028]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
[20417]124 */
125function sortPointsShortestCycle(points)
126{
[28036]127 const order = [];
128 const distances = [];
[20417]129 if (points.length <= 3)
130 {
131 for (let i = 0; i < points.length; ++i)
132 order.push(i);
133
134 return order;
135 }
136
137 // Just add the first 3 points
[28036]138 const pointsToAdd = points.map(p => p.clone());
[20417]139 for (let i = 0; i < 3; ++i)
140 {
141 order.push(i);
[22028]142 pointsToAdd.shift();
[20417]143 if (i)
[22028]144 distances.push(points[order[i]].distanceTo(points[order[i - 1]]));
[20417]145 }
146
[21069]147 distances.push(points[order[0]].distanceTo(points[order[order.length - 1]]));
[20417]148
149 // Add remaining points so the path lengthens the least
[28036]150 const numPointsToAdd = pointsToAdd.length;
[20417]151 for (let i = 0; i < numPointsToAdd; ++i)
152 {
153 let indexToAddTo;
154 let minEnlengthen = Infinity;
155 let minDist1 = 0;
156 let minDist2 = 0;
157 for (let k = 0; k < order.length; ++k)
158 {
[28036]159 const dist1 = pointsToAdd[0].distanceTo(points[order[k]]);
160 const dist2 = pointsToAdd[0].distanceTo(points[order[(k + 1) % order.length]]);
[21069]161
[28036]162 const enlengthen = dist1 + dist2 - distances[k];
[20417]163 if (enlengthen < minEnlengthen)
164 {
165 indexToAddTo = k;
166 minEnlengthen = enlengthen;
167 minDist1 = dist1;
168 minDist2 = dist2;
169 }
170 }
171 order.splice(indexToAddTo + 1, 0, i + 3);
172 distances.splice(indexToAddTo, 1, minDist1, minDist2);
173 pointsToAdd.shift();
174 }
175
176 return order;
177}
Note: See TracBrowser for help on using the repository browser.