box2.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. type Box2 struct {
  6. min Vector2
  7. max Vector2
  8. }
  9. func NewBox2(min, max *Vector2) *Box2 {
  10. this := new(Box2)
  11. this.Set(min, max)
  12. return this
  13. }
  14. func (this *Box2) Set(min, max *Vector2) *Box2 {
  15. if min != nil {
  16. this.min = *min
  17. } else {
  18. this.min.Set(Infinity, Infinity)
  19. }
  20. if max != nil {
  21. this.max = *max
  22. } else {
  23. this.max.Set(-Infinity, -Infinity)
  24. }
  25. return this
  26. }
  27. func (this *Box2) SetFromPoints(points []*Vector2) *Box2 {
  28. this.MakeEmpty()
  29. for i := 0; i < len(points); i++ {
  30. this.ExpandByPoint(points[i])
  31. }
  32. return this
  33. }
  34. func (this *Box2) SetFromCenterAndSize(center, size *Vector2) *Box2 {
  35. var v1 Vector2
  36. halfSize := v1.Copy(size).MultiplyScalar(0.5)
  37. this.min.Copy(center).Sub(halfSize)
  38. this.max.Copy(center).Add(halfSize)
  39. return this
  40. }
  41. func (this *Box2) Copy(box *Box2) *Box2 {
  42. this.min = box.min
  43. this.max = box.max
  44. return this
  45. }
  46. func (this *Box2) MakeEmpty() *Box2 {
  47. this.min.X = Infinity
  48. this.min.Y = Infinity
  49. this.max.X = -Infinity
  50. this.max.Y = -Infinity
  51. return this
  52. }
  53. func (this *Box2) Empty() bool {
  54. return (this.max.X < this.min.X) || (this.max.Y < this.min.Y)
  55. }
  56. func (this *Box2) Center(optionalTarget *Vector2) *Vector2 {
  57. var result *Vector2
  58. if optionalTarget == nil {
  59. result = NewVector2(0, 0)
  60. } else {
  61. result = optionalTarget
  62. }
  63. return result.AddVectors(&this.min, &this.max).MultiplyScalar(0.5)
  64. }
  65. func (this *Box2) Size(optionalTarget *Vector2) *Vector2 {
  66. var result *Vector2
  67. if optionalTarget == nil {
  68. result = NewVector2(0, 0)
  69. } else {
  70. result = optionalTarget
  71. }
  72. return result.SubVectors(&this.min, &this.max)
  73. }
  74. func (this *Box2) ExpandByPoint(point *Vector2) *Box2 {
  75. this.min.Min(point)
  76. this.max.Max(point)
  77. return this
  78. }
  79. func (this *Box2) ExpandByVector(vector *Vector2) *Box2 {
  80. this.min.Sub(vector)
  81. this.max.Add(vector)
  82. return this
  83. }
  84. func (this *Box2) ExpandByScalar(scalar float32) *Box2 {
  85. this.min.AddScalar(-scalar)
  86. this.max.AddScalar(scalar)
  87. return this
  88. }
  89. func (this *Box2) ContainsPoint(point *Vector2) bool {
  90. if point.X < this.min.X || point.X > this.max.X ||
  91. point.Y < this.min.Y || point.Y > this.max.Y {
  92. return false
  93. }
  94. return true
  95. }
  96. func (this *Box2) ContainsBox(box *Box2) bool {
  97. if (this.min.X <= box.min.X) && (box.max.X <= this.max.X) &&
  98. (this.min.Y <= box.min.Y) && (box.max.Y <= this.max.Y) {
  99. return true
  100. }
  101. return false
  102. }
  103. func (this *Box2) GetParameter(point *Vector2, optionalTarget *Vector2) *Vector2 {
  104. var result *Vector2
  105. if optionalTarget == nil {
  106. result = NewVector2(0, 0)
  107. } else {
  108. result = optionalTarget
  109. }
  110. return result.Set(
  111. (point.X-this.min.X)/(this.max.X-this.min.X),
  112. (point.Y-this.min.Y)/(this.max.Y-this.min.Y),
  113. )
  114. }
  115. func (this *Box2) IsIntersectionBox(box *Box2) bool {
  116. // using 6 splitting planes to rule out intersections.
  117. if box.max.X < this.min.X || box.min.X > this.max.X ||
  118. box.max.Y < this.min.Y || box.min.Y > this.max.Y {
  119. return false
  120. }
  121. return true
  122. }
  123. func (this *Box2) ClampPoint(point *Vector2, optionalTarget *Vector2) *Vector2 {
  124. var result *Vector2
  125. if optionalTarget == nil {
  126. result = NewVector2(0, 0)
  127. } else {
  128. result = optionalTarget
  129. }
  130. return result.Copy(point).Clamp(&this.min, &this.max)
  131. }
  132. func (this *Box2) DistanceToPoint(point *Vector2) float32 {
  133. v1 := NewVector2(0, 0)
  134. clampedPoint := v1.Copy(point).Clamp(&this.min, &this.max)
  135. return clampedPoint.Sub(point).Length()
  136. }
  137. func (this *Box2) Intersect(box *Box2) *Box2 {
  138. this.min.Max(&box.min)
  139. this.max.Min(&box.max)
  140. return this
  141. }
  142. func (this *Box2) Union(box *Box2) *Box2 {
  143. this.min.Min(&box.min)
  144. this.max.Max(&box.max)
  145. return this
  146. }
  147. func (this *Box2) Translate(offset *Vector2) *Box2 {
  148. this.min.Add(offset)
  149. this.max.Add(offset)
  150. return this
  151. }
  152. func (this *Box2) Equals(box *Box2) bool {
  153. return box.min.Equals(&this.min) && box.max.Equals(&this.max)
  154. }