adventofcode/helpers/coordinate.go

224 lines
3.8 KiB
Go
Raw Normal View History

2019-12-10 15:17:50 +00:00
package aoc
import (
"fmt"
"math"
"strings"
)
type Coordinate struct {
X, Y int
}
2019-12-16 23:31:11 +00:00
func NewCoordinate(x, y int) *Coordinate {
return &Coordinate{x, y}
}
2020-12-20 21:57:10 +00:00
func (c Coordinate) Relative(t Coordinate) Coordinate {
return Coordinate{X: c.X + t.X, Y: c.Y + t.Y}
}
2022-12-14 17:45:19 +00:00
func (c *Coordinate) MoveSouth() { c.Y++ }
func (c *Coordinate) MoveNorth() { c.Y-- }
func (c *Coordinate) MoveEast() { c.X++ }
func (c *Coordinate) MoveWest() { c.X-- }
func (c *Coordinate) MoveNE() {
c.X++
c.Y--
}
2024-12-04 14:14:00 +00:00
2022-12-14 17:45:19 +00:00
func (c *Coordinate) MoveSE() {
c.X++
c.Y++
}
2024-12-04 14:14:00 +00:00
2022-12-14 17:45:19 +00:00
func (c *Coordinate) MoveSW() {
c.X--
c.Y++
}
2024-12-04 14:14:00 +00:00
2022-12-14 17:45:19 +00:00
func (c *Coordinate) MoveNW() {
c.X--
c.Y--
}
2024-12-04 14:14:00 +00:00
2022-12-27 20:06:28 +00:00
func (c Coordinate) Add(o Coordinate) Coordinate {
return Coordinate{
X: c.X + o.X,
Y: c.Y + o.Y,
}
}
2024-12-04 14:14:00 +00:00
2023-12-18 12:54:10 +00:00
func (c Coordinate) Mul(by int) Coordinate {
return Coordinate{
X: c.X * by,
Y: c.Y * by,
}
}
2022-12-14 17:45:19 +00:00
func (c Coordinate) North() Coordinate {
2020-11-04 17:56:23 +00:00
return Coordinate{X: c.X, Y: c.Y - 1}
}
2024-12-04 14:14:00 +00:00
func (c Coordinate) East() Coordinate {
2020-11-04 17:56:23 +00:00
return Coordinate{X: c.X + 1, Y: c.Y}
}
2024-12-04 14:14:00 +00:00
func (c Coordinate) South() Coordinate {
2020-11-04 17:56:23 +00:00
return Coordinate{X: c.X, Y: c.Y + 1}
}
2024-12-04 14:14:00 +00:00
func (c Coordinate) West() Coordinate {
2020-11-04 17:56:23 +00:00
return Coordinate{X: c.X - 1, Y: c.Y}
}
2024-12-04 14:14:00 +00:00
2020-12-11 14:11:30 +00:00
func (c *Coordinate) NW() Coordinate {
return Coordinate{X: c.X - 1, Y: c.Y - 1}
}
2024-12-04 14:14:00 +00:00
2020-12-11 14:11:30 +00:00
func (c *Coordinate) NE() Coordinate {
return Coordinate{X: c.X + 1, Y: c.Y - 1}
}
2024-12-04 14:14:00 +00:00
2020-12-11 14:11:30 +00:00
func (c *Coordinate) SW() Coordinate {
return Coordinate{X: c.X - 1, Y: c.Y + 1}
}
2024-12-04 14:14:00 +00:00
2020-12-11 14:11:30 +00:00
func (c *Coordinate) SE() Coordinate {
return Coordinate{X: c.X + 1, Y: c.Y + 1}
}
2024-12-04 14:14:00 +00:00
2022-12-13 16:08:27 +00:00
func (c *Coordinate) GetOrthNeighbors() []Coordinate {
return []Coordinate{c.North(), c.East(), c.South(), c.West()}
}
2024-12-04 14:14:00 +00:00
2022-12-27 16:44:20 +00:00
func (c *Coordinate) GetAllNeighbors() []Coordinate {
return []Coordinate{
c.North(), c.NE(),
c.East(), c.SE(),
c.South(), c.SW(),
2024-12-04 14:14:00 +00:00
c.West(), c.NW(),
}
2022-12-27 16:44:20 +00:00
}
2020-11-04 17:56:23 +00:00
2019-12-16 23:31:11 +00:00
func (c *Coordinate) GetNorthCoord() *Coordinate {
return &Coordinate{
X: c.X,
Y: c.Y - 1,
}
}
2024-12-04 14:14:00 +00:00
2019-12-16 23:31:11 +00:00
func (c *Coordinate) GetEastCoord() *Coordinate {
return &Coordinate{
X: c.X + 1,
Y: c.Y,
}
}
2024-12-04 14:14:00 +00:00
2019-12-16 23:31:11 +00:00
func (c *Coordinate) GetSouthCoord() *Coordinate {
return &Coordinate{
X: c.X,
Y: c.Y + 1,
}
}
2024-12-04 14:14:00 +00:00
2019-12-16 23:31:11 +00:00
func (c *Coordinate) GetWestCoord() *Coordinate {
return &Coordinate{
X: c.X - 1,
Y: c.Y,
}
}
2019-12-10 15:17:50 +00:00
func CoordinateFromString(str string) *Coordinate {
c := Coordinate{}
r := strings.NewReader(str)
2019-12-18 22:44:05 +00:00
_, err := fmt.Fscanf(r, "[%d, %d]", &c.X, &c.Y)
2022-12-14 17:45:19 +00:00
if err != nil {
_, err = fmt.Fscanf(r, "%d,%d", &c.X, &c.Y)
}
2019-12-10 15:17:50 +00:00
if err != nil {
panic(err)
}
return &c
}
func (c Coordinate) Angle(t Coordinate) float64 {
ret := math.Atan2(float64(t.X-c.X), float64(c.Y-t.Y)) * 180 / math.Pi
if ret < 0 {
ret = ret + 360
}
return ret
}
func (c Coordinate) String() string {
return fmt.Sprintf("[%d, %d]", c.X, c.Y)
}
func (c Coordinate) Distance(t Coordinate) int {
return ManhattanDistance(c.X, c.Y, t.X, t.Y)
2019-12-10 15:17:50 +00:00
}
2019-12-16 23:31:11 +00:00
func (c Coordinate) Equals(c2 Coordinate) bool {
return c.X == c2.X && c.Y == c2.Y
}
2024-12-04 14:14:00 +00:00
2022-12-09 15:03:12 +00:00
func (c Coordinate) Adjacent(c2 Coordinate) bool {
return c2.Equals(c.North()) ||
c2.Equals(c.NE()) ||
c2.Equals(c.East()) ||
c2.Equals(c.SE()) ||
c2.Equals(c.South()) ||
c2.Equals(c.SW()) ||
c2.Equals(c.West()) ||
c2.Equals(c.NW())
}
2022-12-20 21:04:26 +00:00
func GetHighestY(list ...Coordinate) int {
top := math.MinInt
for i := range list {
if list[i].Y > top {
top = list[i].Y
}
}
return top
}
2024-12-04 14:14:00 +00:00
2022-12-20 21:04:26 +00:00
func GetLowestY(list ...Coordinate) int {
bot := math.MaxInt
for i := range list {
if list[i].Y < bot {
bot = list[i].Y
}
}
return bot
}
2024-12-04 14:14:00 +00:00
2022-12-20 21:04:26 +00:00
func GetLowestX(list ...Coordinate) int {
bot := math.MaxInt
for i := range list {
if list[i].X < bot {
bot = list[i].X
}
}
return bot
}
2024-12-04 14:14:00 +00:00
2022-12-20 21:04:26 +00:00
func GetHighestX(list ...Coordinate) int {
top := math.MinInt
for i := range list {
if list[i].X > top {
top = list[i].X
}
}
return top
}
2023-12-10 16:26:21 +00:00
func CoordListContains(needle Coordinate, haystack []Coordinate) bool {
for _, chk := range haystack {
if chk.Equals(needle) {
return true
}
}
return false
}