geo

package
v0.0.0-...-96c78b7 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2016 License: MIT Imports: 3 Imported by: 7

Documentation

Overview

Package geo conatins geometric and mathematical types and functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NumGen

type NumGen func() float64

NumGen (Number Generator) is a function that returns a number.

func ConstNum

func ConstNum(n float64) NumGen

ConstNum returns a NumGen that always returns n.

func RandNum

func RandNum(min, max float64) NumGen

RandNum returns a NumGen that returns a uniform random number between min and max.

func RandRadius

func RandRadius(minR, maxR float64) NumGen

RandRadius returns a NumGen that returns a uniform circle radius between minR and maxR.

type Rect

type Rect struct {
	X, Y, W, H float64
}

Rect defines rectangular coordinates.

func RectUnion

func RectUnion(rects []Rect) Rect

RectUnion returns a Rect that contains all the given Rects. An empty list returns a Rect with size 0.

func (Rect) Area

func (r Rect) Area() float64

Area returns the area of the rectangle.

func (Rect) Bottom

func (r Rect) Bottom() float64

Bottom returns the bottom boundary.

func (Rect) BottomLeft

func (r Rect) BottomLeft() (x, y float64)

BottomLeft returns the coordinates of the bottom left corner.

func (Rect) BottomRight

func (r Rect) BottomRight() (x, y float64)

BottomRight returns the coordinates of the bottom right corner.

func (Rect) Center

func (r Rect) Center() (x, y float64)

Center returns the center coordinates.

func (Rect) CenterX

func (r Rect) CenterX() float64

CenterX returns the center x coordinates

func (Rect) CenterY

func (r Rect) CenterY() float64

CenterY returns the center y coordinates.

func (*Rect) Clamp

func (r *Rect) Clamp(bounds Rect)

Clamp moves this Rect so that it is within bounds. If it is too large than it is centered within bounds.

func (Rect) Clamped

func (r Rect) Clamped(bounds Rect) Rect

Clamped returns a new Rect that is moved to be within bounds. If it is too large than it is centered within bounds.

func (Rect) CollideList

func (r Rect) CollideList(others []Rect) int

CollideList returns the index of the first Rect this one collides with, or -1 if it collides with none.

func (Rect) CollideListAll

func (r Rect) CollideListAll(others []Rect) []int

CollideListAll returns a list of indices of the Rects that collide with this one, or an empty list if none.

func (Rect) CollidePoint

func (r Rect) CollidePoint(x, y float64) bool

CollidePoint returns true if the point is within the Rect. A point along the right or bottom edge is not considered inside.

func (Rect) CollideRect

func (r Rect) CollideRect(other Rect) bool

CollideRect returns true if the Rects overlap.

func (Rect) Contains

func (r Rect) Contains(other Rect) bool

Contains returns true if other is completely inside this one.

func (*Rect) Fit

func (r *Rect) Fit(bounds Rect)

Fit modifies this Rect so that it is moved and resized to fit within bounds while maintaining its original aspect ratio.

func (Rect) Fitted

func (r Rect) Fitted(bounds Rect) Rect

Fitted returns a new Rect that is moved and resized to fit within bounds while maintaining its original aspect ratio.

func (Rect) Height

func (r Rect) Height() float64

Height returns the height.

func (*Rect) Inflate

func (r *Rect) Inflate(dw, dh float64)

Inflate keeps the same center but changes the size by the given amount, in place.

func (Rect) Inflated

func (r Rect) Inflated(dw, dh float64) Rect

Inflated returns a new Rect with the same center whose size is chaged by the given amount.

func (Rect) Intersect

func (r Rect) Intersect(other Rect) Rect

Intersect returns a new Rect that marks the area where the two overlap. If there is no intersection the returned Rect will have 0 size.

func (Rect) Left

func (r Rect) Left() float64

Left returns the left boundary.

func (Rect) MidBottom

func (r Rect) MidBottom() (x, y float64)

MidBottom returns the coordinates at the bottom of the rectangle below the center.

func (Rect) MidLeft

func (r Rect) MidLeft() (x, y float64)

MidLeft returns the coordinates at the left of the rectangle in line with the center.

func (Rect) MidRight

func (r Rect) MidRight() (x, y float64)

MidRight returns the coordinates at the right of the rectangle in line with the center.

func (Rect) MidTop

func (r Rect) MidTop() (x, y float64)

MidTop returns the coordinates at the top of the rectangle above the center.

func (*Rect) Move

func (r *Rect) Move(dx, dy float64)

Move moves the Rect by the given offset, in place.

func (Rect) Moved

func (r Rect) Moved(dx, dy float64) Rect

Moved returns a new Rect moved by the given offset relative to this one.

func (*Rect) Normalize

func (r *Rect) Normalize()

Normalize fips the Rect in place if its size is negative.

func (Rect) Normalized

func (r Rect) Normalized() Rect

Normalized returns a fipped but equivalent Rect if its size is negative.

func (Rect) Right

func (r Rect) Right() float64

Right returns the right boundary.

func (*Rect) SetBottom

func (r *Rect) SetBottom(bottom float64)

SetBottom sets the bottom boundary.

func (*Rect) SetBottomLeft

func (r *Rect) SetBottomLeft(x, y float64)

SetBottomLeft set the coordinates of the bottom left corner.

func (*Rect) SetBottomRight

func (r *Rect) SetBottomRight(x, y float64)

SetBottomRight sets the coordinates of the bottom right corner.

func (*Rect) SetCenter

func (r *Rect) SetCenter(x, y float64)

SetCenter sets the center coordinates.

func (*Rect) SetCenterX

func (r *Rect) SetCenterX(x float64)

SetCenterX sets the center x coordinates.

func (*Rect) SetCenterY

func (r *Rect) SetCenterY(y float64)

SetCenterY set the center y coordinates.

func (*Rect) SetHeight

func (r *Rect) SetHeight(h float64)

SetHeight sets the height.

func (*Rect) SetLeft

func (r *Rect) SetLeft(left float64)

SetLeft sets the left boundary.

func (*Rect) SetMidBottom

func (r *Rect) SetMidBottom(x, y float64)

SetMidBottom sets the coordinates at the bottom of the rectangle below the center.

func (*Rect) SetMidLeft

func (r *Rect) SetMidLeft(x, y float64)

SetMidLeft sets the coordinates at the left of the rectangle in line with the center.

func (*Rect) SetMidRight

func (r *Rect) SetMidRight(x, y float64)

SetMidRight sets the coordinates at the right of the rectangle in line with the center.

func (*Rect) SetMidTop

func (r *Rect) SetMidTop(x, y float64)

SetMidTop sets the coordinates at the top of the rectangle above the center.

func (*Rect) SetRight

func (r *Rect) SetRight(right float64)

SetRight sets the right boundary.

func (*Rect) SetSize

func (r *Rect) SetSize(w, h float64)

SetSize sets the width and height.

func (*Rect) SetTop

func (r *Rect) SetTop(top float64)

SetTop sets the top boundary.

func (*Rect) SetTopLeft

func (r *Rect) SetTopLeft(x, y float64)

SetTopLeft sets the coordinates of the top left corner.

func (*Rect) SetTopRight

func (r *Rect) SetTopRight(x, y float64)

SetTopRight sets the coordinates of the top right corner.

func (*Rect) SetWidth

func (r *Rect) SetWidth(w float64)

SetWidth sets the width.

func (Rect) Size

func (r Rect) Size() (w, h float64)

Size returns the width and height.

func (Rect) Top

func (r Rect) Top() (top float64)

Top returns the top boundary.

func (Rect) TopLeft

func (r Rect) TopLeft() (x, y float64)

TopLeft returns the coordinates of the top left corner.

func (Rect) TopRight

func (r Rect) TopRight() (x, y float64)

TopRight returns the coordinates of the top right corner.

func (*Rect) Union

func (r *Rect) Union(other Rect)

Union modifies this Rect to contain both itself and other.

func (Rect) Unioned

func (r Rect) Unioned(other Rect) Rect

Unioned retruns a new Rect that contain both Rects.

func (Rect) Width

func (r Rect) Width() float64

Width returns the width.

type Vec

type Vec struct {
	X, Y float64
}

Vec is a 2D vector. Many of the functions for Vec have two versions, one that modifies the Vec and one that returns a new Vec. Their names follow a convention that is hopefully inuitive. For example, when working with Vec as a value you use v1.Plus(v2) which returns a new value and reads like when working with other value types such as "1 + 2".

func RandVec

func RandVec() Vec

RandVec returns a unit vector in a random direction.

func (*Vec) Add

func (v *Vec) Add(v2 Vec)

Add modifies v to be the sum of v2 and itself.

func (Vec) Angle

func (v Vec) Angle() float64

Angle returns the radians relative to the positive x-axis (counterclockwise in screen coordinates). The returned value is in the range [-π, π).

func (Vec) AngleFrom

func (v Vec) AngleFrom(v2 Vec) float64

AngleFrom returns the radians from v2 to v (counterclockwise in screen coordinates). The returned value is in the range [-π, π).

func (Vec) Dist

func (v Vec) Dist(v2 Vec) float64

Dist returns the distance between the two vectors.

func (Vec) Dist2

func (v Vec) Dist2(v2 Vec) float64

Dist2 returns the distance squared between the two vectors.

func (*Vec) Div

func (v *Vec) Div(n float64)

Div modifies v to be itself divided by n.

func (Vec) DividedBy

func (v Vec) DividedBy(n float64) Vec

DividedBy returns a new vector that is v divided by n.

func (Vec) Dot

func (v Vec) Dot(v2 Vec) float64

Dot returns the dot product between the two vectors.

func (Vec) Equals

func (v Vec) Equals(v2 Vec, e float64) bool

Equals returns true if the corresponding components of the vectors are within the error e.

func (Vec) Len

func (v Vec) Len() float64

Len returns the length of the vector.

func (Vec) Len2

func (v Vec) Len2() float64

Len2 is the length of the vector squared.

func (*Vec) Limit

func (v *Vec) Limit(len float64)

Limit constrains the length of the vector be no greater than len. If the vector is already less than len then no change is made.

func (Vec) Limited

func (v Vec) Limited(len float64) Vec

Limited returns a new vector in the same direction as v with length no greater than len. The vector returned will be equivalent to v if v.Len() <= len.

func (Vec) Minus

func (v Vec) Minus(v2 Vec) Vec

Minus returns a new vector that is the difference of the two vectors.

func (*Vec) Mul

func (v *Vec) Mul(n float64)

Mul modifies v to be itself times n.

func (*Vec) Normalize

func (v *Vec) Normalize()

Normalize modifies v to be of length one in the same direction.

func (Vec) Normalized

func (v Vec) Normalized() Vec

Normalized returns a new vector of length one in the same direction as v.

func (Vec) Plus

func (v Vec) Plus(v2 Vec) Vec

Plus returns a new vector that is the sum of the two vectors.

func (*Vec) Project

func (v *Vec) Project(v2 Vec)

Project modifies v to be the vector that is the projection of v onto v2.

func (Vec) Projected

func (v Vec) Projected(v2 Vec) Vec

Projected return the vetor that is v projected onto v2.

func (*Vec) Rotate

func (v *Vec) Rotate(rad float64)

Rotate rotates the vector (counterclockwise in screen coordinates) by the given radians.

func (Vec) Rotated

func (v Vec) Rotated(rad float64) Vec

Rotated returns a new vector that is equal to this one rotated (counterclockwise in screen coordinates) by the given radians.

func (*Vec) SetLen

func (v *Vec) SetLen(l float64)

SetLen sets the length of the vector. Negative lengths will flip the vectors direction. If v's length is zero then it will remain unchanged.

func (*Vec) Sub

func (v *Vec) Sub(v2 Vec)

Sub modifies v to be the difference between itself and v2.

func (Vec) Times

func (v Vec) Times(n float64) Vec

Times returns a new vector that is v times n.

func (Vec) WithLen

func (v Vec) WithLen(l float64) Vec

WithLen returns a new vector in the same direction as v but with the given length. It v's length is 0 then the zero vector is returned.

type VecGen

type VecGen func() Vec

VecGen (Vector Generator) is a function that returns a vector.

func DynamicVec

func DynamicVec(v *Vec) VecGen

DynamicVec returns a VecGen that always returns a copy of the vector pointed to by v.

func OffsetVec

func OffsetVec(gen VecGen, offset VecGen) VecGen

OffsetVec returns a VecGen that adds offset to gen. For example, if you wanted to use RandCircle as an initial position you might use

OffsetVec(RandVecCircle(5, 10), StaticVec(Vec{X: 100, Y: 100}))

to center the circle at position 100, 100.

func RandVecArc

func RandVecArc(minRadius, maxRadius, minRadians, maxRadians float64) VecGen

RandVecArc returns a VecGen that will generate a random vector within the slice of a circle defined by the parameters. The radians are relative to the +x axis. Negative radii are undefined.

func RandVecCircle

func RandVecCircle(minRadius, maxRadius float64) VecGen

RandVecCircle returns a VecGen that will generate a random vector within the given radii. Negative radii are undefined.

func RandVecRect

func RandVecRect(rect Rect) VecGen

RandVecRect returns a VecGen that will generate a random vector within the given Rect.

func RandVecRects

func RandVecRects(rects []Rect) VecGen

RandVecRects returns a VecGen that will generate a random vector that is uniformly distributed between all the given rects. If the slice given is empty then the zero vector is returned.

func StaticVec

func StaticVec(v Vec) VecGen

StaticVec returns a VecGen that always returns the constant vector v.

Jump to

Keyboard shortcuts

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