ChainHull.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System.Collections.Generic;
  2. using Microsoft.Xna.Framework;
  3. namespace FarseerPhysics.Common.ConvexHull
  4. {
  5. public static class ChainHull
  6. {
  7. //Andrew's monotone chain 2D convex hull algorithm.
  8. //Copyright 2001, softSurfer (www.softsurfer.com)
  9. /// <summary>
  10. /// Gets the convex hull.
  11. /// </summary>
  12. /// <remarks>
  13. /// http://www.softsurfer.com/Archive/algorithm_0109/algorithm_0109.htm
  14. /// </remarks>
  15. /// <returns></returns>
  16. public static Vertices GetConvexHull(Vertices P)
  17. {
  18. P.Sort(new PointComparer());
  19. Vector2[] H = new Vector2[P.Count];
  20. Vertices res = new Vertices();
  21. int n = P.Count;
  22. int bot, top = -1; // indices for bottom and top of the stack
  23. int i; // array scan index
  24. // Get the indices of points with min x-coord and min|max y-coord
  25. int minmin = 0, minmax;
  26. float xmin = P[0].X;
  27. for (i = 1; i < n; i++)
  28. if (P[i].X != xmin) break;
  29. minmax = i - 1;
  30. if (minmax == n - 1)
  31. {
  32. // degenerate case: all x-coords == xmin
  33. H[++top] = P[minmin];
  34. if (P[minmax].Y != P[minmin].Y) // a nontrivial segment
  35. H[++top] = P[minmax];
  36. H[++top] = P[minmin]; // add polygon endpoint
  37. for (int j = 0; j < top + 1; j++)
  38. {
  39. res.Add(H[j]);
  40. }
  41. return res;
  42. }
  43. top = res.Count - 1;
  44. // Get the indices of points with max x-coord and min|max y-coord
  45. int maxmin, maxmax = n - 1;
  46. float xmax = P[n - 1].X;
  47. for (i = n - 2; i >= 0; i--)
  48. if (P[i].X != xmax) break;
  49. maxmin = i + 1;
  50. // Compute the lower hull on the stack H
  51. H[++top] = P[minmin]; // push minmin point onto stack
  52. i = minmax;
  53. while (++i <= maxmin)
  54. {
  55. // the lower line joins P[minmin] with P[maxmin]
  56. if (MathUtils.Area(P[minmin], P[maxmin], P[i]) >= 0 && i < maxmin)
  57. continue; // ignore P[i] above or on the lower line
  58. while (top > 0) // there are at least 2 points on the stack
  59. {
  60. // test if P[i] is left of the line at the stack top
  61. if (MathUtils.Area(H[top - 1], H[top], P[i]) > 0)
  62. break; // P[i] is a new hull vertex
  63. else
  64. top--; // pop top point off stack
  65. }
  66. H[++top] = P[i]; // push P[i] onto stack
  67. }
  68. // Next, compute the upper hull on the stack H above the bottom hull
  69. if (maxmax != maxmin) // if distinct xmax points
  70. H[++top] = P[maxmax]; // push maxmax point onto stack
  71. bot = top; // the bottom point of the upper hull stack
  72. i = maxmin;
  73. while (--i >= minmax)
  74. {
  75. // the upper line joins P[maxmax] with P[minmax]
  76. if (MathUtils.Area(P[maxmax], P[minmax], P[i]) >= 0 && i > minmax)
  77. continue; // ignore P[i] below or on the upper line
  78. while (top > bot) // at least 2 points on the upper stack
  79. {
  80. // test if P[i] is left of the line at the stack top
  81. if (MathUtils.Area(H[top - 1], H[top], P[i]) > 0)
  82. break; // P[i] is a new hull vertex
  83. else
  84. top--; // pop top point off stack
  85. }
  86. H[++top] = P[i]; // push P[i] onto stack
  87. }
  88. if (minmax != minmin)
  89. H[++top] = P[minmin]; // push joining endpoint onto stack
  90. for (int j = 0; j < top + 1; j++)
  91. {
  92. res.Add(H[j]);
  93. }
  94. return res;
  95. }
  96. #region Nested type: PointComparer
  97. public class PointComparer : Comparer<Vector2>
  98. {
  99. public override int Compare(Vector2 a, Vector2 b)
  100. {
  101. int f = a.X.CompareTo(b.X);
  102. return f != 0 ? f : a.Y.CompareTo(b.Y);
  103. }
  104. }
  105. #endregion
  106. }
  107. }