SCPolygonUtils.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class SCPolygonUtils
  5. {
  6. public static bool AreVerticesEqual(List<Vector3> firstList, List<Vector3> secondList)
  7. {
  8. if (firstList.Count != secondList.Count)
  9. {
  10. return false;
  11. }
  12. for (int i = 0; i < firstList.Count; i++)
  13. {
  14. if (firstList[i] != secondList[i])
  15. {
  16. return false;
  17. }
  18. }
  19. return true;
  20. }
  21. public static Vector3 GetPolygonGravityPoint(Vector3[] points)
  22. {
  23. var sum_x = 0.0f;
  24. var sum_z = 0.0f;
  25. var sum_area = 0.0f;
  26. var p1 = points[1];
  27. for (var i = 2; i < points.Length; i++)
  28. {
  29. var p2 = points[i];
  30. var area = PolygonArea(points[0], p1, p2);
  31. sum_area += area;
  32. sum_x += (points[0].x + p1.x + p2.x) * area;
  33. sum_z += (points[0].z + p1.z + p2.z) * area;
  34. p1 = p2;
  35. }
  36. var xx = sum_x / sum_area / 3;
  37. var zz = sum_z / sum_area / 3;
  38. return new Vector3(xx, p1.y, zz);
  39. }
  40. private static float PolygonArea(Vector3 p0, Vector3 p1, Vector3 p2)
  41. {
  42. var area = 0.0f;
  43. area = p0.x * p1.z + p1.x * p2.z + p2.x * p0.z - p1.x * p0.z - p2.x * p1.z - p0.x * p2.z;
  44. return area / 2.0f;
  45. }
  46. }