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
- func Abs(x int) int
- func CellsTouchingSegment(cellSize, start, end I2, touch func(cell I2) bool) bool
- func Length(u, v I2) float64
- func SegmentIntersect(p, q, a, b F2) (float64, bool)
- func Sgn(x int) int
- func SignedArea2(a, b, c I2) int64
- type Direction
- type Edge
- type F2
- func (v F2) Add(w F2) F2
- func (v F2) Arg() float64
- func (v F2) C() (float64, float64)
- func (v F2) Cmul(w F2) F2
- func (v F2) Dir() Direction
- func (v F2) Div(k float64) F2
- func (v F2) Dot(w F2) float64
- func (v F2) I2() I2
- func (v F2) InRect(x0, y0, x1, y1 float64) bool
- func (v F2) Mul(k float64) F2
- func (v F2) Norm() float64
- func (v F2) Normal() F2
- func (v F2) Rot(t float64) F2
- func (v F2) RotAbout(t float64, b F2) F2
- func (v F2) Sub(w F2) F2
- func (v F2) Unit() F2
- type Graph
- func (g *Graph) AddEdge(u, v I2)
- func (g *Graph) AllEdges(f func(I2, I2) bool) bool
- func (g *Graph) AllEdgesFacing(p I2, f func(I2, I2) bool) bool
- func (g *Graph) Blocks(start, end I2) bool
- func (g *Graph) Edges() (edges []Edge)
- func (g *Graph) FullyBlocks(start, end I2) bool
- func (g *Graph) NearestBlock(start, end I2) (I2, bool)
- func (g *Graph) NearestPoint(p I2) (e Edge, q I2)
- func (g *Graph) NumEdges() (n int)
- type I2
- func Div(n, d int) I2
- func FindPath(obstacles, paths *Graph, start, end I2, limits Rect) ([]I2, error)
- func LineIntersectI(p, q, a, b I2) (I2, bool)
- func LineNearestPoint(u, v, p I2) (I2, int64)
- func NewI2(x, y int) I2
- func RectRange(ul, dr I2) []I2
- func SegmentIntersectI(p, q, a, b I2) (I2, bool)
- func SegmentNearestPoint(u, v, p I2) (I2, int64)
- func (v I2) Add(w I2) I2
- func (v I2) Area() int
- func (v I2) C() (int, int)
- func (v I2) C64() (int64, int64)
- func (v I2) ClampHi(e I2) I2
- func (v I2) ClampLo(e I2) I2
- func (v I2) Cmul(w I2) I2
- func (v I2) Div(k int) I2
- func (v I2) Dot(w I2) int64
- func (v I2) EDiv(w I2) I2
- func (v I2) EMul(w I2) I2
- func (v I2) F2() F2
- func (v I2) InRect(ul, dr I2) bool
- func (v I2) Mod(k int) I2
- func (v I2) Mul(k int) I2
- func (v I2) MulDiv(n, d int64) I2
- func (v I2) Normal() I2
- func (v I2) Sgn() I2
- func (v I2) Sub(w I2) I2
- func (v I2) Swap() I2
- type Rect
- type VertexSet
Constants ¶
const ( Left = Direction(iota) Right Up Down )
Direction values.
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 CellsTouchingSegment ¶
CellsTouchingSegment calls touch for every rectangular cell that the line segment (start-end) overlaps, stopping at end or when touch returns false.
func SegmentIntersect ¶
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 SignedArea2 ¶
SignedArea2 returns double the signed area of the triangle abc.
Types ¶
type F2 ¶
type F2 struct{ X, Y float64 }
F2 is a tuple of float64 numbers, (X,Y).
func LineIntersect ¶
LineIntersect finds the intersection of the lines (infinite) through p,q and a,b, or returns false if they are parallel.
func (F2) Cmul ¶
Cmul returns the complex product v * w, where the X components are treated as real and the Y components as imaginary.
func (F2) InRect ¶
InRect tests if v is in the rectangle with topleft corner (x0, y0) and bottomright corner (x1, y1).
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 (*Graph) AllEdgesFacing ¶
AllEdgesFacing calls f for every edge that is somewhat facing p.
func (*Graph) Blocks ¶
Blocks determines if the graph intersects the straight line segment start-end. Only edges facing start are considered.
func (*Graph) FullyBlocks ¶
FullyBlocks determines if the graph intersects the straight line segment start-end, including back-facing edges.
func (*Graph) NearestBlock ¶
NearestBlock returns the intersection with the graph nearest to start.
func (*Graph) NearestPoint ¶
NearestPoint finds the edge, and closest point along that edge, to the query point.
type I2 ¶
type I2 struct{ X, Y int }
I2 is a pair of integers, (X,Y).
func FindPath ¶
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 ¶
LineIntersectI finds the intersection of the lines (infinite) through p,q and a,b, or returns false if they are parallel.
func LineNearestPoint ¶
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 RectRange ¶
RectRange makes a list of integer points contained in the Cartesian product [ul.X, dr.X) * [ul.Y, dr.Y).
func SegmentIntersectI ¶
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 ¶
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) Cmul ¶
Cmul returns the complex product v * w, where the X components are treated as real and the Y components as imaginary.
func (I2) MulDiv ¶
MulDiv does both scalar multiplication and division, and tries to avoid overflow on 32-bit.