mat

package
v0.4.7 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package mat focuses on mathematics, in case of more intense operations related to ray cast i left some documentation inside functions in case you want to understand how math here works

missing features: Circle, Polygon

Index

Constants

This section is empty.

Variables

View Source
var (
	Transparent = RGBA{}
	Black       = RGB(0, 0, 0)
	White       = RGB(1, 1, 1)
	Red         = RGB(1, 0, 0)
	Green       = RGB(0, 1, 0)
	Blue        = RGB(0, 0, 1)
)

Color constants

View Source
var ErrInvalidHex = errors.New("byte is not a hex code")

ErrInvalidHex is returned by HexToRGBA if hex string contains non ex characters

View Source
var ErrTooShort = errors.New("hex string is too short (min is 6)")

ErrTooShort is returned by HexToRGBA if hex string is too short to parse a color

View Source
var IM = Mat{Vec{1, 0}, Vec{0, 1}, Vec{0, 0}}

IM is Matrix used as base for all transformations

View Source
var RGBAModel = color.ModelFunc(rgbaModel)

RGBAModel converts colors to RGBA format.

Functions

func Absi

func Absi(a int) int

Absi ...

func Approx

func Approx(a, b float64, precision int) bool

Approx returns whether two floats are same with certain precision

func Clamp

func Clamp(val, min, max float64) float64

Clamp ...

func Lerp

func Lerp(a, b, t float64) float64

Lerp linearly interpolates between two floats

func Maxi

func Maxi(a, b int) int

Maxi ...

func Mini

func Mini(a, b int) int

Mini ...

func Mod

func Mod(x, y float64) float64

Mod is faster version of math.Mod, trade of is that it is accurate only to first 16 significant digits

func Polynomial

func Polynomial(a, b, c float64) (x1, x2 float64)

Polynomial calculates a simple quadratic polynomial

returns zero value if there is no solution as whether poly equals zero can be derived from parameters (b == c == 0 => k == 0 (i can be wrong))

returns h, h where h is solution in case of one

returns h, k in case of two solutions

func Range

func Range(min, max float64) float64

Range returns random number from range

func Round

func Round(v float64, precision int) float64

Round rounds float with to given decimal points

Types

type AABB

type AABB struct {
	Min, Max Vec
}

AABB is a 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.

var ZA AABB

ZA is zero value AABB

func A

func A(minX, minY, maxX, maxY float64) AABB

A returns a new AABB with given the Min and Max coordinates.

Note that the returned rectangle is not automatically normalized.

func Centered

func Centered(c Vec, w, h float64) AABB

Centered returns rect witch center is equal to c, width equal to w, likewise height equal to h

func FromRect

func FromRect(a image.Rectangle) AABB

FromRect converts image.Rectangle to AABB

func Square

func Square(c Vec, size float64) AABB

Square returns AABB with center in c and width and height both equal to size * 2

func ToAABB

func ToAABB(a image.Rectangle) AABB

ToAABB converts image.Rectangle to AABB

func VecBounds

func VecBounds(vectors ...Vec) (base AABB)

VecBounds gets the smallest rectangle in witch all provided points fit in

func (AABB) Area

func (a AABB) Area() float64

Area returns the area of a. If a is not normalized, area may be negative.

func (AABB) Center

func (a AABB) Center() Vec

Center returns the position of the center of the AABB.

func (AABB) Clamp

func (a AABB) Clamp(v Vec) Vec

Clamp clamps Vec inside AABB area

func (AABB) Contains

func (a AABB) Contains(u Vec) bool

Contains checks whether a vector u is contained within this AABB (including it's borders).

func (*AABB) Deco

func (a *AABB) Deco() (left, bottom, right, top float64)

Deco returns edge values

func (AABB) Fits

func (a AABB) Fits(b AABB) bool

Fits reports whether a fits into b so that a.Intersect(b) == a

func (AABB) Flatten

func (a AABB) Flatten() [4]float64

Flatten returns AABB flattened into Array, values are in same order as they would be stored on stack

func (AABB) H

func (a AABB) H() float64

H returns the height of the AABB.

func (AABB) Intersect

func (a AABB) Intersect(s AABB) AABB

Intersect returns the maximal AABB which is covered by both a and s. AABBs a and s must be normalized.

If a and s don't overlap, this function returns a zero-rectangle.

func (AABB) IntersectCircle

func (a AABB) IntersectCircle(c Circ) bool

IntersectsAABB returns whether circle intersects AABB

func (AABB) Intersects

func (a AABB) Intersects(s AABB) bool

Intersects returns whether or not the given AABB intersects at any point with this AABB.

This function is overall about 5x faster than Intersect, so it is better to use if you have no need for the returned AABB from Intersect.

func (AABB) LocalVertices

func (a AABB) LocalVertices() [4]Vec

LocalVertices creates array of vertices relative to center of rect

func (AABB) Moved

func (a AABB) Moved(delta Vec) AABB

Moved returns the AABB moved (both Min and Max) by the given vector delta.

func (*AABB) Mutator

func (a *AABB) Mutator() [4]*float64

Mutator is similar to Iterator but this gives option to mutate state of AABB trough Array Entries

func (AABB) Norm

func (a AABB) Norm() AABB

Norm returns the AABB in normal form, such that Max is component-wise greater or equal than Min.

func (AABB) Resized

func (a AABB) Resized(anchor, size Vec) AABB

Resized returns the AABB resized to the given size while keeping the position of the given anchor.

a.Resized(a.Min, size)      // resizes while keeping the position of the lower-left corner
a.Resized(a.Max, size)      // same with the top-right corner
a.Resized(a.Center(), size) // resizes around the center

func (AABB) ResizedMin

func (a AABB) ResizedMin(size Vec) AABB

ResizedMin returns the AABB resized to the given size while keeping the position of the AABB's Min.

Sizes of zero area are safe here.

func (AABB) Size

func (a AABB) Size() Vec

Size returns the vector of width and height of the AABB.

func (AABB) String

func (a AABB) String() string

String returns the string representation of the AABB.

func (AABB) ToImage

func (a AABB) ToImage() image.Rectangle

ToImage converts AABB to image.AABB

func (AABB) ToVec

func (a AABB) ToVec() Vec

ToVec converts AABB to vec where x is AABB width anc y is rect Height

func (AABB) Union

func (a AABB) Union(s AABB) AABB

Union returns the minimal AABB which covers both a and s. AABBs a and s must be normalized.

func (AABB) Vertices

func (a AABB) Vertices() [4]Vec

Vertices returns a slice of the four corners which make up the rectangle.

func (AABB) W

func (a AABB) W() float64

W returns the width of the AABB.

type Circ

type Circ struct {
	C Vec
	R float64
}

Circ is an circle

func C

func C(x, y, r float64) Circ

C is circle constructor

func (Circ) Approx

func (c Circ) Approx(o Circ, precision int) bool

Approx compers Circles approximately to surthen presision

func (Circ) Contains

func (c Circ) Contains(pos Vec) bool

Contains returns whether circle contains pos

func (Circ) Intersect

func (c Circ) Intersect(s Circ) (k, p Vec)

func (Circ) Intersects

func (c Circ) Intersects(o Circ) bool

Intersects detects intersection between two circles

func (Circ) ProjectX

func (c Circ) ProjectX(x float64) (a, b float64)

ProjectX projects x to y using Polynomial

func (Circ) ProjectY

func (c Circ) ProjectY(y float64) (a, b float64)

ProjectY projects y to x using Polynomial

func (Circ) SimplePrjX

func (c Circ) SimplePrjX(x float64) float64

SimplePrjX performs simple projection x into y on circle

if x cannot be projected NaN is returned

func (Circ) SimplePrjY

func (c Circ) SimplePrjY(y float64) float64

SimplePrjY is analogous to SimplePrjX and projects y to x

if y cannot be projected NaN is returned

func (Circ) String

func (c Circ) String() string

func (Circ) Union

func (c Circ) Union(o Circ) Circ

Union returns smallest possible circle that encloses both circles

type Mat

type Mat struct {
	X, Y, C Vec
}

Mat is matrix used for transformation its a standard 3x3 matrix with setup:

|X.X Y.X C.X| |X.Y Y.Y C.Y| | 0 0 1 |

Structure can tell that X is X-axis direction vector Y si Y-axis direction vector and C is where they intersect IM represents system as it is, if you increase lengths of axis vectors Projected image will increase size, is you rotate both axises, image will be tilted, if you move it Image will get offset. You can combine these how every you like, just mind that order matters.

var ZM Mat

ZM is zero value Mat

func M

func M(pos, scl Vec, rot float64) Mat

M is equivalent to:

IM.ScaledXY(Vec{}, scl).Rotated(Vec{}, rot).Moved(pos)

but 3x faster and shorter and covers mostly all you need

func (Mat) Approx

func (m Mat) Approx(b Mat, precision int) bool

Approx same as normal approx but for matrix

func (Mat) Chained

func (m Mat) Chained(next Mat) Mat

Chained adds another Mat to this one. All tranformations by the next Mat will be applied after the transformations of this Mat.

func (Mat) Move

func (m Mat) Move(delta Vec) Mat

Move moves everything by the delta vector.

func (Mat) Project

func (m Mat) Project(u Vec) Vec

Project applies all transformations added to the Mat to a vector u and returns the result.

func (Mat) Raw

func (m Mat) Raw() [9]float32

Raw returns raw representation of matrix

func (Mat) Rotated

func (m Mat) Rotated(around Vec, angle float64) Mat

Rotated rotates everything around a given point by the given angle in radians.

func (Mat) Scaled

func (m Mat) Scaled(around Vec, scale float64) Mat

Scaled scales everything around a given point by the scale factor.

func (Mat) ScaledXY

func (m Mat) ScaledXY(around Vec, scale Vec) Mat

ScaledXY scales everything around a given point by the scale factor in each axis respectively.

func (Mat) String

func (m Mat) String() string

String returns a string representation of the Mat.

m := mat.IM
fmt.Println(m) // Mat(1 0 0 | 0 1 0)

func (Mat) Unproject

func (m Mat) Unproject(u Vec) Vec

Unproject does the inverse operation to Project.

type Point

type Point struct {
	X, Y int
}

Point is a vector type with X and Y coordinates.

var ZP Point

ZP is zero value Point

func P

func P(x, y int) Point

P returns a new vector with the given coordinates.

func (Point) Add

func (v Point) Add(u Point) Point

Add returns the sum of vectors v and v.

func (*Point) AddE

func (v *Point) AddE(u Point)

AddE same as v = v.Add(u)

func (Point) Clamp

func (v Point) Clamp(min, max Point) Point

Clamp clamps point between two points

func (Point) Div

func (v Point) Div(u Point) Point

Div returns the vector v divided by the vector u component-wise.

func (*Point) DivE

func (v *Point) DivE(u Point)

DivE same as v = v.Div(u)

func (Point) Divided

func (v Point) Divided(c int) Point

Divided returns the vector v divided by c.

func (Point) Flatten

func (v Point) Flatten() [2]int

Flatten flatens the Point into Array, values are ordered as they would on stack

func (Point) Inv

func (v Point) Inv() Point

Inv returns v with both components inverted

func (Point) Max

func (v Point) Max(b Point) Point

Max returns max of two points componentvise

func (Point) Min

func (v Point) Min(b Point) Point

Min returns min of two points componentvise

func (Point) Mul

func (v Point) Mul(u Point) Point

Mul returns the vector v multiplied by the vector u component-wise.

func (*Point) MulE

func (v *Point) MulE(u Point)

MulE same as v = v.Mul(u)

func (*Point) Mutator

func (v *Point) Mutator() [2]*int

Mutator similar to Flatten returns array with vector components though these are pointers to componenets instead

func (Point) Scaled

func (v Point) Scaled(c int) Point

Scaled returns the vector v multiplied by c.

func (Point) Sub

func (v Point) Sub(u Point) Point

Sub subtracts u from v and returns recult.

func (*Point) SubE

func (v *Point) SubE(u Point)

SubE same as v = v.Sub(u)

func (Point) To

func (v Point) To(u Point) Point

To returns the vector from v to u. Equivalent to u.Sub(v).

func (Point) XY

func (v Point) XY() (x, y int)

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

type RGBA

type RGBA struct {
	R, G, B, A float64
}

RGBA represents an alpha-premultiplied RGBA color with components within range [0, 1].

The difference between color.RGBA is that the value range is [0, 1] and the values are floats.

var ZRGBA RGBA

ZRGBA is zero value RGBA

func Alpha

func Alpha(a float64) RGBA

Alpha returns a white RGBA color with the given alpha component.

func HexToRGBA

func HexToRGBA(s string) (r RGBA, err error)

HexToRGBA converts hex string to RGBA

func LerpColor

func LerpColor(a, b RGBA, t float64) RGBA

LerpColor does linear interpolation between two colors

func RGB

func RGB(r, g, b float64) RGBA

RGB returns a fully opaque RGBA color with the given RGB values.

A common way to construct a transparent color is to create one with RGB constructor, then multiply it by a color obtained from the Alpha constructor.

func ToRGBA

func ToRGBA(c color.Color) RGBA

ToRGBA converts a color to RGBA format. Using this function is preferred to using RGBAModel, for performance (using RGBAModel introduces additional unnecessary allocations).

func (RGBA) Add

func (r RGBA) Add(d RGBA) RGBA

Add adds color d to color r component-wise and returns the result (the components are not clamped).

func (RGBA) Div

func (r RGBA) Div(d RGBA) RGBA

Div divides r by d component-wise (the components are not clamped).

func (RGBA) Flatten

func (r RGBA) Flatten() [4]float64

Flatten returns a array representation of color

func (RGBA) Inverted

func (r RGBA) Inverted() RGBA

Inverted returns inverted color, except alpha channel

func (RGBA) Mul

func (r RGBA) Mul(d RGBA) RGBA

Mul multiplies color r by color d component-wise (the components are not clamped).

func (RGBA) Mutator

func (r RGBA) Mutator() [4]*float64

Mutator returns array of pointers to color channels

func (RGBA) RGBA

func (r RGBA) RGBA() (rc, g, b, a uint32)

RGBA returns alpha-premultiplied red, green, blue and alpha components of the RGBA color.

func (RGBA) Scaled

func (r RGBA) Scaled(scale float64) RGBA

Scaled multiplies each component of color r by scale and returns the result (the components are not clamped).

func (RGBA) String

func (r RGBA) String() string

func (RGBA) Sub

func (r RGBA) Sub(d RGBA) RGBA

Sub subtracts color d from color r component-wise and returns the result (the components are not clamped).

type Ray

type Ray struct {
	// O is an origin and V is a directional vector
	O, V Vec
}

Ray is a standard raycast, it supports calculating intersections with all collizion shapes

var ZR Ray

ZR is zero value ray

func R

func R(ox, oy, vx, vy float64) Ray

R creates new raycast, o stands for origin and v for directional vector

func (Ray) Colinear

func (r Ray) Colinear(s Ray) bool

Colinear returns whether two rays are colinear

func (Ray) Contains

func (r Ray) Contains(pos Vec) bool

Contains returns whether ray contains the pos

func (Ray) Formula

func (r Ray) Formula(pos Vec) float64

Formula returns 0 if point belongs to line that ray is on and negative or positive number depending on witch half plane point is in comparison to line

func (Ray) InAABB

func (r Ray) InAABB(pos Vec) bool

InAABB returns whether pos is inside AABB expressed by ray

func (Ray) Intersect

func (r Ray) Intersect(s Ray) (v Vec, ok bool)

Intersect calculates the intersection point between two rays unless rays are colinear, intersection will be returned, but if false is returned, intersection does not include tha both ray segments

func (Ray) IntersectCircle

func (r Ray) IntersectCircle(c Circ, buff []Vec) []Vec

IntersectCircle returns points of intersection between Ray and circle and whether they are valid

func (Ray) IntersectsCircle

func (r Ray) IntersectsCircle(c Circ) bool

IntersectsCircle returns whether ray intersects circle

func (Ray) LineIntersect

func (r Ray) LineIntersect(s Ray) (point Vec)

LineIntersect returns the point of intersection of two lines expressed by rays

function does not make sense for colinear lines

func (Ray) LineIntersectCircle

func (r Ray) LineIntersectCircle(c Circ) (g, h Vec)

LineIntersectCircle calculates intersection points between circle and line

this does not make sense for line and circle that does not intersect

func (Ray) LineIntersectsCircle

func (r Ray) LineIntersectsCircle(c Circ) bool

LineIntersectsCircle returns whether line and circle intersects

func (Ray) ProjectX

func (r Ray) ProjectX(x float64) float64

ProjectX returns y coordinate for x coordinate, resulting point is on line

this does not make sense for vertical ray

func (Ray) ProjectY

func (r Ray) ProjectY(y float64) float64

ProjectY returns x coordinate for y coordinate, resulting point is on line

this does not make sense for horizontal ray

func (Ray) String

func (r Ray) String() string

type Tran

type Tran struct {
	Pos, Scl Vec
	Rot      float64
}

Tran is standard transform with some utility, it plays well with ggl.Sprite

func (*Tran) Mat

func (t *Tran) Mat() Mat

Mat returns matrix reperesenting transform

type Vec

type Vec struct {
	X, Y float64
}

Vec is a vector type with X and Y coordinates.

var ZV Vec

ZV is zero value Vec

func Rad

func Rad(angle, length float64) Vec

Rad returns vector from representation of radial cordenates

func V

func V(x, y float64) Vec

V returns a new vector with the given coordinates.

func (Vec) Add

func (v Vec) Add(u Vec) Vec

Add returns the sum of vectors v and v.

func (*Vec) AddE

func (v *Vec) AddE(u Vec)

AddE same as v = v.Add(u)

func (Vec) Angle

func (v Vec) Angle() float64

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

func (Vec) AngleTo

func (v Vec) AngleTo(u Vec) float64

AngleTo returns angle between v and v.

func (Vec) Approx

func (v Vec) Approx(b Vec, precision int) bool

Approx same as normal approx but for vector

func (Vec) Cross

func (v Vec) Cross(u Vec) float64

Cross return the cross product of vectors v and u.

func (Vec) Div

func (v Vec) Div(u Vec) Vec

Div returns the vector v divided by the vector u component-wise.

func (*Vec) DivE

func (v *Vec) DivE(u Vec)

DivE same as v = v.Div(u)

func (Vec) Divided

func (v Vec) Divided(c float64) Vec

Divided returns the vector v divided by c.

func (Vec) Dot

func (v Vec) Dot(u Vec) float64

Dot returns the dot product of vectors v and u.

func (Vec) Flatten

func (v Vec) Flatten() [2]float64

Flatten flatens the Vec into Array, values are ordered as they would on stack

func (Vec) Floor

func (v Vec) Floor() Vec

Floor converts x and y to their integer equivalents.

func (Vec) Floored added in v0.3.1

func (v Vec) Floored() Vec

Floored applyes math.Floor on both componenets and returns resulting vector

func (Vec) Inv

func (v Vec) Inv() Vec

Inv returns v with both components inverted

func (Vec) Len

func (v Vec) Len() float64

Len returns the length of the vector v.

func (Vec) Len2

func (v Vec) Len2() float64

Len2 returns length*length of vector witch is faster then Len you can for example comparing Len2 of two vectors yields same results as comparing Len

func (Vec) Lerp

func (v Vec) Lerp(b Vec, t float64) Vec

Lerp returns a linear interpolation between vectors 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 (Vec) Map

func (v Vec) Map(f func(float64) float64) Vec

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

v := mat.V(10.5, -1.5)
v := v.Map(math.Floor)   // v is Vec(10, -2), both components of v floored

func (Vec) Max

func (v Vec) Max(u Vec) Vec

Max uses math.Max on both components and returns resulting vector

func (Vec) Min

func (v Vec) Min(u Vec) Vec

Min uses math.Min on both components and returns resulting vector

func (Vec) Mul

func (v Vec) Mul(u Vec) Vec

Mul returns the vector v multiplied by the vector u component-wise.

func (*Vec) MulE

func (v *Vec) MulE(u Vec)

MulE same as v = v.Mul(u)

func (*Vec) Mutator

func (v *Vec) Mutator() [2]*float64

Mutator similar to Flatten returns array with vector components though these are pointers to componenets instead

func (Vec) Normal

func (v Vec) Normal() Vec

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

func (Vec) Normalized

func (v Vec) Normalized() Vec

Normalized returns a vector of length 1 facing the direction of v (has the same angle).

func (Vec) Point

func (v Vec) Point() Point

Point converts Vec to Point

func (Vec) Rotated

func (v Vec) Rotated(angle float64) Vec

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

func (Vec) SameDir added in v0.4.2

func (v Vec) SameDir(u Vec) bool

If cross of two vectors returns 0, this can relayably tell if they are heading same direction

func (Vec) Scaled

func (v Vec) Scaled(c float64) Vec

Scaled returns the vector v multiplied by c.

func (Vec) String

func (v Vec) String() string

String returns the string representation of the vector v.

func (Vec) Sub

func (v Vec) Sub(u Vec) Vec

Sub subtracts u from v and returns recult.

func (*Vec) SubE

func (v *Vec) SubE(u Vec)

SubE same as v = v.Sub(u)

func (Vec) Swapped

func (v Vec) Swapped() Vec

Swapped returns v with swapped components

func (Vec) To

func (v Vec) To(u Vec) Vec

To returns the vector from v to u. Equivalent to u.Sub(v).

func (Vec) ToAABB

func (v Vec) ToAABB() AABB

ToAABB turns Vec into AABB where Min is V(0 0) and Max is v

func (Vec) XY

func (v Vec) XY() (x, y float64)

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

Directories

Path Synopsis
Package mat3 is still in progress and will documented after its finished
Package mat3 is still in progress and will documented after its finished

Jump to

Keyboard shortcuts

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