package point2d import ( "fmt" "math" ) // A Point 2D type Point struct { X int // X Axis (Horizontal) Y int // Y Axis (Vertical) } // Creates a new point at (0, 0) func NewPoint() *Point { return &Point{} } // Creates a new point given 1-2 integers (more integers are ignored) func AsPoint(to ...int) *Point { var p *Point = &Point{} if len(to) == 1 { p.X = to[0] p.Y = to[0] } else if len(to) >= 2 { p.X = to[0] p.Y = to[1] } return p } // Assignment by 1-2 integers (more integers are ignored) func (p *Point) Set(to ...int) *Point { if len(to) == 1 { p.X = to[0] p.Y = to[0] } else if len(to) >= 2 { p.X = to[0] p.Y = to[1] } return p } // Assignment to another Point's location func (p *Point) SetTo(o *Point) *Point { p.X = o.X p.Y = o.Y return p } // Translate by 1-2 integers (more integers are ignored) func (p *Point) Move(by ...int) *Point { if len(by) == 1 { p.X += by[0] p.Y += by[0] } else if len(by) >= 2 { p.X += by[0] p.Y += by[1] } return p } // Translate by another Point's location func (p *Point) MoveBy(o *Point) *Point { p.X += o.X p.Y += o.Y return p } // Assigns this Point to (0, 0) func (p *Point) Zero() *Point { p.X = 0 p.Y = 0 return p } // Flips both the X and Y axis's signs func (p *Point) Negate() *Point { p.X = -p.X p.Y = -p.Y return p } // Makes both X and Y axis's positive func (p *Point) Abs() *Point { if p.X < 0 { p.X = -p.X } if p.Y < 0 { p.Y = -p.Y } return p } // Makes a copy of the Point's location func (p *Point) Copy() *Point { return &Point{p.X, p.Y} } // Outputs the Point as "X Y" func (p *Point) String() string { return fmt.Sprintf("%d %d", p.X, p.Y) } // Compares if the Point is (0, 0) func (p *Point) IsZero() bool { return p.X == 0 && p.Y == 0 } // Compares if the Point's X is the same as given X axis func (p *Point) IsX(x int) bool { return p.X == x } // Compares if the Point's Y is the same as given Y axis func (p *Point) IsY(y int) bool { return p.Y == y } // Compares if the Point is equal to the 1-2 integers (more integers are ignored) func (p *Point) Equal(to ...int) bool { if len(to) == 1 { return p.X == to[0] && p.Y == to[0] } else if len(to) >= 2 { return p.X == to[0] && p.Y == to[1] } return false } // Compares the Point to another Point func (p *Point) EqualTo(o *Point) bool { return p.X == o.X && p.Y == o.Y } // Compares the Point against 1-2 integers (more integers are ignored) func (p *Point) Less(than ...int) bool { if len(than) == 1 { return p.X <= than[0] && p.Y <= than[0] } else if len(than) >= 2 { return p.X <= than[0] && p.Y <= than[1] } return false } // Compares the Point against another Point func (p *Point) LessThan(o *Point) bool { return p.X <= o.X && p.Y <= o.Y } // Compares the Point against 1-2 integers (more integers are ignored) func (p *Point) Greater(than ...int) bool { if len(than) == 1 { return p.X >= than[0] && p.Y >= than[0] } else if len(than) >= 2 { return p.X >= than[0] && p.Y >= than[1] } return false } // Compares the Point to another Point func (p *Point) GreaterThan(o *Point) bool { return p.X >= o.X && p.Y >= o.Y } // Compares the Point to see if it's within the 2 other Points // // Supports if you gave the parameters backwards func (p *Point) Within(topLeft, botRight *Point) bool { if p.EqualTo(topLeft) || p.EqualTo(botRight) { return true } if topLeft.EqualTo(botRight) { // We didn't match either and they both are the same return false } if topLeft.GreaterThan(botRight) { topLeft, botRight = botRight, topLeft } return (p.X >= topLeft.X && p.X <= botRight.X) && (p.Y >= topLeft.Y && p.Y <= botRight.Y) } // The distance from the Point to the assumed location func (p *Point) Distance(x, y int) int { a := float64(p.X - x) b := float64(p.Y - y) return int(math.Max(math.Abs(a), math.Abs(b))) } // The distance from the Point to another Point func (p *Point) DistanceTo(o *Point) int { return p.Distance(o.X, o.Y) }