line3.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package math32
  5. // Line3 represents a 3D line segment defined by a start and an end point.
  6. type Line3 struct {
  7. start Vector3
  8. end Vector3
  9. }
  10. // NewLine3 creates and returns a pointer to a new Line3 with the
  11. // specified start and end points.
  12. func NewLine3(start, end *Vector3) *Line3 {
  13. l := new(Line3)
  14. l.Set(start, end)
  15. return l
  16. }
  17. // Set sets this line segment start and end points.
  18. // Returns pointer to this updated line segment.
  19. func (l *Line3) Set(start, end *Vector3) *Line3 {
  20. if start != nil {
  21. l.start = *start
  22. }
  23. if end != nil {
  24. l.end = *end
  25. }
  26. return l
  27. }
  28. // Copy copy other line segment to this one.
  29. // Returns pointer to this updated line segment.
  30. func (l *Line3) Copy(other *Line3) *Line3 {
  31. *l = *other
  32. return l
  33. }
  34. // Center calculates this line segment center point.
  35. // Store its pointer into optionalTarget, if not nil, and also returns it.
  36. func (l *Line3) Center(optionalTarget *Vector3) *Vector3 {
  37. var result *Vector3
  38. if optionalTarget == nil {
  39. result = NewVector3(0, 0, 0)
  40. } else {
  41. result = optionalTarget
  42. }
  43. return result.AddVectors(&l.start, &l.end).MultiplyScalar(0.5)
  44. }
  45. // Delta calculates the vector from the start to end point of this line segment.
  46. // Store its pointer in optionalTarget, if not nil, and also returns it.
  47. func (l *Line3) Delta(optionalTarget *Vector3) *Vector3 {
  48. var result *Vector3
  49. if optionalTarget == nil {
  50. result = NewVector3(0, 0, 0)
  51. } else {
  52. result = optionalTarget
  53. }
  54. return result.SubVectors(&l.end, &l.start)
  55. }
  56. // DistanceSq returns the square of the distance from the start point to the end point.
  57. func (l *Line3) DistanceSq() float32 {
  58. return l.start.DistanceToSquared(&l.end)
  59. }
  60. // Distance returns the distance from the start point to the end point.
  61. func (l *Line3) Distance() float32 {
  62. return l.start.DistanceTo(&l.end)
  63. }
  64. // ApplyMatrix4 applies the specified matrix to this line segment start and end points.
  65. // Returns pointer to this updated line segment.
  66. func (l *Line3) ApplyMatrix4(matrix *Matrix4) *Line3 {
  67. l.start.ApplyMatrix4(matrix)
  68. l.end.ApplyMatrix4(matrix)
  69. return l
  70. }
  71. // Equals returns if this line segement is equal to other.
  72. func (l *Line3) Equals(other *Line3) bool {
  73. return other.start.Equals(&l.start) && other.end.Equals(&l.end)
  74. }
  75. // Clone creates and returns a pointer to a copy of this line segment.
  76. func (l *Line3) Clone() *Line3 {
  77. return NewLine3(&l.start, &l.end)
  78. }