1 | #region MIT License |
---|
2 | /* |
---|
3 | * Copyright (c) 2005-2008 Jonathan Mark Porter. http://physics2d.googlepages.com/ |
---|
4 | * |
---|
5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
6 | * of this software and associated documentation files (the "Software"), to deal |
---|
7 | * in the Software without restriction, including without limitation the rights to |
---|
8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
---|
9 | * the Software, and to permit persons to whom the Software is furnished to do so, |
---|
10 | * subject to the following conditions: |
---|
11 | * |
---|
12 | * The above copyright notice and this permission notice shall be |
---|
13 | * included in all copies or substantial portions of the Software. |
---|
14 | * |
---|
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
---|
16 | * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
---|
17 | * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
---|
18 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
---|
19 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
---|
20 | * OTHER DEALINGS IN THE SOFTWARE. |
---|
21 | */ |
---|
22 | #endregion |
---|
23 | |
---|
24 | |
---|
25 | |
---|
26 | |
---|
27 | #if UseDouble |
---|
28 | using Scalar = System.Double; |
---|
29 | #else |
---|
30 | using Scalar = System.Single; |
---|
31 | #endif |
---|
32 | using System; |
---|
33 | using System.Collections.Generic; |
---|
34 | using AdvanceMath; |
---|
35 | using AdvanceMath.Geometry2D; |
---|
36 | |
---|
37 | |
---|
38 | namespace Physics2DDotNet.Shapes |
---|
39 | { |
---|
40 | |
---|
41 | public class VertexInfo |
---|
42 | { |
---|
43 | Vector2D centroid; |
---|
44 | Scalar inertia; |
---|
45 | Scalar area; |
---|
46 | public VertexInfo(Vector2D centroid, Scalar inertia, Scalar area) |
---|
47 | { |
---|
48 | this.centroid = centroid; |
---|
49 | this.inertia = inertia; |
---|
50 | this.area = area; |
---|
51 | } |
---|
52 | public Vector2D Centroid |
---|
53 | { |
---|
54 | get { return centroid; } |
---|
55 | } |
---|
56 | public Scalar Inertia |
---|
57 | { |
---|
58 | get { return inertia; } |
---|
59 | } |
---|
60 | public Scalar Area |
---|
61 | { |
---|
62 | get { return area; } |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | public static class VertexHelper |
---|
67 | { |
---|
68 | public static Vector2D[] ApplyMatrix(ref Matrix2x3 matrix, Vector2D[] vertexes) |
---|
69 | { |
---|
70 | return OperationHelper.ArrayRefOp<Matrix2x3, Vector2D, Vector2D>(ref matrix, vertexes, Vector2D.Transform); |
---|
71 | } |
---|
72 | public static Vector2D[][] ApplyMatrixToRange(ref Matrix2x3 matrix, Vector2D[][] polygons) |
---|
73 | { |
---|
74 | if (polygons == null) { throw new ArgumentNullException("polygons"); } |
---|
75 | Vector2D[][] result = new Vector2D[polygons.Length][]; |
---|
76 | for (int index = 0; index < polygons.Length; ++index) |
---|
77 | { |
---|
78 | result[index] = ApplyMatrix(ref matrix, polygons[index]); |
---|
79 | } |
---|
80 | return result; |
---|
81 | } |
---|
82 | |
---|
83 | /// <summary> |
---|
84 | /// Takes a 2D Boolean array with a true value representing a collidable pixel and converts |
---|
85 | /// it to an array of vertex that surrounds that bitmap. The bitmap should be a single piece |
---|
86 | /// if there are many pieces it will only return the geometry of the first piece it comes |
---|
87 | /// across. Make sure there are at least 3 pixels in a piece otherwise an exception will be |
---|
88 | /// thrown (it wont be a polygon). |
---|
89 | /// </summary> |
---|
90 | /// <param name="bitmap">a bitmap to be converted. true means its collidable.</param> |
---|
91 | /// <returns>A Vector2D[] representing the bitmap.</returns> |
---|
92 | public static Vector2D[] CreateFromBitmap(bool[,] bitmap) |
---|
93 | { |
---|
94 | return CreateFromBitmap(new ArrayBitmap(bitmap)); |
---|
95 | } |
---|
96 | public static Vector2D[] CreateFromBitmap(IBitmap bitmap) |
---|
97 | { |
---|
98 | if (bitmap == null) { throw new ArgumentNullException("bitmap"); } |
---|
99 | if (bitmap.Width < 2 || bitmap.Height < 2) { throw new ArgumentOutOfRangeException("bitmap"); } |
---|
100 | return BitmapHelper.CreateFromBitmap(bitmap); |
---|
101 | } |
---|
102 | public static Vector2D[][] CreateRangeFromBitmap(bool[,] bitmap) |
---|
103 | { |
---|
104 | return CreateRangeFromBitmap(new ArrayBitmap(bitmap)); |
---|
105 | } |
---|
106 | public static Vector2D[][] CreateRangeFromBitmap(IBitmap bitmap) |
---|
107 | { |
---|
108 | if (bitmap == null) { throw new ArgumentNullException("bitmap"); } |
---|
109 | if (bitmap.Width < 2 || bitmap.Height < 2) { throw new ArgumentOutOfRangeException("bitmap"); } |
---|
110 | return BitmapHelper.CreateManyFromBitmap(bitmap); |
---|
111 | } |
---|
112 | /// <summary> |
---|
113 | /// creates vertexes that describe a Rectangle. |
---|
114 | /// </summary> |
---|
115 | /// <param name="width"></param> |
---|
116 | /// <param name="height">The length of the Rectangle</param> |
---|
117 | /// <returns>array of vectors the describe a rectangle</returns> |
---|
118 | public static Vector2D[] CreateRectangle(Scalar width, Scalar height) |
---|
119 | { |
---|
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"); } |
---|
122 | Scalar wd2 = width * .5f; |
---|
123 | Scalar ld2 = height * .5f; |
---|
124 | return new Vector2D[4] |
---|
125 | { |
---|
126 | new Vector2D(wd2, ld2), |
---|
127 | new Vector2D(-wd2, ld2), |
---|
128 | new Vector2D(-wd2, -ld2), |
---|
129 | new Vector2D(wd2, -ld2) |
---|
130 | }; |
---|
131 | } |
---|
132 | public static Vector2D[] CreateCircle(Scalar radius, int vertexCount) |
---|
133 | { |
---|
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"); } |
---|
136 | Vector2D[] result = new Vector2D[vertexCount]; |
---|
137 | Scalar angleIncrement = MathHelper.TwoPi / vertexCount; |
---|
138 | for (int index = 0; index < vertexCount; ++index) |
---|
139 | { |
---|
140 | Scalar angle = angleIncrement * index; |
---|
141 | Vector2D.FromLengthAndAngle(ref radius, ref angle, out result[index]); |
---|
142 | } |
---|
143 | return result; |
---|
144 | } |
---|
145 | /// <summary> |
---|
146 | /// Calculates the moment of inertia for a polygon |
---|
147 | /// </summary> |
---|
148 | /// <param name="vertexes"></param> |
---|
149 | /// <returns>the moment of inertia</returns> |
---|
150 | public static Scalar GetInertia(Vector2D[] vertexes) |
---|
151 | { |
---|
152 | return BoundingPolygon.GetInertia(vertexes); |
---|
153 | } |
---|
154 | public static Scalar GetInertiaOfRange(Vector2D[][] polygons) |
---|
155 | { |
---|
156 | if (polygons == null) { throw new ArgumentNullException("polygons"); } |
---|
157 | if (polygons.Length == 0) { throw new ArgumentOutOfRangeException("polygons"); } |
---|
158 | Scalar denom = 0; |
---|
159 | Scalar numer = 0; |
---|
160 | Scalar a, b, c, d; |
---|
161 | Vector2D v1, v2; |
---|
162 | for (int polyIndex = 0; polyIndex < polygons.Length; ++polyIndex) |
---|
163 | { |
---|
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) |
---|
170 | { |
---|
171 | v2 = vertexes[index]; |
---|
172 | Vector2D.Dot(ref v2, ref v2, out a); |
---|
173 | Vector2D.Dot(ref v2, ref v1, out b); |
---|
174 | Vector2D.Dot(ref v1, ref v1, out c); |
---|
175 | Vector2D.ZCross(ref v1, ref v2, out d); |
---|
176 | d = Math.Abs(d); |
---|
177 | numer += d; |
---|
178 | denom += (a + b + c) * d; |
---|
179 | } |
---|
180 | } |
---|
181 | if (numer == 0) { return 1; } |
---|
182 | return denom / (numer * 6); |
---|
183 | } |
---|
184 | |
---|
185 | /// <summary> |
---|
186 | /// INCOMPLETE! TODO: FINISH |
---|
187 | /// </summary> |
---|
188 | /// <param name="vertexes"></param> |
---|
189 | /// <param name="radius"></param> |
---|
190 | /// <returns></returns> |
---|
191 | public static Vector2D[] GetIntersection(Vector2D[] vertexes, Scalar radius) |
---|
192 | { |
---|
193 | Scalar[] distances = new Scalar[vertexes.Length]; |
---|
194 | for (int index = 0; index < vertexes.Length; ++index) |
---|
195 | { |
---|
196 | distances[index] = vertexes[index].Magnitude - radius; |
---|
197 | } |
---|
198 | Scalar lastDistance = distances[distances.Length - 1]; |
---|
199 | Vector2D lastVertex = vertexes[vertexes.Length - 1]; |
---|
200 | Vector2D vertex; |
---|
201 | Scalar distance; |
---|
202 | List<Vector2D> result = new List<Vector2D>(vertexes.Length + 1); |
---|
203 | for (int index = 0; index < vertexes.Length; ++index, lastVertex = vertex, lastDistance = distance) |
---|
204 | { |
---|
205 | vertex = vertexes[index]; |
---|
206 | distance = distances[index]; |
---|
207 | if (Math.Abs(Math.Sign(distance) - Math.Sign(lastDistance)) == 2) |
---|
208 | { |
---|
209 | Scalar lastABS = Math.Abs(lastDistance); |
---|
210 | Scalar total = (lastABS + Math.Abs(distance)); |
---|
211 | Scalar percent = lastABS / total; |
---|
212 | Vector2D intersection; |
---|
213 | Vector2D.Lerp(ref lastVertex, ref vertex, ref percent, out intersection); |
---|
214 | result.Add(intersection); |
---|
215 | } |
---|
216 | if (distance >= 0) |
---|
217 | { |
---|
218 | result.Add(vertex); |
---|
219 | } |
---|
220 | } |
---|
221 | return result.ToArray(); |
---|
222 | } |
---|
223 | |
---|
224 | public static VertexInfo GetVertexInfo(Vector2D[] vertexes) |
---|
225 | { |
---|
226 | if (vertexes == null) { throw new ArgumentNullException("vertexes"); } |
---|
227 | if (vertexes.Length < 3) { throw new ArgumentOutOfRangeException("vertexes"); } |
---|
228 | Scalar area = 0; |
---|
229 | Scalar denom = 0; |
---|
230 | Scalar numer = 0; |
---|
231 | Scalar a, b, c, d; |
---|
232 | Vector2D centroid = Vector2D.Zero; |
---|
233 | Vector2D v1 = vertexes[vertexes.Length - 1]; |
---|
234 | Vector2D v2; |
---|
235 | for (int index = 0; index < vertexes.Length; index++, v1 = v2) |
---|
236 | { |
---|
237 | v2 = vertexes[index]; |
---|
238 | Vector2D.Dot(ref v2, ref v2, out a); |
---|
239 | Vector2D.Dot(ref v2, ref v1, out b); |
---|
240 | Vector2D.Dot(ref v1, ref v1, out c); |
---|
241 | Vector2D.ZCross(ref v1, ref v2, out d); |
---|
242 | area += d; |
---|
243 | centroid.X += ((v1.X + v2.X) * d); |
---|
244 | centroid.Y += ((v1.Y + v2.Y) * d); |
---|
245 | d = Math.Abs(d); |
---|
246 | numer += d; |
---|
247 | denom += (a + b + c) * d; |
---|
248 | } |
---|
249 | area = Math.Abs(area * .5f); |
---|
250 | d = 1 / (area * 6); |
---|
251 | centroid.X *= d; |
---|
252 | centroid.Y *= d; |
---|
253 | return new VertexInfo(centroid, denom / (numer * 6), area); |
---|
254 | } |
---|
255 | public static VertexInfo GetVertexInfoOfRange(Vector2D[][] polygons) |
---|
256 | { |
---|
257 | if (polygons == null) { throw new ArgumentNullException("polygons"); } |
---|
258 | if (polygons.Length == 0) { throw new ArgumentOutOfRangeException("polygons"); } |
---|
259 | Scalar a, b, c, d; |
---|
260 | Scalar denom = 0; |
---|
261 | Scalar numer = 0; |
---|
262 | Scalar areaTotal = 0; |
---|
263 | Vector2D centroid = Vector2D.Zero; |
---|
264 | for (int index1 = 0; index1 < polygons.Length; ++index1) |
---|
265 | { |
---|
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"); } |
---|
269 | Scalar area = 0; |
---|
270 | Vector2D v1 = vertexes[vertexes.Length - 1]; |
---|
271 | Vector2D v2; |
---|
272 | for (int index = 0; index < vertexes.Length; ++index, v1 = v2) |
---|
273 | { |
---|
274 | v2 = vertexes[index]; |
---|
275 | Vector2D.Dot(ref v2, ref v2, out a); |
---|
276 | Vector2D.Dot(ref v2, ref v1, out b); |
---|
277 | Vector2D.Dot(ref v1, ref v1, out c); |
---|
278 | Vector2D.ZCross(ref v1, ref v2, out d); |
---|
279 | area += d; |
---|
280 | centroid.X += ((v1.X + v2.X) * d); |
---|
281 | centroid.Y += ((v1.Y + v2.Y) * d); |
---|
282 | d = Math.Abs(d); |
---|
283 | numer += d; |
---|
284 | denom += (a + b + c) * d; |
---|
285 | } |
---|
286 | areaTotal += Math.Abs(area); |
---|
287 | } |
---|
288 | areaTotal *= .5f; |
---|
289 | d = 1 / (areaTotal * 6); |
---|
290 | centroid.X *= d; |
---|
291 | centroid.Y *= d; |
---|
292 | return new VertexInfo( |
---|
293 | centroid, |
---|
294 | (numer == 0) ? (1) : (denom / (numer * 6)), |
---|
295 | areaTotal); |
---|
296 | } |
---|
297 | |
---|
298 | internal static void CalculateNormals(Vector2D[] vertexes, Vector2D[] normals, int offset) |
---|
299 | { |
---|
300 | Vector2D[] edges = new Vector2D[vertexes.Length]; |
---|
301 | Vector2D last = vertexes[0]; |
---|
302 | Vector2D current; |
---|
303 | Vector2D temp; |
---|
304 | for (int index = vertexes.Length - 1; index > -1; --index, last = current) |
---|
305 | { |
---|
306 | current = vertexes[index]; |
---|
307 | Vector2D.Subtract(ref current, ref last, out temp); |
---|
308 | Vector2D.Normalize(ref temp, out temp); |
---|
309 | Vector2D.GetRightHandNormal(ref temp, out edges[index]); |
---|
310 | } |
---|
311 | last = edges[vertexes.Length - 1]; |
---|
312 | for (int index = 0; index < vertexes.Length; ++index, last = current) |
---|
313 | { |
---|
314 | current = edges[index]; |
---|
315 | Vector2D.Add(ref current, ref last, out temp); |
---|
316 | Vector2D.Normalize(ref temp, out normals[index + offset]); |
---|
317 | } |
---|
318 | } |
---|
319 | |
---|
320 | |
---|
321 | public static Vector2D[] GetVertexNormals(Vector2D[] vertexes) |
---|
322 | { |
---|
323 | if (vertexes == null) { throw new ArgumentNullException("vertexes"); } |
---|
324 | Vector2D[] result = new Vector2D[vertexes.Length]; |
---|
325 | CalculateNormals(vertexes, result, 0); |
---|
326 | return result; |
---|
327 | } |
---|
328 | public static Vector2D[][] GetVertexNormalsOfRange(Vector2D[][] polygons) |
---|
329 | { |
---|
330 | if (polygons == null) { throw new ArgumentNullException("polygons"); } |
---|
331 | Vector2D[][] result = new Vector2D[polygons.Length][]; |
---|
332 | for (int index = 0; index < polygons.Length; ++index) |
---|
333 | { |
---|
334 | result[index] = GetVertexNormals(polygons[index]); |
---|
335 | } |
---|
336 | return result; |
---|
337 | } |
---|
338 | |
---|
339 | |
---|
340 | |
---|
341 | public static Vector2D[] GetIntersection(Vector2D[] vertexes, Line line) |
---|
342 | { |
---|
343 | Scalar[] distances = new Scalar[vertexes.Length]; |
---|
344 | for (int index = 0; index < vertexes.Length; ++index) |
---|
345 | { |
---|
346 | line.GetDistance(ref vertexes[index], out distances[index]); |
---|
347 | } |
---|
348 | Scalar lastDistance = distances[distances.Length - 1]; |
---|
349 | Vector2D lastVertex = vertexes[vertexes.Length - 1]; |
---|
350 | Vector2D vertex; |
---|
351 | Scalar distance; |
---|
352 | List<Vector2D> result = new List<Vector2D>(vertexes.Length + 1); |
---|
353 | for (int index = 0; index < vertexes.Length; ++index, lastVertex = vertex, lastDistance = distance) |
---|
354 | { |
---|
355 | vertex = vertexes[index]; |
---|
356 | distance = distances[index]; |
---|
357 | if (Math.Abs(Math.Sign(distance) - Math.Sign(lastDistance)) == 2) |
---|
358 | { |
---|
359 | Scalar lastABS = Math.Abs(lastDistance); |
---|
360 | Scalar total = (lastABS + Math.Abs(distance)); |
---|
361 | Scalar percent = lastABS / total; |
---|
362 | Vector2D intersection; |
---|
363 | Vector2D.Lerp(ref lastVertex, ref vertex, ref percent, out intersection); |
---|
364 | result.Add(intersection); |
---|
365 | } |
---|
366 | if (distance >= 0) |
---|
367 | { |
---|
368 | result.Add(vertex); |
---|
369 | } |
---|
370 | } |
---|
371 | return result.ToArray(); |
---|
372 | } |
---|
373 | |
---|
374 | /// <summary> |
---|
375 | /// makes sure the distance between 2 vertexes is under the length passed, by adding vertexes between them. |
---|
376 | /// </summary> |
---|
377 | /// <param name="vertexes">the original vertexes.</param> |
---|
378 | /// <param name="maxLength">the maximum distance allowed between 2 vertexes</param> |
---|
379 | /// <returns>The new vertexes.</returns> |
---|
380 | public static Vector2D[] Subdivide(Vector2D[] vertexes, Scalar maxLength) |
---|
381 | { |
---|
382 | return Subdivide(vertexes, maxLength, true); |
---|
383 | } |
---|
384 | /// <summary> |
---|
385 | /// makes sure the distance between 2 vertexes is under the length passed, by adding vertexes between them. |
---|
386 | /// </summary> |
---|
387 | /// <param name="vertexes">the original vertexes.</param> |
---|
388 | /// <param name="maxLength">the maximum distance allowed between 2 vertexes</param> |
---|
389 | /// <param name="loop">if it should check the distance between the first and last vertex.</param> |
---|
390 | /// <returns>The new vertexes.</returns> |
---|
391 | public static Vector2D[] Subdivide(Vector2D[] vertexes, Scalar maxLength, bool loop) |
---|
392 | { |
---|
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; |
---|
398 | if (loop) |
---|
399 | { |
---|
400 | prevNode = list.Last; |
---|
401 | node = list.First; |
---|
402 | } |
---|
403 | else |
---|
404 | { |
---|
405 | prevNode = list.First; |
---|
406 | node = prevNode.Next; |
---|
407 | } |
---|
408 | for (; node != null; |
---|
409 | prevNode = node, |
---|
410 | node = node.Next) |
---|
411 | { |
---|
412 | Vector2D edge = node.Value - prevNode.Value; |
---|
413 | Scalar mag = edge.Magnitude; |
---|
414 | if (mag > maxLength) |
---|
415 | { |
---|
416 | int count = (int)Math.Ceiling(mag / maxLength); |
---|
417 | mag = 1f / count; |
---|
418 | Vector2D.Multiply(ref edge, ref mag, out edge); |
---|
419 | for (int pos = 1; pos < count; ++pos) |
---|
420 | { |
---|
421 | prevNode = list.AddAfter(prevNode, edge + prevNode.Value); |
---|
422 | } |
---|
423 | } |
---|
424 | } |
---|
425 | Vector2D[] result = new Vector2D[list.Count]; |
---|
426 | list.CopyTo(result, 0); |
---|
427 | return result; |
---|
428 | } |
---|
429 | /// <summary> |
---|
430 | /// Reduces a Polygon's number of vertexes. |
---|
431 | /// </summary> |
---|
432 | /// <param name="vertexes">The Polygon to reduce.</param> |
---|
433 | /// <returns>The reduced vertexes.</returns> |
---|
434 | public static Vector2D[] Reduce(Vector2D[] vertexes) |
---|
435 | { |
---|
436 | return Reduce(vertexes, 0); |
---|
437 | } |
---|
438 | /// <summary> |
---|
439 | /// Reduces a Polygon's number of vertexes. |
---|
440 | /// </summary> |
---|
441 | /// <param name="vertexes">The Polygon to reduce.</param> |
---|
442 | /// <param name="areaTolerance"> |
---|
443 | /// The amount the removal of a vertex is allowed to change the area of the polygon. |
---|
444 | /// (Setting this value to 0 will reverse what the Subdivide method does) |
---|
445 | /// </param> |
---|
446 | /// <returns>The reduced vertexes.</returns> |
---|
447 | public static Vector2D[] Reduce(Vector2D[] vertexes, Scalar areaTolerance) |
---|
448 | { |
---|
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); |
---|
453 | Vector2D v1, v2, v3; |
---|
454 | Scalar old1, old2, new1; |
---|
455 | v1 = vertexes[vertexes.Length - 2]; |
---|
456 | v2 = vertexes[vertexes.Length - 1]; |
---|
457 | areaTolerance *= 2; |
---|
458 | for (int index = 0; index < vertexes.Length; ++index, v2 = v3) |
---|
459 | { |
---|
460 | if (index == vertexes.Length - 1) |
---|
461 | { |
---|
462 | if (result.Count == 0) { throw new ArgumentOutOfRangeException("areaTolerance", "The Tolerance is too high!"); } |
---|
463 | v3 = result[0]; |
---|
464 | } |
---|
465 | else { v3 = vertexes[index]; } |
---|
466 | Vector2D.ZCross(ref v1, ref v2, out old1); |
---|
467 | Vector2D.ZCross(ref v2, ref v3, out old2); |
---|
468 | Vector2D.ZCross(ref v1, ref v3, out new1); |
---|
469 | if (Math.Abs(new1 - (old1 + old2)) > areaTolerance) |
---|
470 | { |
---|
471 | result.Add(v2); |
---|
472 | v1 = v2; |
---|
473 | } |
---|
474 | } |
---|
475 | return result.ToArray(); |
---|
476 | } |
---|
477 | /// <summary> |
---|
478 | /// Calculates the area of a polygon. |
---|
479 | /// </summary> |
---|
480 | /// <param name="vertexes">The vertexes of the polygon.</param> |
---|
481 | /// <returns>The area.</returns> |
---|
482 | public static Scalar GetArea(Vector2D[] vertexes) |
---|
483 | { |
---|
484 | Scalar result; |
---|
485 | BoundingPolygon.GetArea(vertexes, out result); |
---|
486 | return result; |
---|
487 | } |
---|
488 | /// <summary> |
---|
489 | /// Calculates the Centroid of a polygon. |
---|
490 | /// </summary> |
---|
491 | /// <param name="vertexes">The vertexes of the polygon.</param> |
---|
492 | /// <returns>The Centroid of a polygon.</returns> |
---|
493 | /// <remarks> |
---|
494 | /// This is Also known as Center of Gravity/Mass. |
---|
495 | /// </remarks> |
---|
496 | public static Vector2D GetCentroid(Vector2D[] vertexes) |
---|
497 | { |
---|
498 | Vector2D result; |
---|
499 | BoundingPolygon.GetCentroid(vertexes, out result); |
---|
500 | return result; |
---|
501 | } |
---|
502 | /// <summary> |
---|
503 | /// repositions the polygon so the Centroid is the origin. |
---|
504 | /// </summary> |
---|
505 | /// <param name="vertexes">The vertexes of the polygon.</param> |
---|
506 | /// <returns>The vertexes of the polygon with the Centroid as the Origin.</returns> |
---|
507 | public static Vector2D[] CenterVertexes(Vector2D[] vertexes) |
---|
508 | { |
---|
509 | Vector2D centroid; |
---|
510 | BoundingPolygon.GetCentroid(vertexes, out centroid); |
---|
511 | return OperationHelper.ArrayRefOp<Vector2D, Vector2D, Vector2D>(vertexes, ref centroid, Vector2D.Subtract); |
---|
512 | } |
---|
513 | |
---|
514 | public static Vector2D[][] GetIntersectionOfRange(Vector2D[][] polygons, Line line) |
---|
515 | { |
---|
516 | List<Vector2D[]> submerged = new List<Vector2D[]>(polygons.Length); |
---|
517 | for (int index = 0; index < polygons.Length; ++index) |
---|
518 | { |
---|
519 | Vector2D[] vertexes = GetIntersection(polygons[index], line); |
---|
520 | if (vertexes.Length >= 3) { submerged.Add(vertexes); } |
---|
521 | } |
---|
522 | return submerged.ToArray(); |
---|
523 | } |
---|
524 | public static Scalar GetAreaOfRange(Vector2D[][] polygons) |
---|
525 | { |
---|
526 | if (polygons == null) { throw new ArgumentNullException("polygons"); } |
---|
527 | if (polygons.Length == 0) { throw new ArgumentOutOfRangeException("polygons"); } |
---|
528 | Scalar result = 0; |
---|
529 | Scalar temp; |
---|
530 | Vector2D[] polygon; |
---|
531 | for (int index = 0; index < polygons.Length; ++index) |
---|
532 | { |
---|
533 | polygon = polygons[index]; |
---|
534 | if (polygon == null) { throw new ArgumentNullException("polygons"); } |
---|
535 | BoundingPolygon.GetArea(polygon, out temp); |
---|
536 | result += temp; |
---|
537 | } |
---|
538 | return result; |
---|
539 | } |
---|
540 | public static Vector2D GetCentroidOfRange(Vector2D[][] polygons) |
---|
541 | { |
---|
542 | if (polygons == null) { throw new ArgumentNullException("polygons"); } |
---|
543 | if (polygons.Length == 0) { throw new ArgumentOutOfRangeException("polygons"); } |
---|
544 | |
---|
545 | |
---|
546 | Scalar temp, area, areaTotal; |
---|
547 | Vector2D v1, v2; |
---|
548 | Vector2D[] vertexes; |
---|
549 | areaTotal = 0; |
---|
550 | Vector2D result = Vector2D.Zero; |
---|
551 | for (int index1 = 0; index1 < polygons.Length; ++index1) |
---|
552 | { |
---|
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]; |
---|
557 | area = 0; |
---|
558 | for (int index = 0; index < vertexes.Length; ++index, v1 = v2) |
---|
559 | { |
---|
560 | v2 = vertexes[index]; |
---|
561 | Vector2D.ZCross(ref v1, ref v2, out temp); |
---|
562 | area += temp; |
---|
563 | result.X += ((v1.X + v2.X) * temp); |
---|
564 | result.Y += ((v1.Y + v2.Y) * temp); |
---|
565 | } |
---|
566 | areaTotal += Math.Abs(area); |
---|
567 | } |
---|
568 | temp = 1 / (areaTotal * 3); |
---|
569 | result.X *= temp; |
---|
570 | result.Y *= temp; |
---|
571 | return result; |
---|
572 | } |
---|
573 | public static Vector2D[][] CenterVertexesRange(Vector2D[][] polygons) |
---|
574 | { |
---|
575 | if (polygons == null) { throw new ArgumentNullException("polygons"); } |
---|
576 | Vector2D centroid = GetCentroidOfRange(polygons); |
---|
577 | Vector2D[][] result = new Vector2D[polygons.Length][]; |
---|
578 | for (int index = 0; index < polygons.Length; ++index) |
---|
579 | { |
---|
580 | result[index] = OperationHelper.ArrayRefOp<Vector2D, Vector2D, Vector2D>(polygons[index], ref centroid, Vector2D.Subtract); |
---|
581 | } |
---|
582 | return result; |
---|
583 | } |
---|
584 | public static Vector2D[][] ReduceRange(Vector2D[][] polygons) |
---|
585 | { |
---|
586 | return ReduceRange(polygons, 0); |
---|
587 | } |
---|
588 | public static Vector2D[][] ReduceRange(Vector2D[][] polygons, Scalar areaTolerance) |
---|
589 | { |
---|
590 | if (polygons == null) { throw new ArgumentNullException("polygons"); } |
---|
591 | Vector2D[][] result = new Vector2D[polygons.Length][]; |
---|
592 | for (int index = 0; index < polygons.Length; ++index) |
---|
593 | { |
---|
594 | result[index] = Reduce(polygons[index], areaTolerance); |
---|
595 | } |
---|
596 | return result; |
---|
597 | } |
---|
598 | public static Vector2D[][] SubdivideRange(Vector2D[][] polygons, Scalar maxLength) |
---|
599 | { |
---|
600 | return SubdivideRange(polygons, maxLength, true); |
---|
601 | } |
---|
602 | public static Vector2D[][] SubdivideRange(Vector2D[][] polygons, Scalar maxLength, bool loop) |
---|
603 | { |
---|
604 | if (polygons == null) { throw new ArgumentNullException("polygons"); } |
---|
605 | Vector2D[][] result = new Vector2D[polygons.Length][]; |
---|
606 | for (int index = 0; index < polygons.Length; ++index) |
---|
607 | { |
---|
608 | result[index] = Subdivide(polygons[index], maxLength, loop); |
---|
609 | } |
---|
610 | return result; |
---|
611 | } |
---|
612 | |
---|
613 | } |
---|
614 | } |
---|