vector2.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. // Vector2 is a 2D vector/point with X and Y components.
  6. type Vector2 struct {
  7. X float32
  8. Y float32
  9. }
  10. // NewVector2 creates and returns a pointer to a new Vector2 with
  11. // the specified x and y components
  12. func NewVector2(x, y float32) *Vector2 {
  13. return &Vector2{X: x, Y: y}
  14. }
  15. // Set sets this vector X and Y components.
  16. // Returns the pointer to this updated vector.
  17. func (v *Vector2) Set(x, y float32) *Vector2 {
  18. v.X = x
  19. v.Y = y
  20. return v
  21. }
  22. // SetX sets this vector X component.
  23. // Returns the pointer to this updated Vector.
  24. func (v *Vector2) SetX(x float32) *Vector2 {
  25. v.X = x
  26. return v
  27. }
  28. // SetY sets this vector Y component.
  29. // Returns the pointer to this updated vector.
  30. func (v *Vector2) SetY(y float32) *Vector2 {
  31. v.Y = y
  32. return v
  33. }
  34. // SetComponent sets this vector component value by its index: 0 for X, 1 for Y.
  35. // Returns the pointer to this updated vector
  36. func (v *Vector2) SetComponent(index int, value float32) *Vector2 {
  37. switch index {
  38. case 0:
  39. v.X = value
  40. case 1:
  41. v.Y = value
  42. default:
  43. panic("index is out of range")
  44. }
  45. return v
  46. }
  47. // Component returns this vector component by its index: 0 for X, 1 for Y
  48. func (v *Vector2) Component(index int) float32 {
  49. switch index {
  50. case 0:
  51. return v.X
  52. case 1:
  53. return v.Y
  54. default:
  55. panic("Vector2 index is out of range")
  56. }
  57. }
  58. // Copy copies other vector to this one.
  59. // It is equivalent to: *v = *other.
  60. // Returns the pointer to this updated vector.
  61. func (v *Vector2) Copy(other *Vector2) *Vector2 {
  62. v.X = other.X
  63. v.Y = other.Y
  64. return v
  65. }
  66. // Add adds other vector to this one.
  67. // Returns the pointer to this updated vector.
  68. func (v *Vector2) Add(other *Vector2) *Vector2 {
  69. v.X += other.X
  70. v.Y += other.Y
  71. return v
  72. }
  73. // AddScalar adds the specified scalar to each component of this vector.
  74. // Returns the pointer to this updated vector.
  75. func (v *Vector2) AddScalar(s float32) *Vector2 {
  76. v.X += s
  77. v.Y += s
  78. return v
  79. }
  80. // AddVectors adds vectors a and b to this one.
  81. // Returns the pointer to this updated vector.
  82. func (v *Vector2) AddVectors(a, b *Vector2) *Vector2 {
  83. v.X = a.X + b.X
  84. v.Y = a.Y + b.Y
  85. return v
  86. }
  87. // Sub subtracts other vector from this one.
  88. // Returns the pointer to this updated vector.
  89. func (v *Vector2) Sub(other *Vector2) *Vector2 {
  90. v.X -= other.X
  91. v.Y -= other.Y
  92. return v
  93. }
  94. // SubScalar subtracts scalar s from each component of this vector.
  95. // Returns the pointer to this updated vector.
  96. func (v *Vector2) SubScalar(s float32) *Vector2 {
  97. v.X -= s
  98. v.Y -= s
  99. return v
  100. }
  101. // SubVectors subtracts vectors a and b from this vector.
  102. // Returns the pointer to this updated vector.
  103. func (v *Vector2) SubVectors(a, b *Vector2) *Vector2 {
  104. v.X = a.X - b.X
  105. v.Y = a.Y - b.Y
  106. return v
  107. }
  108. // Multiply multiplies each component of this vector by the corresponding one from other vector.
  109. // Returns the pointer to this updated vector.
  110. func (v *Vector2) Multiply(other *Vector2) *Vector2 {
  111. v.X *= other.X
  112. v.Y *= other.Y
  113. return v
  114. }
  115. // MultiplyScalar multiplies each component of this vector by the scalar s.
  116. // Returns the pointer to this updated vector.
  117. func (v *Vector2) MultiplyScalar(s float32) *Vector2 {
  118. v.X *= s
  119. v.Y *= s
  120. return v
  121. }
  122. // Divide divides each component of this vector by the corresponding one from other vector.
  123. // Returns the pointer to this updated vector
  124. func (v *Vector2) Divide(other *Vector2) *Vector2 {
  125. v.X /= other.X
  126. v.Y /= other.Y
  127. return v
  128. }
  129. // DivideScalar divides each component of this vector by the scalar s.
  130. // If scalar is zero, sets this vector to zero.
  131. // Returns the pointer to this updated vector.
  132. func (v *Vector2) DivideScalar(scalar float32) *Vector2 {
  133. if scalar != 0 {
  134. invScalar := 1 / scalar
  135. v.X *= invScalar
  136. v.Y *= invScalar
  137. } else {
  138. v.X = 0
  139. v.Y = 0
  140. }
  141. return v
  142. }
  143. // Min sets this vector components to the minimum values of itself and other vector.
  144. // Returns the pointer to this updated vector.
  145. func (v *Vector2) Min(other *Vector2) *Vector2 {
  146. if v.X > other.X {
  147. v.X = other.X
  148. }
  149. if v.Y > other.Y {
  150. v.Y = other.Y
  151. }
  152. return v
  153. }
  154. // Max sets this vector components to the maximum value of itself and other vector.
  155. // Returns the pointer to this updated vector.
  156. func (v *Vector2) Max(other *Vector2) *Vector2 {
  157. if v.X < other.X {
  158. v.X = other.X
  159. }
  160. if v.Y < other.Y {
  161. v.Y = other.Y
  162. }
  163. return v
  164. }
  165. // Clamp sets this vector components to be no less than the corresponding components of min
  166. // and not greater than the corresponding component of max.
  167. // Assumes min < max, if this assumption isn't true it will not operate correctly.
  168. // Returns the pointer to this updated vector.
  169. func (v *Vector2) Clamp(min, max *Vector2) *Vector2 {
  170. if v.X < min.X {
  171. v.X = min.X
  172. } else if v.X > max.X {
  173. v.X = max.X
  174. }
  175. if v.Y < min.Y {
  176. v.Y = min.Y
  177. } else if v.Y > max.Y {
  178. v.Y = max.Y
  179. }
  180. return v
  181. }
  182. // ClampScalar sets this vector components to be no less than minVal and not greater than maxVal.
  183. // Returns the pointer to this updated vector.
  184. func (v *Vector2) ClampScalar(minVal, maxVal float32) *Vector2 {
  185. if v.X < minVal {
  186. v.X = minVal
  187. } else if v.X > maxVal {
  188. v.X = maxVal
  189. }
  190. if v.Y < minVal {
  191. v.Y = minVal
  192. } else if v.Y > maxVal {
  193. v.Y = maxVal
  194. }
  195. return v
  196. }
  197. // Floor applies math32.Floor() to each of this vector's components.
  198. // Returns the pointer to this updated vector.
  199. func (v *Vector2) Floor() *Vector2 {
  200. v.X = Floor(v.X)
  201. v.Y = Floor(v.Y)
  202. return v
  203. }
  204. // Ceil applies math32.Ceil() to each of this vector's components.
  205. // Returns the pointer to this updated vector.
  206. func (v *Vector2) Ceil() *Vector2 {
  207. v.X = Ceil(v.X)
  208. v.Y = Ceil(v.Y)
  209. return v
  210. }
  211. // Round rounds each of this vector's components.
  212. // Returns the pointer to this updated vector.
  213. func (v *Vector2) Round() *Vector2 {
  214. v.X = Floor(v.X + 0.5)
  215. v.Y = Floor(v.Y + 0.5)
  216. return v
  217. }
  218. // Negate negates each of this vector's components.
  219. // Returns the pointer to this updated vector.
  220. func (v *Vector2) Negate() *Vector2 {
  221. v.X = -v.X
  222. v.Y = -v.Y
  223. return v
  224. }
  225. // Dot returns the dot product of this vector with other.
  226. // None of the vectors are changed.
  227. func (v *Vector2) Dot(other *Vector2) float32 {
  228. return v.X*other.X + v.Y*other.Y
  229. }
  230. // LengthSq returns the length squared of this vector.
  231. // LengthSq can be used to compare vectors' lengths without the need to perform a square root.
  232. func (v *Vector2) LengthSq() float32 {
  233. return v.X*v.X + v.Y*v.Y
  234. }
  235. // Length returns the length of this vector.
  236. func (v *Vector2) Length() float32 {
  237. return Sqrt(v.X*v.X + v.Y*v.Y)
  238. }
  239. // Normalize normalizes this vector so its length will be 1.
  240. // Returns the pointer to this updated vector.
  241. func (v *Vector2) Normalize() *Vector2 {
  242. return v.DivideScalar(v.Length())
  243. }
  244. // DistanceTo returns the distance of this point to other.
  245. func (v *Vector2) DistanceTo(other *Vector2) float32 {
  246. return Sqrt(v.DistanceToSquared(other))
  247. }
  248. // DistanceToSquared returns the distance squared of this point to other.
  249. func (v *Vector2) DistanceToSquared(other *Vector2) float32 {
  250. dx := v.X - other.X
  251. dy := v.Y - other.Y
  252. return dx*dx + dy*dy
  253. }
  254. // SetLength sets this vector to have the specified length.
  255. // Returns the pointer to this updated vector.
  256. func (v *Vector2) SetLength(l float32) *Vector2 {
  257. oldLength := v.Length()
  258. if oldLength != 0 && l != oldLength {
  259. v.MultiplyScalar(l / oldLength)
  260. }
  261. return v
  262. }
  263. // Lerp sets each of this vector's components to the linear interpolated value of
  264. // alpha between ifself and the corresponding other component.
  265. // Returns the pointer to this updated vector.
  266. func (v *Vector2) Lerp(other *Vector2, alpha float32) *Vector2 {
  267. v.X += (other.X - v.X) * alpha
  268. v.Y += (other.Y - v.Y) * alpha
  269. return v
  270. }
  271. // Equals returns if this vector is equal to other.
  272. func (v *Vector2) Equals(other *Vector2) bool {
  273. return (other.X == v.X) && (other.Y == v.Y)
  274. }
  275. // FromArray sets this vector's components from the specified array and offset
  276. // Returns the pointer to this updated vector.
  277. func (v *Vector2) FromArray(array []float32, offset int) *Vector2 {
  278. v.X = array[offset]
  279. v.Y = array[offset+1]
  280. return v
  281. }
  282. // ToArray copies this vector's components to array starting at offset.
  283. // Returns the array.
  284. func (this *Vector2) ToArray(array []float32, offset int) []float32 {
  285. array[offset] = this.X
  286. array[offset+1] = this.Y
  287. return array
  288. }