PDFInternalUtils.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using UnityEngine;
  2. namespace Paroxe.PdfRenderer.Internal
  3. {
  4. public static class PDFInternalUtils
  5. {
  6. public static float CalculateRectTransformIntersectArea(RectTransform a, RectTransform b)
  7. {
  8. Vector3[] worldCorners = new Vector3[4];
  9. a.GetWorldCorners(worldCorners);
  10. Vector2 min = worldCorners[0];
  11. Vector2 max = worldCorners[0];
  12. for (int i = 1; i < 4; ++i)
  13. {
  14. if (worldCorners[i].x < min.x)
  15. min.x = worldCorners[i].x;
  16. if (worldCorners[i].y < min.y)
  17. min.y = worldCorners[i].y;
  18. if (worldCorners[i].x > max.x)
  19. max.x = worldCorners[i].x;
  20. if (worldCorners[i].y > max.y)
  21. max.y = worldCorners[i].y;
  22. }
  23. Rect ra = new Rect(min.x, min.y, max.x - min.x, max.y - min.y);
  24. b.GetWorldCorners(worldCorners);
  25. min = worldCorners[0];
  26. max = worldCorners[0];
  27. for (int i = 1; i < 4; ++i)
  28. {
  29. if (worldCorners[i].x < min.x)
  30. min.x = worldCorners[i].x;
  31. if (worldCorners[i].y < min.y)
  32. min.y = worldCorners[i].y;
  33. if (worldCorners[i].x > max.x)
  34. max.x = worldCorners[i].x;
  35. if (worldCorners[i].y > max.y)
  36. max.y = worldCorners[i].y;
  37. }
  38. Rect rb = new Rect(min.x, min.y, max.x - min.x, max.y - min.y);
  39. float x_overlap = Mathf.Min(ra.xMin + ra.width, rb.xMin + rb.width) - Mathf.Max(ra.xMin, rb.xMin) + 1;
  40. float y_overlap = Mathf.Min(ra.yMin + ra.height, rb.yMin + rb.height) - Mathf.Max(ra.yMin, rb.yMin) + 1;
  41. if (x_overlap <= 0.0f || y_overlap <= 0.0f)
  42. return 0.0f;
  43. return x_overlap * y_overlap;
  44. }
  45. public static float CubicEaseIn(float currentTime, float startingValue, float finalValue, float duration)
  46. {
  47. return finalValue * (currentTime /= duration) * currentTime * currentTime + startingValue;
  48. }
  49. }
  50. }