| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 | 
							- 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)
 
- }
 
 
  |