Triangulator.cs 3.2 KB

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