vec2.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. package dmmo
  2. import (
  3. "fmt"
  4. )
  5. // A 2d Point/Vector
  6. type Vec2 struct {
  7. // X-Axis (Left <-> Right)
  8. X int64
  9. // Y-Axis (Up <-> Down)
  10. Y int64
  11. }
  12. // Create a new Vector from 0 or 2+ arguments
  13. //
  14. // Rules:
  15. //
  16. // * 0 arguments returns a Vec of (0, 0)
  17. //
  18. // * 1 arguments returns a Vec of (X, X)
  19. //
  20. // * 2+ arguments returns a Vec of (X, Y) : Only the first 2 arguments are used
  21. func NewVec2(axis ...int64) *Vec2 {
  22. if len(axis) == 0 {
  23. return &Vec2{0, 0}
  24. } else if len(axis) == 1 {
  25. return &Vec2{axis[0], axis[0]}
  26. } else {
  27. return &Vec2{axis[0], axis[1]}
  28. }
  29. }
  30. // Returns a copy of the Vec
  31. //
  32. // Because almost all Vec directly modify the Vec, Cloning is needed
  33. func (v *Vec2) Clone() *Vec2 {
  34. if v == nil {
  35. return nil
  36. }
  37. return &Vec2{v.X, v.Y}
  38. }
  39. // Makes X-Axis and Y-Axis Positive
  40. func (v *Vec2) Abs() *Vec2 {
  41. if v == nil {
  42. return nil
  43. }
  44. if v.X < 0 {
  45. v.X = -v.X
  46. }
  47. if v.Y < 0 {
  48. v.Y = -v.Y
  49. }
  50. return v
  51. }
  52. // Flips the sign for X-Axis and Y-Axis
  53. func (v *Vec2) Negate() *Vec2 {
  54. if v == nil {
  55. return nil
  56. }
  57. v.X = -v.X
  58. v.Y = -v.Y
  59. return v
  60. }
  61. // Assigns 0 to 2+ arguments to Vector
  62. //
  63. // Rules:
  64. //
  65. // * 0 arguments will assign Vec to (0, 0)
  66. //
  67. // * 1 arguments will assign Vec to (X, X)
  68. //
  69. // * 2+ arguments will assign Vec to (X, Y) : Only the first 2 arguments are used
  70. func (v *Vec2) Set(axis ...int64) *Vec2 {
  71. if v == nil {
  72. return nil
  73. }
  74. if len(axis) == 0 {
  75. v.X = 0
  76. v.Y = 0
  77. return v
  78. } else if len(axis) == 1 {
  79. v.X = axis[0]
  80. v.Y = axis[0]
  81. return v
  82. } else {
  83. v.X = axis[0]
  84. v.Y = axis[1]
  85. return v
  86. }
  87. }
  88. // Assigns this Vec to another Vec
  89. //
  90. // No other Vec will assign to (0, 0)
  91. func (v *Vec2) SetBy(o *Vec2) *Vec2 {
  92. if v == nil {
  93. return nil
  94. }
  95. if o == nil {
  96. v.X = 0
  97. v.Y = 0
  98. return v
  99. }
  100. v.X = o.X
  101. v.Y = o.Y
  102. return v
  103. }
  104. // Translates Vec by 0 to 2+ arguments
  105. //
  106. // Rules:
  107. //
  108. // * 0 arguments is no operation
  109. //
  110. // * 1 argument for moving Vec (X, X)
  111. //
  112. // * 2+ arguments for moving Vec (X, Y) : Only first 2 arguments used
  113. func (v *Vec2) Add(axis ...int64) *Vec2 {
  114. if v == nil {
  115. return nil
  116. }
  117. if len(axis) == 0 {
  118. return v
  119. } else if len(axis) == 1 {
  120. v.X += axis[0]
  121. v.Y += axis[0]
  122. return v
  123. } else {
  124. v.X += axis[0]
  125. v.Y += axis[1]
  126. return v
  127. }
  128. }
  129. // Translates this Vec by another Vec
  130. func (v *Vec2) AddBy(o *Vec2) *Vec2 {
  131. if v == nil {
  132. return nil
  133. }
  134. if o == nil {
  135. return v
  136. }
  137. v.X += o.X
  138. v.Y += o.Y
  139. return v
  140. }
  141. // Checks Equality of Vec by 0 to 2+ arguments
  142. //
  143. // Rules:
  144. //
  145. // * 0 for checking Vec is (0, 0)
  146. //
  147. // * 1 for checking Vec is (X, X)
  148. //
  149. // * 2+ for checking Vec is (X, Y) : Only first 2 arguments used
  150. func (v *Vec2) Eq(axis ...int64) bool {
  151. if v == nil {
  152. return len(axis) == 0
  153. }
  154. if len(axis) == 0 {
  155. return v.X == 0 && v.Y == 0
  156. } else if len(axis) == 1 {
  157. return v.X == axis[0] && v.Y == axis[0]
  158. } else {
  159. return v.X == axis[0] && v.Y == axis[1]
  160. }
  161. }
  162. // Checks this Vec is equal to another Vec
  163. //
  164. // When no other Vec provided checks against (0, 0)
  165. func (v *Vec2) EqTo(o *Vec2) bool {
  166. if v == nil {
  167. return o == nil
  168. }
  169. if o == nil {
  170. return v.X == 0 && v.Y == 0
  171. }
  172. return v.X == o.X && v.Y == o.Y
  173. }
  174. // Less Than or Equal To (<=)
  175. //
  176. // Rules:
  177. //
  178. // * 0, Vec <= (0, 0)
  179. //
  180. // * 1, Vec <= (X, X)
  181. //
  182. // * 2+, Vec <= (X, Y) : Only first 2 arguments used
  183. func (v *Vec2) Less(axis ...int64) bool {
  184. if v == nil {
  185. return false
  186. }
  187. if len(axis) == 0 {
  188. return v.X <= 0 && v.Y <= 0
  189. } else if len(axis) == 1 {
  190. return v.X <= axis[0] && v.Y <= axis[0]
  191. } else {
  192. return v.X <= axis[0] && v.Y <= axis[1]
  193. }
  194. }
  195. // Greater Than or Equal To (>=)
  196. //
  197. // Rules:
  198. //
  199. // * 0, Vec >= (0, 0)
  200. //
  201. // * 1, Vec >= (X, X)
  202. //
  203. // * 2+, Vec >= (X, Y) : Only first 2 arguments used
  204. func (v *Vec2) Great(axis ...int64) bool {
  205. if v == nil {
  206. return false
  207. }
  208. if len(axis) == 0 {
  209. return v.X >= 0 && v.Y >= 0
  210. } else if len(axis) == 1 {
  211. return v.X >= axis[0] && v.Y >= axis[0]
  212. } else {
  213. return v.X >= axis[0] && v.Y >= axis[1]
  214. }
  215. }
  216. // Less Than or Equal to (<=)
  217. //
  218. // Compares this Vec to another Vec
  219. func (v *Vec2) LessThan(o *Vec2) bool {
  220. if v == nil {
  221. return false
  222. }
  223. if o == nil {
  224. return v.Less()
  225. }
  226. return v.Less(o.X, o.Y)
  227. }
  228. // Greater Than or Equal to (>=)
  229. //
  230. // Compares this Vec to another Vec
  231. func (v *Vec2) GreatThan(o *Vec2) bool {
  232. if v == nil {
  233. return false
  234. }
  235. if o == nil {
  236. return v.Great()
  237. }
  238. return v.Great(o.X, o.Y)
  239. }
  240. // Casts a Vec into a single integer
  241. //
  242. // X + Y
  243. func (v *Vec2) Int64() int64 {
  244. if v == nil {
  245. return 0
  246. }
  247. return v.X + v.Y
  248. }
  249. // Casts a Vec to string
  250. //
  251. // "(X, Y)"
  252. func (v *Vec2) String() string {
  253. if v == nil {
  254. return "(0, 0)"
  255. }
  256. return fmt.Sprintf("(%d, %d)", v.X, v.Y)
  257. }
  258. // Distance from Vec to 0-2+ arguments
  259. //
  260. // Rules:
  261. //
  262. // * 0, Vec distance to (0, 0)
  263. //
  264. // * 1, Vec distance to (X, X)
  265. //
  266. // * 2+, Vec distance to (X, Y) : Only first 2 arguments used
  267. func (v *Vec2) Distance(axis ...int64) *Vec2 {
  268. if v == nil {
  269. return nil
  270. }
  271. if len(axis) == 0 {
  272. return v.Clone().Abs()
  273. } else if len(axis) == 1 {
  274. o := v.Clone()
  275. o.X -= axis[0]
  276. o.Y -= axis[0]
  277. return o.Abs()
  278. } else {
  279. o := v.Clone()
  280. o.X -= axis[0]
  281. o.Y -= axis[1]
  282. return o.Abs()
  283. }
  284. }
  285. // Distance from Vec to another Vec
  286. //
  287. // When no other Vec provided, Vec distance to (0, 0)
  288. func (v *Vec2) DistanceTo(o *Vec2) *Vec2 {
  289. if v == nil {
  290. return nil
  291. }
  292. if o == nil {
  293. return v.Distance()
  294. }
  295. return v.Distance(o.X, o.Y)
  296. }