Line.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using UnityEngine;
  2. namespace ARLocation
  3. {
  4. public class Line : Curve
  5. {
  6. private Vector3 p0;
  7. private Vector3 p1;
  8. private float distance;
  9. public Vector3 P0
  10. {
  11. get
  12. {
  13. return p0;
  14. }
  15. set
  16. {
  17. p0 = value;
  18. Calculate();
  19. }
  20. }
  21. public Vector3 P1
  22. {
  23. get
  24. {
  25. return p1;
  26. }
  27. set
  28. {
  29. p1 = value;
  30. Calculate();
  31. }
  32. }
  33. public Line(Vector3 p0, Vector3 p1)
  34. {
  35. this.p0 = p0;
  36. this.p1 = p1;
  37. Calculate();
  38. }
  39. public void Calculate()
  40. {
  41. distance = Vector3.Distance(p1, p0);
  42. }
  43. public override float EstimateLength(int n = 100)
  44. {
  45. return distance;
  46. }
  47. public override float GetParameterForLength(float s)
  48. {
  49. return s / distance;
  50. }
  51. public override Vector3 GetPoint(float u)
  52. {
  53. return p0 * (1 - u) + p1 * u;
  54. }
  55. public Vector3 GetTangent(float u)
  56. {
  57. return (p1 - p0) / distance;
  58. }
  59. public override CurvePointData GetPointAndTangent(float u)
  60. {
  61. return new CurvePointData
  62. {
  63. point = GetPoint(u),
  64. tangent = GetTangent(u)
  65. };
  66. }
  67. public override CurvePointData GetPointAndTangentAtLength(float s)
  68. {
  69. var u = GetParameterForLength(s);
  70. return GetPointAndTangent(u);
  71. }
  72. public override Vector3 GetPointAtLength(float s)
  73. {
  74. var u = GetParameterForLength(s);
  75. return GetPoint(u);
  76. }
  77. public override Vector3[] Sample(int n)
  78. {
  79. if (n == 0)
  80. {
  81. return new[] { GetPoint(0), GetPoint(1) };
  82. }
  83. var sample = new Vector3[n + 2];
  84. var delta = 1.0f / (n + 1.0f);
  85. for (var i = 0; i < (n + 2); i++)
  86. {
  87. sample[i] = GetPoint(i * delta);
  88. }
  89. return sample;
  90. }
  91. }
  92. }