vec

package module
v0.0.0-...-a25763c Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2016 License: Apache-2.0 Imports: 2 Imported by: 2

README

vec

Simple 2D vector math.

Documentation

Overview

Package vec is a simplistic, non-optimised 2D vector math library and some routines for segment intersections and graphs in the plane.

It has two basic types, F2 and I2, representing points in the plane. The components of F2 are float64, and the components of I2 are int. Functions for I2 use int operations and avoid division where possible, though sometimes it is necessary to cast into int64 to avoid overflow.

Index

Constants

View Source
const (
	Left = Direction(iota)
	Right
	Up
	Down
)

Direction values.

View Source
const Epsilon = 0.0000000001

Epsilon is a small quantity used for floating point comparisons. TODO: do it a better way.

Variables

This section is empty.

Functions

func Abs

func Abs(x int) int

Abs is the absolute value of x.

func CellsTouchingSegment

func CellsTouchingSegment(cellSize, start, end I2, touch func(cell I2) bool) bool

CellsTouchingSegment calls touch for every rectangular cell that the line segment (start-end) overlaps, stopping at end or when touch returns false.

func Length

func Length(u, v I2) float64

Length returns the length of u-v.

func SegmentIntersect

func SegmentIntersect(p, q, a, b F2) (float64, bool)

SegmentIntersect tests for the intersection of the line segments p-q, a-b; if there is an intersection it returns how far along a-b the intersection occurs.

func Sgn

func Sgn(x int) int

Sgn is the sign of X (-1, 0, or 1).

func SignedArea2

func SignedArea2(a, b, c I2) int64

SignedArea2 returns double the signed area of the triangle abc.

Types

type Direction

type Direction int

One of the four cardinal directions.

type Edge

type Edge struct {
	U, V I2
}

Edge describes an edge between two I2 vertices.

func (Edge) Length

func (e Edge) Length() float64

Length is the length of the edge.

func (Edge) Reverse

func (e Edge) Reverse() Edge

Reverse is the edge flipped (V->U).

type F2

type F2 struct{ X, Y float64 }

F2 is a tuple of float64 numbers, (X,Y).

func LineIntersect

func LineIntersect(p, q, a, b F2) (F2, bool)

LineIntersect finds the intersection of the lines (infinite) through p,q and a,b, or returns false if they are parallel.

func NewF2

func NewF2(x, y float64) F2

NewF2 is a convenience function for creating an F2.

func Unit

func Unit(t float64) F2

Unit returns the unit vector at angle t.

func (F2) Add

func (v F2) Add(w F2) F2

Add returns v + w.

func (F2) Arg

func (v F2) Arg() float64

Arg returns the angle between the X-axis and the vector.

func (F2) C

func (v F2) C() (float64, float64)

C returns the components of v (X,Y).

func (F2) Cmul

func (v F2) Cmul(w F2) F2

Cmul returns the complex product v * w, where the X components are treated as real and the Y components as imaginary.

func (F2) Dir

func (v F2) Dir() Direction

Dir returns the general direction of v (Up, Down, Left, Right).

func (F2) Div

func (v F2) Div(k float64) F2

Div returns the componentwise division by k.

func (F2) Dot

func (v F2) Dot(w F2) float64

Dot returns the dot product, v dot w.

func (F2) I2

func (v F2) I2() I2

I2 rounds this F2 to an I2, which will generally result in loss of precision.

func (F2) InRect

func (v F2) InRect(x0, y0, x1, y1 float64) bool

InRect tests if v is in the rectangle with topleft corner (x0, y0) and bottomright corner (x1, y1).

func (F2) Mul

func (v F2) Mul(k float64) F2

Mul returns the scalar product k * v.

func (F2) Norm

func (v F2) Norm() float64

Norm returns the length of v (the square root of v dot v).

func (F2) Normal

func (v F2) Normal() F2

Normal returns a vector perpendicular to v of the same length.

func (F2) Rot

func (v F2) Rot(t float64) F2

Rot rotates the vector by the angle t.

func (F2) RotAbout

func (v F2) RotAbout(t float64, b F2) F2

RotAbout rotates the vector by the angle t around the vector b.

func (F2) Sub

func (v F2) Sub(w F2) F2

Sub returns v - w.

func (F2) Unit

func (v F2) Unit() F2

Unit returns the unit vector pointing in the same direction as v.

type Graph

type Graph struct {
	V VertexSet        // vertices
	E map[I2]VertexSet // edges; E[u] = {v: u-v is an edge}
}

Graph is an adjacency-set implementation of a graph.

func NewGraph

func NewGraph() *Graph

NewGraph creates a new empty *Graph.

func (*Graph) AddEdge

func (g *Graph) AddEdge(u, v I2)

AddEdge adds the edge u-v to the graph.

func (*Graph) AllEdges

func (g *Graph) AllEdges(f func(I2, I2) bool) bool

AllEdges runs a function for every edge.

func (*Graph) AllEdgesFacing

func (g *Graph) AllEdgesFacing(p I2, f func(I2, I2) bool) bool

AllEdgesFacing calls f for every edge that is somewhat facing p.

func (*Graph) Blocks

func (g *Graph) Blocks(start, end I2) bool

Blocks determines if the graph intersects the straight line segment start-end. Only edges facing start are considered.

func (*Graph) Edges

func (g *Graph) Edges() (edges []Edge)

Edges returns all the edges as a slice.

func (*Graph) FullyBlocks

func (g *Graph) FullyBlocks(start, end I2) bool

FullyBlocks determines if the graph intersects the straight line segment start-end, including back-facing edges.

func (*Graph) NearestBlock

func (g *Graph) NearestBlock(start, end I2) (I2, bool)

NearestBlock returns the intersection with the graph nearest to start.

func (*Graph) NearestPoint

func (g *Graph) NearestPoint(p I2) (e Edge, q I2)

NearestPoint finds the edge, and closest point along that edge, to the query point.

func (*Graph) NumEdges

func (g *Graph) NumEdges() (n int)

NumEdges counts the number of edges.

type I2

type I2 struct{ X, Y int }

I2 is a pair of integers, (X,Y).

func Div

func Div(n, d int) I2

Div returns (n%d, n/d) as an I2.

func FindPath

func FindPath(obstacles, paths *Graph, start, end I2, limits Rect) ([]I2, error)

FindPath finds a path from start to end, even if start and end are not vertices. The path will only use vertices contained in the limits Rect.

func LineIntersectI

func LineIntersectI(p, q, a, b I2) (I2, bool)

LineIntersectI finds the intersection of the lines (infinite) through p,q and a,b, or returns false if they are parallel.

func LineNearestPoint

func LineNearestPoint(u, v, p I2) (I2, int64)

LineNearestPoint locates the point on the line passing through uv that is closest to p, and returns the point and the square of the distance.

func NewI2

func NewI2(x, y int) I2

NewI2 is a convenience function for creating an I2.

func RectRange

func RectRange(ul, dr I2) []I2

RectRange makes a list of integer points contained in the Cartesian product [ul.X, dr.X) * [ul.Y, dr.Y).

func SegmentIntersectI

func SegmentIntersectI(p, q, a, b I2) (I2, bool)

SegmentIntersectI tests for the intersection of the line segments p-q, a-b. If there is an intersection an approximation of the point of intersection will be returned.

func SegmentNearestPoint

func SegmentNearestPoint(u, v, p I2) (I2, int64)

SegmentNearestPoint locates the point on the line segment uv that is closest to p, and returns the point and the square of the distance.

func (I2) Add

func (v I2) Add(w I2) I2

Add returns v + w.

func (I2) Area

func (v I2) Area() int

Area returns the product of X and Y.

func (I2) C

func (v I2) C() (int, int)

C returns the components of v (X,Y).

func (I2) C64

func (v I2) C64() (int64, int64)

C64 returns the components of v (X,Y) as int64s.

func (I2) ClampHi

func (v I2) ClampHi(e I2) I2

ClampHi returns v, but with components clamped above by components of e.

func (I2) ClampLo

func (v I2) ClampLo(e I2) I2

ClampLo returns v, but with components clamped below by components of e.

func (I2) Cmul

func (v I2) Cmul(w I2) I2

Cmul returns the complex product v * w, where the X components are treated as real and the Y components as imaginary.

func (I2) Div

func (v I2) Div(k int) I2

Div integer-divides both components by k.

func (I2) Dot

func (v I2) Dot(w I2) int64

Dot returns the dot product, v dot w.

func (I2) EDiv

func (v I2) EDiv(w I2) I2

EDiv returns the element-wise quotient of v and w.

func (I2) EMul

func (v I2) EMul(w I2) I2

EMul returns the element-wise product of v and w.

func (I2) F2

func (v I2) F2() F2

F2 casts this I2 to an F2, which may potentially cause loss of precision.

func (I2) InRect

func (v I2) InRect(ul, dr I2) bool

InRect tests if v is in the rectangle ul-dr.

func (I2) Mod

func (v I2) Mod(k int) I2

Mod returns the remainder of both components divided by k.

func (I2) Mul

func (v I2) Mul(k int) I2

Mul returns the scalar product k * v.

func (I2) MulDiv

func (v I2) MulDiv(n, d int64) I2

MulDiv does both scalar multiplication and division, and tries to avoid overflow on 32-bit.

func (I2) Normal

func (v I2) Normal() I2

Normal returns a vector perpendicular to v of the same length.

func (I2) Sgn

func (v I2) Sgn() I2

Sgn returns a "unit-ish" vector (each component is normalised).

func (I2) Sub

func (v I2) Sub(w I2) I2

Sub returns v - w.

func (I2) Swap

func (v I2) Swap() I2

Swap switches x and y components.

type Rect

type Rect struct {
	UL, DR I2
}

func NewRect

func NewRect(x0, y0, x1, y1 int) Rect

func (Rect) C

func (r Rect) C() (x0, y0, x1, y1 int)

func (Rect) Contains

func (r Rect) Contains(p I2) bool

func (Rect) Expand

func (r Rect) Expand(e I2) Rect

func (Rect) Overlaps

func (r Rect) Overlaps(s Rect) bool

func (Rect) Reposition

func (r Rect) Reposition(ul I2) Rect

func (Rect) Resize

func (r Rect) Resize(sz I2) Rect

func (Rect) Size

func (r Rect) Size() I2

func (Rect) Translate

func (r Rect) Translate(p I2) Rect

type VertexSet

type VertexSet map[I2]bool

VertexSet is a set of vertices.

Jump to

Keyboard shortcuts

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