vector2.go 9.5 KB

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