math

package
v0.0.0-...-f3028fa Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2022 License: MIT Imports: 1 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs(x int) int

func ChebyDist

func ChebyDist(p1, p2 Point) int

Calculates the Chebyshev distance between two points.

func Max

func Max(x, y int) int

func Min

func Min(x, y int) int

func Sgn

func Sgn(x int) int

Types

type Point

type Point struct {
	X, Y int
}

A point in 2D space.

var Origin Point

(0,0). Also serves as the zero value for Point in comparisons.

func Adj

func Adj(pt Point) []Point

func ChebyEdge

func ChebyEdge(r int) []Point

Creates a list of points that have Chebyshev distance == r from (0, 0). The points are ordered, following them will take you from topleft -> bottomleft -> bottomright -> topright.

func Pt

func Pt(x, y int) Point

Shorthand to create a new point.

func (Point) Add

func (p Point) Add(q Point) Point

Returns p + q.

func (Point) In

func (p Point) In(s Shape) bool

Delegates to s.HasPoint -- exists because pt.In(s) is prettier to read than s.HasPoint(p) sometimes.

func (Point) String

func (p Point) String() string

Stringifies a point.

func (Point) Sub

func (p Point) Sub(q Point) Point

Returns p - q

type Rectangle

type Rectangle struct {
	Min, Max Point
}

A rectangle with top-left point Min and top-right point Max, such that it contains points where with Min.X <= p.X < Max.X and Min.Y <= p.Y < Max.Y.

var ZeroRect Rectangle

func Chebyshev

func Chebyshev(r int) Rectangle

Yields rectangle that contains all coordinates of Chebyshev distance <= r from (0, 0). You can iterate over all points like:

points := Chebyshev(3)

for y := points.Min.Y; y < points.Max.Y; y++ {
  for x := points.Min.X; x < points.Max.X; x++ {
      -- do something with Pt(x, y)
  }
}

func Rect

func Rect(min, max Point) Rectangle

func (Rectangle) Center

func (r Rectangle) Center() Point

func (Rectangle) Clip

func (r Rectangle) Clip(pts []Point) []Point

"Filters" the given list of points into a new list that only contains those points which are contained in r.

func (Rectangle) HasPoint

func (r Rectangle) HasPoint(p Point) bool

func (Rectangle) Height

func (r Rectangle) Height() int

func (Rectangle) Intersect

func (r Rectangle) Intersect(s Rectangle) Rectangle

Intersect returns the largest rectangle contained by both r and s. If the two rectangles do not overlap then the zero rectangle will be returned. Stolen from https://golang.org/src/image/geom.go?m=text.

func (Rectangle) String

func (r Rectangle) String() string

func (Rectangle) Width

func (r Rectangle) Width() int

type Shape

type Shape interface {
	HasPoint(p Point) bool
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL