point.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package point2d
  2. import (
  3. "fmt"
  4. "math"
  5. )
  6. // A Point 2D
  7. type Point struct {
  8. X int // X Axis (Horizontal)
  9. Y int // Y Axis (Vertical)
  10. }
  11. // Creates a new point at (0, 0)
  12. func NewPoint() *Point {
  13. return &Point{}
  14. }
  15. // Creates a new point given 1-2 integers (more integers are ignored)
  16. func AsPoint(to ...int) *Point {
  17. var p *Point = &Point{}
  18. if len(to) == 1 {
  19. p.X = to[0]
  20. p.Y = to[0]
  21. } else if len(to) >= 2 {
  22. p.X = to[0]
  23. p.Y = to[1]
  24. }
  25. return p
  26. }
  27. // Assignment by 1-2 integers (more integers are ignored)
  28. func (p *Point) Set(to ...int) *Point {
  29. if len(to) == 1 {
  30. p.X = to[0]
  31. p.Y = to[0]
  32. } else if len(to) >= 2 {
  33. p.X = to[0]
  34. p.Y = to[1]
  35. }
  36. return p
  37. }
  38. // Assignment to another Point's location
  39. func (p *Point) SetTo(o *Point) *Point {
  40. p.X = o.X
  41. p.Y = o.Y
  42. return p
  43. }
  44. // Translate by 1-2 integers (more integers are ignored)
  45. func (p *Point) Move(by ...int) *Point {
  46. if len(by) == 1 {
  47. p.X += by[0]
  48. p.Y += by[0]
  49. } else if len(by) >= 2 {
  50. p.X += by[0]
  51. p.Y += by[1]
  52. }
  53. return p
  54. }
  55. // Translate by another Point's location
  56. func (p *Point) MoveBy(o *Point) *Point {
  57. p.X += o.X
  58. p.Y += o.Y
  59. return p
  60. }
  61. // Assigns this Point to (0, 0)
  62. func (p *Point) Zero() *Point {
  63. p.X = 0
  64. p.Y = 0
  65. return p
  66. }
  67. // Flips both the X and Y axis's signs
  68. func (p *Point) Negate() *Point {
  69. p.X = -p.X
  70. p.Y = -p.Y
  71. return p
  72. }
  73. // Makes both X and Y axis's positive
  74. func (p *Point) Abs() *Point {
  75. if p.X < 0 {
  76. p.X = -p.X
  77. }
  78. if p.Y < 0 {
  79. p.Y = -p.Y
  80. }
  81. return p
  82. }
  83. // Makes a copy of the Point's location
  84. func (p *Point) Copy() *Point {
  85. return &Point{p.X, p.Y}
  86. }
  87. // Outputs the Point as "X Y"
  88. func (p *Point) String() string {
  89. return fmt.Sprintf("%d %d", p.X, p.Y)
  90. }
  91. // Compares if the Point is (0, 0)
  92. func (p *Point) IsZero() bool {
  93. return p.X == 0 && p.Y == 0
  94. }
  95. // Compares if the Point's X is the same as given X axis
  96. func (p *Point) IsX(x int) bool {
  97. return p.X == x
  98. }
  99. // Compares if the Point's Y is the same as given Y axis
  100. func (p *Point) IsY(y int) bool {
  101. return p.Y == y
  102. }
  103. // Compares if the Point is equal to the 1-2 integers (more integers are ignored)
  104. func (p *Point) Equal(to ...int) bool {
  105. if len(to) == 1 {
  106. return p.X == to[0] && p.Y == to[0]
  107. } else if len(to) >= 2 {
  108. return p.X == to[0] && p.Y == to[1]
  109. }
  110. return false
  111. }
  112. // Compares the Point to another Point
  113. func (p *Point) EqualTo(o *Point) bool {
  114. return p.X == o.X && p.Y == o.Y
  115. }
  116. // Compares the Point against 1-2 integers (more integers are ignored)
  117. func (p *Point) Less(than ...int) bool {
  118. if len(than) == 1 {
  119. return p.X <= than[0] && p.Y <= than[0]
  120. } else if len(than) >= 2 {
  121. return p.X <= than[0] && p.Y <= than[1]
  122. }
  123. return false
  124. }
  125. // Compares the Point against another Point
  126. func (p *Point) LessThan(o *Point) bool {
  127. return p.X <= o.X && p.Y <= o.Y
  128. }
  129. // Compares the Point against 1-2 integers (more integers are ignored)
  130. func (p *Point) Greater(than ...int) bool {
  131. if len(than) == 1 {
  132. return p.X >= than[0] && p.Y >= than[0]
  133. } else if len(than) >= 2 {
  134. return p.X >= than[0] && p.Y >= than[1]
  135. }
  136. return false
  137. }
  138. // Compares the Point to another Point
  139. func (p *Point) GreaterThan(o *Point) bool {
  140. return p.X >= o.X && p.Y >= o.Y
  141. }
  142. // Compares the Point to see if it's within the 2 other Points
  143. //
  144. // Supports if you gave the parameters backwards
  145. func (p *Point) Within(topLeft, botRight *Point) bool {
  146. if p.EqualTo(topLeft) || p.EqualTo(botRight) {
  147. return true
  148. }
  149. if topLeft.EqualTo(botRight) {
  150. // We didn't match either and they both are the same
  151. return false
  152. }
  153. if topLeft.GreaterThan(botRight) {
  154. topLeft, botRight = botRight, topLeft
  155. }
  156. return (p.X >= topLeft.X && p.X <= botRight.X) && (p.Y >= topLeft.Y && p.Y <= botRight.Y)
  157. }
  158. // The distance from the Point to the assumed location
  159. func (p *Point) Distance(x, y int) int {
  160. a := float64(p.X - x)
  161. b := float64(p.Y - y)
  162. return int(math.Max(math.Abs(a), math.Abs(b)))
  163. }
  164. // The distance from the Point to another Point
  165. func (p *Point) DistanceTo(o *Point) int {
  166. return p.Distance(o.X, o.Y)
  167. }