vector2.go 9.8 KB

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