Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

geometry.go 607B

12345678910111213141516171819202122232425262728
  1. package common
  2. import (
  3. "fmt"
  4. )
  5. // Point represents a point on a 2D grid.
  6. type Point struct {
  7. X, Y int64
  8. }
  9. // Plus returns a new Point which is the sum of this point and the other point.
  10. func (p Point) Plus(p2 Point) Point {
  11. return Point{
  12. X: p.X + p2.X,
  13. Y: p.Y + p2.Y,
  14. }
  15. }
  16. // String returns a user-friendly string for debugging purposes.
  17. func (p Point) String() string {
  18. return fmt.Sprintf("(%d, %d)", p.X, p.Y)
  19. }
  20. // Manhattan calculates the Manhattan distance between this point and the other point.
  21. func (p Point) Manhattan(other Point) int64 {
  22. return Abs(p.X-other.X) + Abs(p.Y-other.Y)
  23. }