Triangulator.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. public class Triangulator
  4. {
  5. private List<Vector2> m_points = new List<Vector2>();
  6. public Triangulator(Vector2[] points)
  7. {
  8. m_points = new List<Vector2>(points);
  9. }
  10. public int[] Triangulate()
  11. {
  12. List<int> indices = new List<int>();
  13. int n = m_points.Count;
  14. if (n < 3)
  15. return indices.ToArray();
  16. int[] V = new int[n];
  17. if (Area() > 0)
  18. {
  19. for (int v = 0; v < n; v++)
  20. V[v] = v;
  21. }
  22. else
  23. {
  24. for (int v = 0; v < n; v++)
  25. V[v] = (n - 1) - v;
  26. }
  27. int nv = n;
  28. int count = 2 * nv;
  29. for (int m = 0, v = nv - 1; nv > 2;)
  30. {
  31. if ((count--) <= 0)
  32. return indices.ToArray();
  33. int u = v;
  34. if (nv <= u)
  35. u = 0;
  36. v = u + 1;
  37. if (nv <= v)
  38. v = 0;
  39. int w = v + 1;
  40. if (nv <= w)
  41. w = 0;
  42. if (Snip(u, v, w, nv, V))
  43. {
  44. int a, b, c, s, t;
  45. a = V[u];
  46. b = V[v];
  47. c = V[w];
  48. indices.Add(a);
  49. indices.Add(b);
  50. indices.Add(c);
  51. m++;
  52. for (s = v, t = v + 1; t < nv; s++, t++)
  53. V[s] = V[t];
  54. nv--;
  55. count = 2 * nv;
  56. }
  57. }
  58. indices.Reverse();
  59. return indices.ToArray();
  60. }
  61. private float Area()
  62. {
  63. int n = m_points.Count;
  64. float A = 0.0f;
  65. for (int p = n - 1, q = 0; q < n; p = q++)
  66. {
  67. Vector2 pval = m_points[p];
  68. Vector2 qval = m_points[q];
  69. A += pval.x * qval.y - qval.x * pval.y;
  70. }
  71. return (A * 0.5f);
  72. }
  73. private bool Snip(int u, int v, int w, int n, int[] V)
  74. {
  75. int p;
  76. Vector2 A = m_points[V[u]];
  77. Vector2 B = m_points[V[v]];
  78. Vector2 C = m_points[V[w]];
  79. if (Mathf.Epsilon > (((B.x - A.x) * (C.y - A.y)) - ((B.y - A.y) * (C.x - A.x))))
  80. return false;
  81. for (p = 0; p < n; p++)
  82. {
  83. if ((p == u) || (p == v) || (p == w))
  84. continue;
  85. Vector2 P = m_points[V[p]];
  86. if (InsideTriangle(A, B, C, P))
  87. return false;
  88. }
  89. return true;
  90. }
  91. private bool InsideTriangle(Vector2 A, Vector2 B, Vector2 C, Vector2 P)
  92. {
  93. float ax, ay, bx, by, cx, cy, apx, apy, bpx, bpy, cpx, cpy;
  94. float cCROSSap, bCROSScp, aCROSSbp;
  95. ax = C.x - B.x; ay = C.y - B.y;
  96. bx = A.x - C.x; by = A.y - C.y;
  97. cx = B.x - A.x; cy = B.y - A.y;
  98. apx = P.x - A.x; apy = P.y - A.y;
  99. bpx = P.x - B.x; bpy = P.y - B.y;
  100. cpx = P.x - C.x; cpy = P.y - C.y;
  101. aCROSSbp = ax * bpy - ay * bpx;
  102. cCROSSap = cx * apy - cy * apx;
  103. bCROSScp = bx * cpy - by * cpx;
  104. return ((aCROSSbp >= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f));
  105. }
  106. }