box2.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. // Box2 represents a 2D bounding box defined by two points:
  6. // the point with minimum coordinates and the point with maximum coordinates.
  7. type Box2 struct {
  8. min Vector2
  9. max Vector2
  10. }
  11. // NewBox2 creates and returns a pointer to a new Box2 defined
  12. // by its minimum and maximum coordinates.
  13. func NewBox2(min, max *Vector2) *Box2 {
  14. b := new(Box2)
  15. b.Set(min, max)
  16. return b
  17. }
  18. // Set sets this bounding box minimum and maximum coordinates.
  19. // Returns pointer to this updated bounding box.
  20. func (b *Box2) Set(min, max *Vector2) *Box2 {
  21. if min != nil {
  22. b.min = *min
  23. } else {
  24. b.min.Set(Infinity, Infinity)
  25. }
  26. if max != nil {
  27. b.max = *max
  28. } else {
  29. b.max.Set(-Infinity, -Infinity)
  30. }
  31. return b
  32. }
  33. // SetFromPoints set this bounding box from the specified array of points.
  34. // Returns pointer to this updated bounding box.
  35. func (b *Box2) SetFromPoints(points []*Vector2) *Box2 {
  36. b.MakeEmpty()
  37. for i := 0; i < len(points); i++ {
  38. b.ExpandByPoint(points[i])
  39. }
  40. return b
  41. }
  42. // SetFromCenterAndSize set this bounding box from a center point and size.
  43. // Size is a vector from the minimum point to the maximum point.
  44. // Returns pointer to this updated bounding box.
  45. func (b *Box2) SetFromCenterAndSize(center, size *Vector2) *Box2 {
  46. var v1 Vector2
  47. halfSize := v1.Copy(size).MultiplyScalar(0.5)
  48. b.min.Copy(center).Sub(halfSize)
  49. b.max.Copy(center).Add(halfSize)
  50. return b
  51. }
  52. // Copy copy other to this bounding box.
  53. // Returns pointer to this updated bounding box.
  54. func (b *Box2) Copy(box *Box2) *Box2 {
  55. b.min = box.min
  56. b.max = box.max
  57. return b
  58. }
  59. // MakeEmpty set this bounding box to empty.
  60. // Returns pointer to this updated bounding box.
  61. func (b *Box2) MakeEmpty() *Box2 {
  62. b.min.X = Infinity
  63. b.min.Y = Infinity
  64. b.max.X = -Infinity
  65. b.max.Y = -Infinity
  66. return b
  67. }
  68. // Empty returns if this bounding box is empty.
  69. func (b *Box2) Empty() bool {
  70. return (b.max.X < b.min.X) || (b.max.Y < b.min.Y)
  71. }
  72. // Center calculates the center point of this bounding box and
  73. // stores its pointer to optionalTarget, if not nil, and also returns it.
  74. func (b *Box2) Center(optionalTarget *Vector2) *Vector2 {
  75. var result *Vector2
  76. if optionalTarget == nil {
  77. result = NewVector2(0, 0)
  78. } else {
  79. result = optionalTarget
  80. }
  81. return result.AddVectors(&b.min, &b.max).MultiplyScalar(0.5)
  82. }
  83. // Size calculates the size of this bounding box: the vector from
  84. // its minimum point to its maximum point.
  85. // Store pointer to the calculated size into optionalTarget, if not nil,
  86. // and also returns it.
  87. func (b *Box2) Size(optionalTarget *Vector2) *Vector2 {
  88. var result *Vector2
  89. if optionalTarget == nil {
  90. result = NewVector2(0, 0)
  91. } else {
  92. result = optionalTarget
  93. }
  94. return result.SubVectors(&b.min, &b.max)
  95. }
  96. // ExpandByPoint may expand this bounding box to include the specified point.
  97. // Returns pointer to this updated bounding box.
  98. func (b *Box2) ExpandByPoint(point *Vector2) *Box2 {
  99. b.min.Min(point)
  100. b.max.Max(point)
  101. return b
  102. }
  103. // ExpandByVector expands this bounding box by the specified vector.
  104. // Returns pointer to this updated bounding box.
  105. func (b *Box2) ExpandByVector(vector *Vector2) *Box2 {
  106. b.min.Sub(vector)
  107. b.max.Add(vector)
  108. return b
  109. }
  110. // ExpandByScalar expands this bounding box by the specified scalar.
  111. // Returns pointer to this updated bounding box.
  112. func (b *Box2) ExpandByScalar(scalar float32) *Box2 {
  113. b.min.AddScalar(-scalar)
  114. b.max.AddScalar(scalar)
  115. return b
  116. }
  117. // ContainsPoint returns if this bounding box contains the specified point.
  118. func (b *Box2) ContainsPoint(point *Vector2) bool {
  119. if point.X < b.min.X || point.X > b.max.X ||
  120. point.Y < b.min.Y || point.Y > b.max.Y {
  121. return false
  122. }
  123. return true
  124. }
  125. // ContainsBox returns if this bounding box contains other box.
  126. func (b *Box2) ContainsBox(other *Box2) bool {
  127. if (b.min.X <= other.min.X) && (other.max.X <= b.max.X) &&
  128. (b.min.Y <= other.min.Y) && (other.max.Y <= b.max.Y) {
  129. return true
  130. }
  131. return false
  132. }
  133. // IsIntersectionBox returns if other box intersects this one.
  134. func (b *Box2) IsIntersectionBox(other *Box2) bool {
  135. // using 6 splitting planes to rule out intersections.
  136. if other.max.X < b.min.X || other.min.X > b.max.X ||
  137. other.max.Y < b.min.Y || other.min.Y > b.max.Y {
  138. return false
  139. }
  140. return true
  141. }
  142. // ClampPoint calculates a new point which is the specified point clamped inside this box.
  143. // Stores the pointer to this new point into optionaTarget, if not nil, and also returns it.
  144. func (b *Box2) ClampPoint(point *Vector2, optionalTarget *Vector2) *Vector2 {
  145. var result *Vector2
  146. if optionalTarget == nil {
  147. result = NewVector2(0, 0)
  148. } else {
  149. result = optionalTarget
  150. }
  151. return result.Copy(point).Clamp(&b.min, &b.max)
  152. }
  153. // DistanceToPoint returns the distance from this box to the specified point.
  154. func (b *Box2) DistanceToPoint(point *Vector2) float32 {
  155. v1 := NewVector2(0, 0)
  156. clampedPoint := v1.Copy(point).Clamp(&b.min, &b.max)
  157. return clampedPoint.Sub(point).Length()
  158. }
  159. // Intersect sets this box to the intersection with other box.
  160. // Returns pointer to this updated bounding box.
  161. func (b *Box2) Intersect(other *Box2) *Box2 {
  162. b.min.Max(&other.min)
  163. b.max.Min(&other.max)
  164. return b
  165. }
  166. // Union set this box to the union with other box.
  167. // Returns pointer to this updated bounding box.
  168. func (b *Box2) Union(other *Box2) *Box2 {
  169. b.min.Min(&other.min)
  170. b.max.Max(&other.max)
  171. return b
  172. }
  173. // Translate translates the position of this box by offset.
  174. // Returns pointer to this updated box.
  175. func (b *Box2) Translate(offset *Vector2) *Box2 {
  176. b.min.Add(offset)
  177. b.max.Add(offset)
  178. return b
  179. }
  180. // Equals returns if this box is equal to other
  181. func (b *Box2) Equals(other *Box2) bool {
  182. return other.min.Equals(&b.min) && other.max.Equals(&b.max)
  183. }