33using System.Collections.Generic;
74 if (polygons ==
null) {
throw new ArgumentNullException(
"polygons"); }
76 for (
int index = 0; index < polygons.Length; ++index)
78 result[index] =
ApplyMatrix(ref matrix, polygons[index]);
98 if (bitmap ==
null) {
throw new ArgumentNullException(
"bitmap"); }
99 if (bitmap.
Width < 2 || bitmap.
Height < 2) {
throw new ArgumentOutOfRangeException(
"bitmap"); }
108 if (bitmap ==
null) {
throw new ArgumentNullException(
"bitmap"); }
109 if (bitmap.
Width < 2 || bitmap.
Height < 2) {
throw new ArgumentOutOfRangeException(
"bitmap"); }
120 if (width <= 0) {
throw new ArgumentOutOfRangeException(
"width",
"must be greater then 0"); }
121 if (height <= 0) {
throw new ArgumentOutOfRangeException(
"height",
"must be greater then 0"); }
123 Scalar ld2 = height * .5f;
134 if (radius <= 0) {
throw new ArgumentOutOfRangeException(
"radius",
"Must be greater then zero."); }
135 if (vertexCount < 3) {
throw new ArgumentOutOfRangeException(
"vertexCount",
"Must be equal or greater then 3"); }
138 for (
int index = 0; index < vertexCount; ++index)
140 Scalar angle = angleIncrement * index;
156 if (polygons ==
null) {
throw new ArgumentNullException(
"polygons"); }
157 if (polygons.Length == 0) {
throw new ArgumentOutOfRangeException(
"polygons"); }
162 for (
int polyIndex = 0; polyIndex < polygons.Length; ++polyIndex)
164 Vector2D[] vertexes = polygons[polyIndex];
165 if (vertexes ==
null) {
throw new ArgumentNullException(
"polygons"); }
166 if (vertexes.Length == 0) {
throw new ArgumentOutOfRangeException(
"polygons"); }
167 if (vertexes.Length == 1) {
break; }
168 v1 = vertexes[vertexes.Length - 1];
169 for (
int index = 0; index < vertexes.Length; index++, v1 = v2)
171 v2 = vertexes[index];
178 denom += (a + b + c) * d;
181 if (numer == 0) {
return 1; }
182 return denom / (numer * 6);
194 for (
int index = 0; index < vertexes.Length; ++index)
196 distances[index] = vertexes[index].
Magnitude - radius;
198 Scalar lastDistance = distances[distances.Length - 1];
199 Vector2D lastVertex = vertexes[vertexes.Length - 1];
202 List<Vector2D> result =
new List<Vector2D>(vertexes.Length + 1);
203 for (
int index = 0; index < vertexes.Length; ++index, lastVertex = vertex, lastDistance = distance)
205 vertex = vertexes[index];
206 distance = distances[index];
207 if (Math.Abs(Math.Sign(distance) - Math.Sign(lastDistance)) == 2)
209 Scalar lastABS = Math.Abs(lastDistance);
210 Scalar total = (lastABS + Math.Abs(distance));
211 Scalar percent = lastABS / total;
213 Vector2D.
Lerp(ref lastVertex, ref vertex, ref percent, out intersection);
214 result.
Add(intersection);
226 if (vertexes ==
null) {
throw new ArgumentNullException(
"vertexes"); }
227 if (vertexes.Length < 3) {
throw new ArgumentOutOfRangeException(
"vertexes"); }
233 Vector2D v1 = vertexes[vertexes.Length - 1];
235 for (
int index = 0; index < vertexes.Length; index++, v1 = v2)
237 v2 = vertexes[index];
243 centroid.
X += ((v1.X + v2.X) * d);
244 centroid.
Y += ((v1.Y + v2.Y) * d);
247 denom += (a + b + c) * d;
249 area = Math.Abs(area * .5f);
253 return new VertexInfo(centroid, denom / (numer * 6), area);
257 if (polygons ==
null) {
throw new ArgumentNullException(
"polygons"); }
258 if (polygons.Length == 0) {
throw new ArgumentOutOfRangeException(
"polygons"); }
264 for (
int index1 = 0; index1 < polygons.Length; ++index1)
266 Vector2D[] vertexes = polygons[index1];
267 if (vertexes ==
null) {
throw new ArgumentNullException(
"polygons"); }
268 if (vertexes.Length < 3) {
throw new ArgumentOutOfRangeException(
"polygons",
"There must be at least 3 vertexes"); }
270 Vector2D v1 = vertexes[vertexes.Length - 1];
272 for (
int index = 0; index < vertexes.Length; ++index, v1 = v2)
274 v2 = vertexes[index];
280 centroid.
X += ((v1.X + v2.X) * d);
281 centroid.
Y += ((v1.Y + v2.Y) * d);
284 denom += (a + b + c) * d;
286 areaTotal += Math.Abs(area);
289 d = 1 / (areaTotal * 6);
294 (numer == 0) ? (1) : (denom / (numer * 6)),
304 for (
int index = vertexes.Length - 1; index > -1; --index, last = current)
306 current = vertexes[index];
311 last = edges[vertexes.Length - 1];
312 for (
int index = 0; index < vertexes.Length; ++index, last = current)
314 current = edges[index];
323 if (vertexes ==
null) {
throw new ArgumentNullException(
"vertexes"); }
330 if (polygons ==
null) {
throw new ArgumentNullException(
"polygons"); }
332 for (
int index = 0; index < polygons.Length; ++index)
344 for (
int index = 0; index < vertexes.Length; ++index)
346 line.
GetDistance(ref vertexes[index], out distances[index]);
348 Scalar lastDistance = distances[distances.Length - 1];
349 Vector2D lastVertex = vertexes[vertexes.Length - 1];
352 List<Vector2D> result =
new List<Vector2D>(vertexes.Length + 1);
353 for (
int index = 0; index < vertexes.Length; ++index, lastVertex = vertex, lastDistance = distance)
355 vertex = vertexes[index];
356 distance = distances[index];
357 if (Math.Abs(Math.Sign(distance) - Math.Sign(lastDistance)) == 2)
359 Scalar lastABS = Math.Abs(lastDistance);
360 Scalar total = (lastABS + Math.Abs(distance));
361 Scalar percent = lastABS / total;
363 Vector2D.
Lerp(ref lastVertex, ref vertex, ref percent, out intersection);
364 result.
Add(intersection);
382 return Subdivide(vertexes, maxLength,
true);
393 if (vertexes ==
null) {
throw new ArgumentNullException(
"vertexes"); }
394 if (vertexes.Length < 2) {
throw new ArgumentOutOfRangeException(
"vertexes"); }
395 if (maxLength <= 0) {
throw new ArgumentOutOfRangeException(
"maxLength",
"must be greater then zero"); }
396 LinkedList<Vector2D> list =
new LinkedList<Vector2D>(vertexes);
397 LinkedListNode<Vector2D> prevNode, node;
400 prevNode = list.Last;
405 prevNode = list.First;
406 node = prevNode.Next;
412 Vector2D edge = node.Value - prevNode.Value;
416 int count = (int)Math.Ceiling(mag / maxLength);
419 for (
int pos = 1; pos < count; ++pos)
421 prevNode = list.AddAfter(prevNode, edge + prevNode.Value);
436 return Reduce(vertexes, 0);
449 if (vertexes ==
null) {
throw new ArgumentNullException(
"vertexes"); }
450 if (vertexes.Length < 2) {
throw new ArgumentOutOfRangeException(
"vertexes"); }
451 if (areaTolerance < 0) {
throw new ArgumentOutOfRangeException(
"areaTolerance",
"must be equal to or greater then zero."); }
452 List<Vector2D> result =
new List<Vector2D>(vertexes.Length);
455 v1 = vertexes[vertexes.Length - 2];
456 v2 = vertexes[vertexes.Length - 1];
458 for (
int index = 0; index < vertexes.Length; ++index, v2 = v3)
460 if (index == vertexes.Length - 1)
462 if (result.Count == 0) {
throw new ArgumentOutOfRangeException(
"areaTolerance",
"The Tolerance is too high!"); }
465 else { v3 = vertexes[index]; }
469 if (Math.Abs(new1 - (old1 + old2)) > areaTolerance)
475 return result.ToArray();
516 List<Vector2D[]> submerged =
new List<Vector2D[]>(polygons.Length);
517 for (
int index = 0; index < polygons.Length; ++index)
520 if (vertexes.Length >= 3) { submerged.Add(vertexes); }
522 return submerged.ToArray();
526 if (polygons ==
null) {
throw new ArgumentNullException(
"polygons"); }
527 if (polygons.Length == 0) {
throw new ArgumentOutOfRangeException(
"polygons"); }
531 for (
int index = 0; index < polygons.Length; ++index)
533 polygon = polygons[index];
534 if (polygon ==
null) {
throw new ArgumentNullException(
"polygons"); }
542 if (polygons ==
null) {
throw new ArgumentNullException(
"polygons"); }
543 if (polygons.Length == 0) {
throw new ArgumentOutOfRangeException(
"polygons"); }
546 Scalar temp, area, areaTotal;
551 for (
int index1 = 0; index1 < polygons.Length; ++index1)
553 vertexes = polygons[index1];
554 if (vertexes ==
null) {
throw new ArgumentNullException(
"polygons"); }
555 if (vertexes.Length < 3) {
throw new ArgumentOutOfRangeException(
"polygons",
"There must be at least 3 vertexes"); }
556 v1 = vertexes[vertexes.Length - 1];
558 for (
int index = 0; index < vertexes.Length; ++index, v1 = v2)
560 v2 = vertexes[index];
563 result.
X += ((v1.X + v2.X) * temp);
564 result.
Y += ((v1.Y + v2.Y) * temp);
566 areaTotal += Math.Abs(area);
568 temp = 1 / (areaTotal * 3);
575 if (polygons ==
null) {
throw new ArgumentNullException(
"polygons"); }
578 for (
int index = 0; index < polygons.Length; ++index)
590 if (polygons ==
null) {
throw new ArgumentNullException(
"polygons"); }
592 for (
int index = 0; index < polygons.Length; ++index)
594 result[index] =
Reduce(polygons[index], areaTolerance);
604 if (polygons ==
null) {
throw new ArgumentNullException(
"polygons"); }
606 for (
int index = 0; index < polygons.Length; ++index)
608 result[index] =
Subdivide(polygons[index], maxLength, loop);
static Scalar GetInertia(Vector2D[] vertexes)
static Vector2D GetCentroid(Vector2D[] vertexes)
Calculates the Centroid of a polygon.
static Scalar GetArea(Vector2D[] vertexes)
Calculates the area of a polygon.
static Vector2D[] CreateFromBitmap(IBitmap bitmap)
static Vector2D[][] CreateManyFromBitmap(IBitmap bitmap)
static Vector2D[][] CenterVertexesRange(Vector2D[][] polygons)
static Vector2D[] Subdivide(Vector2D[] vertexes, Scalar maxLength, bool loop)
makes sure the distance between 2 vertexes is under the length passed, by adding vertexes between the...
static VertexInfo GetVertexInfoOfRange(Vector2D[][] polygons)
static Vector2D[][] ReduceRange(Vector2D[][] polygons)
static void CalculateNormals(Vector2D[] vertexes, Vector2D[] normals, int offset)
static Vector2D[] GetIntersection(Vector2D[] vertexes, Line line)
static Vector2D[] CreateRectangle(Scalar width, Scalar height)
creates vertexes that describe a Rectangle.
static Scalar GetInertia(Vector2D[] vertexes)
Calculates the moment of inertia for a polygon
static Vector2D[] Subdivide(Vector2D[] vertexes, Scalar maxLength)
makes sure the distance between 2 vertexes is under the length passed, by adding vertexes between the...
static Vector2D[][] ReduceRange(Vector2D[][] polygons, Scalar areaTolerance)
static Vector2D[] CreateFromBitmap(bool[,] bitmap)
Takes a 2D Boolean array with a true value representing a collidable pixel and converts it to an arra...
static Vector2D[][] GetVertexNormalsOfRange(Vector2D[][] polygons)
static Scalar GetArea(Vector2D[] vertexes)
Calculates the area of a polygon.
static Vector2D GetCentroid(Vector2D[] vertexes)
Calculates the Centroid of a polygon.
static Scalar GetAreaOfRange(Vector2D[][] polygons)
static Vector2D[] CreateFromBitmap(IBitmap bitmap)
static Vector2D[][] ApplyMatrixToRange(ref Matrix2x3 matrix, Vector2D[][] polygons)
static Vector2D[] GetIntersection(Vector2D[] vertexes, Scalar radius)
INCOMPLETE! TODO: FINISH
static VertexInfo GetVertexInfo(Vector2D[] vertexes)
static Scalar GetInertiaOfRange(Vector2D[][] polygons)
static Vector2D[] Reduce(Vector2D[] vertexes, Scalar areaTolerance)
Reduces a Polygon's number of vertexes.
static Vector2D[] CenterVertexes(Vector2D[] vertexes)
repositions the polygon so the Centroid is the origin.
static Vector2D[] ApplyMatrix(ref Matrix2x3 matrix, Vector2D[] vertexes)
static Vector2D[] Reduce(Vector2D[] vertexes)
Reduces a Polygon's number of vertexes.
static Vector2D[] CreateCircle(Scalar radius, int vertexCount)
static Vector2D[][] SubdivideRange(Vector2D[][] polygons, Scalar maxLength, bool loop)
static Vector2D[][] CreateRangeFromBitmap(IBitmap bitmap)
static Vector2D[] GetVertexNormals(Vector2D[] vertexes)
static Vector2D[][] CreateRangeFromBitmap(bool[,] bitmap)
static Vector2D[][] SubdivideRange(Vector2D[][] polygons, Scalar maxLength)
static Vector2D[][] GetIntersectionOfRange(Vector2D[][] polygons, Line line)
static Vector2D GetCentroidOfRange(Vector2D[][] polygons)
VertexInfo(Vector2D centroid, Scalar inertia, Scalar area)
Scalar GetDistance(Vector2D point)
A 2x3 matrix which can represent rotations around axes.
This is the Vector Class.
static readonly Vector2D Zero
Vector2D(0,0)
static Vector2D Add(Vector2D left, Vector2D right)
Adds 2 Vectors2Ds.
static Vector2D GetRightHandNormal(Vector2D source)
Gets a Vector2D that is perpendicular(orthogonal) to the passed Vector2D while staying on the XY Plan...
static Vector2D Multiply(Vector2D source, Scalar scalar)
Does Scaler Multiplication on a Vector2D.
Scalar Magnitude
Gets or Sets the Magnitude (Length) of the Vector2D.
Scalar X
This is the X value. (Usually represents a horizontal position or direction.)
static Vector2D Transform(Matrix3x3 matrix, Vector2D source)
Uses a matrix multiplication to Transform the vector.
void CopyTo(Scalar[] array, int index)
static Scalar Dot(Vector2D left, Vector2D right)
Does a Dot Operation Also know as an Inner Product.
static Vector2D Normalize(Vector2D source)
This returns the Normalized Vector2D that is passed. This is also known as a Unit Vector.
static Vector2D FromLengthAndAngle(Scalar length, Scalar radianAngle)
Creates a Vector2D With the given length (Magnitude) and the given Angle.
static Vector2D Subtract(Vector2D left, Vector2D right)
Subtracts 2 Vector2Ds.
Scalar Y
This is the Y value. (Usually represents a vertical position or direction.)
static Scalar ZCross(Vector2D left, Vector2D right)
Does a "2D" Cross Product also know as an Outer Product.
static Vector2D Lerp(Vector2D left, Vector2D right, Scalar amount)