common

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2019 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Up is a direction
	Up = Direction(0)
	// Right is a direction
	Right = Direction(1)
	// Down is a direction
	Down = Direction(2)
	// Left is a direction
	Left = Direction(3)
)

Variables

View Source
var (
	// ErrNoNodesLoaded is returned when a path is called before any nodes are set
	ErrNoNodesLoaded = fmt.Errorf("no nodes are loaded")
	// ErrRouteNotFound is returned when all options are exhausted
	ErrRouteNotFound = fmt.Errorf("route not found")
)
View Source
var ZV = &Vector{0, 0}

ZV is a zero vector

Functions

This section is empty.

Types

type Direction added in v0.0.9

type Direction int

Direction represents a direction

func (Direction) String added in v0.0.9

func (d Direction) String() string

type Edge added in v0.0.9

type Edge struct {
	Dest   *Node
	Action Direction
	// contains filtered or unexported fields
}

Edge represents an edge of an node

func (*Edge) String added in v0.0.9

func (e *Edge) String() string

type Item added in v0.0.9

type Item struct {
	// contains filtered or unexported fields
}

Item represents a node item

type Node added in v0.0.9

type Node struct {
	X, Y       int
	IsCollider bool
	Cost       float64
}

Node represents an element in the grid

func (*Node) Copy added in v0.0.9

func (n *Node) Copy() *Node

Copy creates a copy of node

func (*Node) Heuristic added in v0.0.9

func (n *Node) Heuristic(goalX, goalY int) float64

Heuristic is a euclidean norm

func (*Node) String added in v0.0.9

func (n *Node) String() string

func (*Node) Success added in v0.0.9

func (n *Node) Success(goalX, goalY int) bool

Success returns true when we meet a goal

type Path added in v0.0.9

type Path struct {
	// contains filtered or unexported fields
}

Path represents a navigation mesh

func NewPath added in v0.0.9

func NewPath() (p *Path)

NewPath returns a new path system

func (*Path) Neighbors added in v0.0.9

func (p *Path) Neighbors(nodeX int, nodeY int) (neighbors []*Edge)

Neighbors returns edge neighbors of a position

func (*Path) NewNode added in v0.0.9

func (p *Path) NewNode(ix int, iy int, isCollider bool, cost float64)

NewNode adds a new node

func (*Path) Node added in v0.0.9

func (p *Path) Node(x int, y int) *Node

Node returns a node at a coordinate

func (*Path) Path added in v0.0.9

func (p *Path) Path(startX int, startY int, endX int, endY int) (path []*Edge, err error)

Path using astar

func (*Path) Route added in v0.0.10

func (p *Path) Route(startX, startY, endX, endY int) ([]*Edge, error)

Route determines a route based on start and end tile poisitions

type PriorityQueue added in v0.0.9

type PriorityQueue []*Item

PriorityQueue is an array of items

func (PriorityQueue) Len added in v0.0.9

func (pq PriorityQueue) Len() int

func (PriorityQueue) Less added in v0.0.9

func (pq PriorityQueue) Less(i, j int) bool

func (*PriorityQueue) Pop added in v0.0.9

func (pq *PriorityQueue) Pop() interface{}

Pop removes an item from a queue

func (*PriorityQueue) Push added in v0.0.9

func (pq *PriorityQueue) Push(x interface{})

Push add an item to a queue

func (PriorityQueue) Swap added in v0.0.9

func (pq PriorityQueue) Swap(i, j int)

type Rectangle

type Rectangle struct {
	Min, Max Vector
}

Rectangle is a 2D rectangle aligned with the axes of the coordinate system. It is defined by two points, Min and Max.

The invariant should hold, that Max's components are greater or equal than Min's components respectively.

func Rect

func Rect(x0 float64, y0 float64, x1 float64, y1 float64) Rectangle

Rect returns a rectangle

func RectImageCopy

func RectImageCopy(srcRect image.Rectangle) *Rectangle

RectImageCopy converts an image rectangle to model rect

func (*Rectangle) Dx

func (r *Rectangle) Dx() float64

Dx returns r's width

func (*Rectangle) Dy

func (r *Rectangle) Dy() float64

Dy returns r's height

func (*Rectangle) String

func (r *Rectangle) String() string

String returns the string representation of the rectangle

type Vector

type Vector struct {
	X, Y float64
}

Vector is a 2D vector type with X and Y coordinates

func NewVectorFromPoint

func NewVectorFromPoint(point image.Point) *Vector

NewVectorFromPoint converts an image point to a vector

func Unit

func Unit(angle float64) *Vector

Unit returns a vector of length 1 facing the given angle.

func Vect

func Vect(x, y float64) Vector

Vect returns a 2D vector based on two points

func (*Vector) Add

func (v *Vector) Add(nv *Vector) *Vector

Add returns the sum of vectors u and v.

func (*Vector) Angle

func (v *Vector) Angle() float64

Angle returns the angle between the vector u and the x-axis. The result is in range [-Pi, Pi].

func (*Vector) Copy

func (v *Vector) Copy() *Vector

Copy creates a copy of provided vector

func (*Vector) Cross

func (v *Vector) Cross(nv *Vector) float64

Cross return the cross product of vector u and v.

func (*Vector) Dot

func (v *Vector) Dot(nv *Vector) float64

Dot returns the dot product of vector u and v.

func (*Vector) Equal

func (v *Vector) Equal(nv *Vector) bool

Equal returns true if both vectors are at the same location

func (*Vector) Len

func (v *Vector) Len() float64

Len returns the length of the vector.

func (*Vector) Lerp

func (v *Vector) Lerp(a *Vector, b *Vector, t float64) *Vector

Lerp returns a linear interpolation between vector a and b.

This function basically returns a point along the line between a and b and t chooses which one. If t is 0, then a will be returned, if t is 1, b will be returned. Anything between 0 and 1 will return the appropriate point between a and b and so on.

func (*Vector) Map

func (v *Vector) Map(f func(float64) float64) *Vector

Map applies the function f to both x and y components of the vector u and returns the modified vector.

u := pixel.V(10.5, -1.5)
v := nv.Map(math.Floor)   // v is Vector(10, -2), both components of u floored

func (*Vector) Normal

func (v *Vector) Normal() *Vector

Normal returns a vector normal to nv. Equivalent to nv.Rotated(math.Pi / 2), but faster.

func (*Vector) Project

func (v *Vector) Project(nv *Vector) *Vector

Project returns a projection (or component) of vector u in the direction of vector v.

Behaviour is undefined if v is a zero vector.

func (*Vector) Rotated

func (v *Vector) Rotated(angle float64) *Vector

Rotated returns the vector u rotated by the given angle in radians.

func (*Vector) Scaled

func (v *Vector) Scaled(s float64) *Vector

Scaled returns the vector u multiplied by c.

func (*Vector) ScaledXY

func (v *Vector) ScaledXY(nv *Vector) *Vector

ScaledXY returns the vector u multiplied by c.

func (*Vector) String

func (v *Vector) String() string

String returns the string representation of the vector

func (*Vector) Sub

func (v *Vector) Sub(nv *Vector) *Vector

Sub returns the difference betweeen vectors u and v.

func (*Vector) Unit

func (v *Vector) Unit() *Vector

Unit returns a vector of length 1 facing the direction of u (has the same angle).

func (*Vector) XY

func (v *Vector) XY() (x, y float64)

XY returns the components of the vector in two return values.

Jump to

Keyboard shortcuts

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