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 ¶
- Variables
- func Absi(a int) int
- func Approx(a, b float64, precision int) bool
- func Clamp(val, min, max float64) float64
- func Lerp(a, b, t float64) float64
- func Maxi(a, b int) int
- func Mini(a, b int) int
- func Mod(x, y float64) float64
- func Polynomial(a, b, c float64) (x1, x2 float64)
- func Range(min, max float64) float64
- func Round(v float64, precision int) float64
- type AABB
- func (a AABB) Area() float64
- func (a AABB) Center() Vec
- func (a AABB) Clamp(v Vec) Vec
- func (a AABB) Contains(u Vec) bool
- func (a *AABB) Deco() (left, bottom, right, top float64)
- func (a AABB) Fits(b AABB) bool
- func (a AABB) Flatten() [4]float64
- func (a AABB) H() float64
- func (a AABB) Intersect(s AABB) AABB
- func (a AABB) IntersectCircle(c Circ) bool
- func (a AABB) Intersects(s AABB) bool
- func (a AABB) LocalVertices() [4]Vec
- func (a AABB) Moved(delta Vec) AABB
- func (a *AABB) Mutator() [4]*float64
- func (a AABB) Norm() AABB
- func (a AABB) Resized(anchor, size Vec) AABB
- func (a AABB) ResizedMin(size Vec) AABB
- func (a AABB) Size() Vec
- func (a AABB) String() string
- func (a AABB) ToImage() image.Rectangle
- func (a AABB) ToVec() Vec
- func (a AABB) Union(s AABB) AABB
- func (a AABB) Vertices() [4]Vec
- func (a AABB) W() float64
- type Circ
- func (c Circ) Approx(o Circ, precision int) bool
- func (c Circ) Contains(pos Vec) bool
- func (c Circ) Intersect(s Circ) (k, p Vec)
- func (c Circ) Intersects(o Circ) bool
- func (c Circ) ProjectX(x float64) (a, b float64)
- func (c Circ) ProjectY(y float64) (a, b float64)
- func (c Circ) SimplePrjX(x float64) float64
- func (c Circ) SimplePrjY(y float64) float64
- func (c Circ) String() string
- func (c Circ) Union(o Circ) Circ
- type Mat
- func (m Mat) Approx(b Mat, precision int) bool
- func (m Mat) Chained(next Mat) Mat
- func (m Mat) Move(delta Vec) Mat
- func (m Mat) Project(u Vec) Vec
- func (m Mat) Raw() [9]float32
- func (m Mat) Rotated(around Vec, angle float64) Mat
- func (m Mat) Scaled(around Vec, scale float64) Mat
- func (m Mat) ScaledXY(around Vec, scale Vec) Mat
- func (m Mat) String() string
- func (m Mat) Unproject(u Vec) Vec
- type Point
- func (v Point) Add(u Point) Point
- func (v *Point) AddE(u Point)
- func (v Point) Clamp(min, max Point) Point
- func (v Point) Div(u Point) Point
- func (v *Point) DivE(u Point)
- func (v Point) Divided(c int) Point
- func (v Point) Flatten() [2]int
- func (v Point) Inv() Point
- func (v Point) Max(b Point) Point
- func (v Point) Min(b Point) Point
- func (v Point) Mul(u Point) Point
- func (v *Point) MulE(u Point)
- func (v *Point) Mutator() [2]*int
- func (v Point) Scaled(c int) Point
- func (v Point) Sub(u Point) Point
- func (v *Point) SubE(u Point)
- func (v Point) To(u Point) Point
- func (v Point) XY() (x, y int)
- type RGBA
- func (r RGBA) Add(d RGBA) RGBA
- func (r RGBA) Div(d RGBA) RGBA
- func (r RGBA) Flatten() [4]float64
- func (r RGBA) Inverted() RGBA
- func (r RGBA) Mul(d RGBA) RGBA
- func (r RGBA) Mutator() [4]*float64
- func (r RGBA) RGBA() (rc, g, b, a uint32)
- func (r RGBA) Scaled(scale float64) RGBA
- func (r RGBA) String() string
- func (r RGBA) Sub(d RGBA) RGBA
- type Ray
- func (r Ray) Colinear(s Ray) bool
- func (r Ray) Contains(pos Vec) bool
- func (r Ray) Formula(pos Vec) float64
- func (r Ray) InAABB(pos Vec) bool
- func (r Ray) Intersect(s Ray) (v Vec, ok bool)
- func (r Ray) IntersectCircle(c Circ, buff []Vec) []Vec
- func (r Ray) IntersectsCircle(c Circ) bool
- func (r Ray) LineIntersect(s Ray) (point Vec)
- func (r Ray) LineIntersectCircle(c Circ) (g, h Vec)
- func (r Ray) LineIntersectsCircle(c Circ) bool
- func (r Ray) ProjectX(x float64) float64
- func (r Ray) ProjectY(y float64) float64
- func (r Ray) String() string
- type Tran
- type Vec
- func (v Vec) Add(u Vec) Vec
- func (v *Vec) AddE(u Vec)
- func (v Vec) Angle() float64
- func (v Vec) AngleTo(u Vec) float64
- func (v Vec) Approx(b Vec, precision int) bool
- func (v Vec) Cross(u Vec) float64
- func (v Vec) Div(u Vec) Vec
- func (v *Vec) DivE(u Vec)
- func (v Vec) Divided(c float64) Vec
- func (v Vec) Dot(u Vec) float64
- func (v Vec) Flatten() [2]float64
- func (v Vec) Floor() Vec
- func (v Vec) Floored() Vec
- func (v Vec) Inv() Vec
- func (v Vec) Len() float64
- func (v Vec) Len2() float64
- func (v Vec) Lerp(b Vec, t float64) Vec
- func (v Vec) Map(f func(float64) float64) Vec
- func (v Vec) Max(u Vec) Vec
- func (v Vec) Min(u Vec) Vec
- func (v Vec) Mul(u Vec) Vec
- func (v *Vec) MulE(u Vec)
- func (v *Vec) Mutator() [2]*float64
- func (v Vec) Normal() Vec
- func (v Vec) Normalized() Vec
- func (v Vec) Point() Point
- func (v Vec) Rotated(angle float64) Vec
- func (v Vec) SameDir(u Vec) bool
- func (v Vec) Scaled(c float64) Vec
- func (v Vec) String() string
- func (v Vec) Sub(u Vec) Vec
- func (v *Vec) SubE(u Vec)
- func (v Vec) Swapped() Vec
- func (v Vec) To(u Vec) Vec
- func (v Vec) ToAABB() AABB
- func (v Vec) XY() (x, y float64)
Constants ¶
This section is empty.
Variables ¶
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
var ErrInvalidHex = errors.New("byte is not a hex code")
ErrInvalidHex is returned by HexToRGBA if hex string contains non ex characters
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
var IM = Mat{Vec{1, 0}, Vec{0, 1}, Vec{0, 0}}
IM is Matrix used as base for all transformations
var RGBAModel = color.ModelFunc(rgbaModel)
RGBAModel converts colors to RGBA format.
Functions ¶
func Mod ¶
Mod is faster version of math.Mod, trade of is that it is accurate only to first 16 significant digits
func Polynomial ¶
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
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 ¶
A returns a new AABB with given the Min and Max coordinates.
Note that the returned rectangle is not automatically normalized.
func Centered ¶
Centered returns rect witch center is equal to c, width equal to w, likewise height equal to h
func (AABB) Contains ¶
Contains checks whether a vector u is contained within this AABB (including it's borders).
func (AABB) Flatten ¶
Flatten returns AABB flattened into Array, values are in same order as they would be stored on stack
func (AABB) Intersect ¶
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 ¶
IntersectsAABB returns whether circle intersects AABB
func (AABB) Intersects ¶
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 ¶
LocalVertices creates array of vertices relative to center of rect
func (*AABB) Mutator ¶
Mutator is similar to Iterator but this gives option to mutate state of AABB trough Array Entries
func (AABB) Norm ¶
Norm returns the AABB in normal form, such that Max is component-wise greater or equal than Min.
func (AABB) Resized ¶
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 ¶
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) Union ¶
Union returns the minimal AABB which covers both a and s. AABBs a and s must be normalized.
type Circ ¶
Circ is an circle
func (Circ) Intersects ¶
Intersects detects intersection between two circles
func (Circ) SimplePrjX ¶
SimplePrjX performs simple projection x into y on circle
if x cannot be projected NaN is returned
func (Circ) SimplePrjY ¶
SimplePrjY is analogous to SimplePrjX and projects y to x
if y cannot be projected NaN is returned
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 ¶
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) Chained ¶
Chained adds another Mat to this one. All tranformations by the next Mat will be applied after the transformations of this Mat.
func (Mat) Project ¶
Project applies all transformations added to the Mat to a vector u and returns the result.
func (Mat) ScaledXY ¶
ScaledXY scales everything around a given point by the scale factor in each axis respectively.
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 (Point) Flatten ¶
Flatten flatens the Point into Array, values are ordered as they would on stack
func (*Point) Mutator ¶
Mutator similar to Flatten returns array with vector components though these are pointers to componenets instead
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 RGB ¶
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 ¶
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 ¶
Add adds color d to color r component-wise and returns the result (the components are not clamped).
func (RGBA) Mul ¶
Mul multiplies color r by color d component-wise (the components are not clamped).
func (RGBA) RGBA ¶
RGBA returns alpha-premultiplied red, green, blue and alpha components of the RGBA color.
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 (Ray) Formula ¶
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) Intersect ¶
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 ¶
IntersectCircle returns points of intersection between Ray and circle and whether they are valid
func (Ray) IntersectsCircle ¶
IntersectsCircle returns whether ray intersects circle
func (Ray) LineIntersect ¶
LineIntersect returns the point of intersection of two lines expressed by rays
function does not make sense for colinear lines
func (Ray) LineIntersectCircle ¶
LineIntersectCircle calculates intersection points between circle and line
this does not make sense for line and circle that does not intersect
func (Ray) LineIntersectsCircle ¶
LineIntersectsCircle returns whether line and circle intersects
func (Ray) ProjectX ¶
ProjectX returns y coordinate for x coordinate, resulting point is on line
this does not make sense for vertical ray
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 (Vec) Angle ¶
Angle returns the angle between the vector v and the x-axis. The result is in range [-Pi, Pi].
func (Vec) Floored ¶ added in v0.3.1
Floored applyes math.Floor on both componenets and returns resulting vector
func (Vec) Len2 ¶
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 ¶
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 ¶
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) Mutator ¶
Mutator similar to Flatten returns array with vector components though these are pointers to componenets instead
func (Vec) Normal ¶
Normal returns a vector normal to v. Equivalent to v.Rotated(math.Pi / 2), but faster.
func (Vec) Normalized ¶
Normalized returns a vector of length 1 facing the direction of v (has the same angle).
func (Vec) SameDir ¶ added in v0.4.2
If cross of two vectors returns 0, this can relayably tell if they are heading same direction