10 static void Get2DPolygon(notnull array<vector> points3D, out notnull array<float> points2D)
13 if (points3D.IsEmpty())
16 points2D.Reserve(points3D.Count() * 2);
18 foreach (
vector point3D : points3D)
20 points2D.Insert(point3D[0]);
21 points2D.Insert(point3D[2]);
31 static void Get3DPolygon(notnull array<float> points2D, out notnull array<vector> points3D)
33 int count = points2D.Count();
39 for (
int i; i < count; i += 2)
41 points3D.Insert({ points2D[i], 0, points2D[i + 1] });
52 static bool GetMinMaxPolygon(notnull array<float> polygon, out
float minX, out
float maxX, out
float minY, out
float maxY)
54 if (!IsPolygonValid(polygon))
63 for (
int i = 2, count = polygon.Count(); i < count; i += 2)
86 static float GetPolygonArea(notnull array<float> polygon)
88 if (!IsPolygonValid(polygon))
94 for (
int i = 0, count = polygon.Count(); i < count; i += 2)
97 result += 0.5 * (polygon[i] * polygon[j + 1] - polygon[j] * polygon[i + 1]);
115 static bool GetRandomPointInPolygon(notnull array<float> polygon, out
float x, out
float y)
117 float minX, minY, maxX, maxY;
118 if (!GetMinMaxPolygon(polygon, minX, maxX, minY, maxY))
121 GetRandomPointInRectangle(minX, maxX, minY, maxY, x, y);
122 while (!
Math2D.IsPointInPolygon(polygon, x, y))
124 GetRandomPointInRectangle(minX, maxX, minY, maxY, x, y);
140 static bool GetRandomPointInRectangle(
float minX,
float maxX,
float minY,
float maxY, out
float x, out
float y)
142 x =
Math.RandomFloat(minX, maxX);
143 y =
Math.RandomFloat(minY, maxY);
157 static bool GetRandomPointInSector(
float originX,
float originY,
float angleFrom,
float angleTo,
float radius, out
float x, out
float y)
160 float angle =
Math.RandomFloat(angleFrom, angleTo);
170 static bool IsPolygonValid(notnull array<float> polygon)
172 int count = polygon.Count();
206 protected static bool CartesianToPolar(
float x,
float y, out
float angle, out
float radius)
208 angle =
Math.Atan2(y, x);
209 radius =
Math.Sqrt(x + y);
222 x =
Math.Cos(angle) * radius;
223 y =
Math.Sin(angle) * radius;
253 value =
Math.PI_HALF - value *
Math.DEG2RAD;
255 if (value < 0 || value >
Math.PI2)
256 value =
Math.Repeat(value,
Math.PI2);
261 if (
float.AlmostEqual(value,
Math.PI2))
342 static bool GetLinesIntersectionXZ(
float x0,
float y0,
float angleRad0,
float x1,
float y1,
float angleRad1, out
float x, out
float y)
344 if (angleRad0 < 0 || angleRad0 >
Math.PI2)
345 angleRad0 =
Math.Repeat(angleRad0,
Math.PI2);
347 if (angleRad1 < 0 || angleRad1 >
Math.PI2)
348 angleRad1 =
Math.Repeat(angleRad1,
Math.PI2);
351 float a1 =
Math.Sin(angleRad0);
352 float b1 = -
Math.Cos(angleRad0);
353 float c1 = a1 * x0 + b1 * y0;
356 float a2 =
Math.Sin(angleRad1);
357 float b2 = -
Math.Cos(angleRad1);
358 float c2 = a2 * x1 + b2 * y1;
360 float determinant = a1 * b2 - a2 * b1;
363 if (
float.AlmostEqual(determinant, 0))
366 x = (b2 * c1 - b1 * c2) / determinant;
367 y = (a1 * c2 - a2 * c1) / determinant;
static bool GetLinesIntersectionXZ(float x0, float y0, float angleRad0, float x1, float y1, float angleRad1, out float x, out float y)