vector2.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 Vector2 struct {
  6. X float32
  7. Y float32
  8. }
  9. func NewVector2(x, y float32) *Vector2 {
  10. return &Vector2{X: x, Y: y}
  11. }
  12. func (this *Vector2) Set(x, y float32) *Vector2 {
  13. this.X = x
  14. this.Y = y
  15. return this
  16. }
  17. func (this *Vector2) SetX(x float32) *Vector2 {
  18. this.X = x
  19. return this
  20. }
  21. func (this *Vector2) SetY(y float32) *Vector2 {
  22. this.Y = y
  23. return this
  24. }
  25. func (this *Vector2) SetComponent(index int, value float32) {
  26. switch index {
  27. case 0:
  28. this.X = value
  29. case 1:
  30. this.Y = value
  31. default:
  32. panic("index is out of range")
  33. }
  34. }
  35. func (this *Vector2) GetComponent(index int) float32 {
  36. switch index {
  37. case 0:
  38. return this.X
  39. case 1:
  40. return this.Y
  41. default:
  42. panic("index is out of range: ")
  43. }
  44. }
  45. func (this *Vector2) Copy(v *Vector2) *Vector2 {
  46. this.X = v.X
  47. this.Y = v.Y
  48. return this
  49. }
  50. func (this *Vector2) Add(v *Vector2) *Vector2 {
  51. this.X += v.X
  52. this.Y += v.Y
  53. return this
  54. }
  55. func (this *Vector2) AddScalar(s float32) *Vector2 {
  56. this.X += s
  57. this.Y += s
  58. return this
  59. }
  60. func (this *Vector2) AddVectors(a, b *Vector2) *Vector2 {
  61. this.X = a.X + b.X
  62. this.Y = a.Y + b.Y
  63. return this
  64. }
  65. func (this *Vector2) Sub(v *Vector2) *Vector2 {
  66. this.X -= v.X
  67. this.Y -= v.Y
  68. return this
  69. }
  70. func (this *Vector2) SubScalar(s float32) *Vector2 {
  71. this.X -= s
  72. this.Y -= s
  73. return this
  74. }
  75. func (this *Vector2) SubVectors(a, b *Vector2) *Vector2 {
  76. this.X = a.X - b.X
  77. this.Y = a.Y - b.Y
  78. return this
  79. }
  80. func (this *Vector2) Multiply(v *Vector2) *Vector2 {
  81. this.X *= v.X
  82. this.Y *= v.Y
  83. return this
  84. }
  85. func (this *Vector2) MultiplyScalar(s float32) *Vector2 {
  86. this.X *= s
  87. this.Y *= s
  88. return this
  89. }
  90. func (this *Vector2) Divide(v *Vector2) *Vector2 {
  91. this.X /= v.X
  92. this.Y /= v.Y
  93. return this
  94. }
  95. func (this *Vector2) DivideScalar(scalar float32) *Vector2 {
  96. if scalar != 0 {
  97. invScalar := 1 / scalar
  98. this.X *= invScalar
  99. this.Y *= invScalar
  100. } else {
  101. this.X = 0
  102. this.Y = 0
  103. }
  104. return this
  105. }
  106. func (this *Vector2) Min(v *Vector2) *Vector2 {
  107. if this.X > v.X {
  108. this.X = v.X
  109. }
  110. if this.Y > v.Y {
  111. this.Y = v.Y
  112. }
  113. return this
  114. }
  115. func (this *Vector2) Max(v *Vector2) *Vector2 {
  116. if this.X < v.X {
  117. this.X = v.X
  118. }
  119. if this.Y < v.Y {
  120. this.Y = v.Y
  121. }
  122. return this
  123. }
  124. func (this *Vector2) Clamp(min, max *Vector2) *Vector2 {
  125. // This function assumes min < max, if this assumption isn't true it will not operate correctly
  126. if this.X < min.X {
  127. this.X = min.X
  128. } else if this.X > max.X {
  129. this.X = max.X
  130. }
  131. if this.Y < min.Y {
  132. this.Y = min.Y
  133. } else if this.Y > max.Y {
  134. this.Y = max.Y
  135. }
  136. return this
  137. }
  138. func (this *Vector2) ClampScalar(minVal, maxVal float32) *Vector2 {
  139. min := NewVector2(0, 0)
  140. max := NewVector2(0, 0)
  141. min.Set(minVal, minVal)
  142. max.Set(maxVal, maxVal)
  143. return this.Clamp(min, max)
  144. }
  145. func (this *Vector2) Floor() *Vector2 {
  146. this.X = Floor(this.X)
  147. this.Y = Floor(this.Y)
  148. return this
  149. }
  150. func (this *Vector2) Ceil() *Vector2 {
  151. this.X = Ceil(this.X)
  152. this.Y = Ceil(this.Y)
  153. return this
  154. }
  155. func (this *Vector2) Round() *Vector2 {
  156. // TODO NEED CHECK
  157. this.X = Floor(this.X + 0.5)
  158. this.Y = Floor(this.Y + 0.5)
  159. return this
  160. }
  161. func (this *Vector2) RoundToZero() *Vector2 {
  162. if this.X < 0 {
  163. this.X = Ceil(this.X)
  164. } else {
  165. this.X = Floor(this.X)
  166. }
  167. if this.Y < 0 {
  168. this.Y = Ceil(this.Y)
  169. } else {
  170. this.Y = Floor(this.Y)
  171. }
  172. return this
  173. }
  174. func (this *Vector2) Negate() *Vector2 {
  175. this.X = -this.X
  176. this.Y = -this.Y
  177. return this
  178. }
  179. func (this *Vector2) Dot(v *Vector2) float32 {
  180. return this.X*v.X + this.Y*v.Y
  181. }
  182. func (this *Vector2) LengthSq() float32 {
  183. return this.X*this.X + this.Y*this.Y
  184. }
  185. func (this *Vector2) Length() float32 {
  186. return Sqrt(this.X*this.X + this.Y*this.Y)
  187. }
  188. func (this *Vector2) Normalize() *Vector2 {
  189. return this.DivideScalar(this.Length())
  190. }
  191. func (this *Vector2) DistanceTo(v *Vector2) float32 {
  192. return Sqrt(this.DistanceToSquared(v))
  193. }
  194. func (this *Vector2) DistanceToSquared(v *Vector2) float32 {
  195. dx := this.X - v.X
  196. dy := this.Y - v.Y
  197. return dx*dx + dy*dy
  198. }
  199. func (this *Vector2) SetLength(l float32) *Vector2 {
  200. oldLength := this.Length()
  201. if oldLength != 0 && l != oldLength {
  202. this.MultiplyScalar(l / oldLength)
  203. }
  204. return this
  205. }
  206. func (this *Vector2) Lerp(v *Vector2, alpha float32) *Vector2 {
  207. this.X += (v.X - this.X) * alpha
  208. this.Y += (v.Y - this.Y) * alpha
  209. return this
  210. }
  211. func (this *Vector2) LerpVectors(v1, v2 *Vector2, alpha float32) *Vector2 {
  212. this.SubVectors(v2, v1).MultiplyScalar(alpha).Add(v1)
  213. return this
  214. }
  215. func (this *Vector2) Equals(v *Vector2) bool {
  216. return (v.X == this.X) && (v.Y == this.Y)
  217. }
  218. func (this *Vector2) FromArray(array []float32, offset int) *Vector2 {
  219. this.X = array[offset]
  220. this.Y = array[offset+1]
  221. return this
  222. }
  223. func (this *Vector2) ToArray(array []float32, offset int) []float32 {
  224. array[offset] = this.X
  225. array[offset+1] = this.Y
  226. return array
  227. }
  228. // TODO attribute ???
  229. //func (this *Vector2) FromAttribute(attribute, index, offset) *Vector2 {
  230. //
  231. //
  232. //
  233. //
  234. //}
  235. func (this *Vector2) Close() *Vector2 {
  236. return NewVector2(this.X, this.Y)
  237. }