Documentation ¶
Index ¶
- func Collinear(a, b, c Vec, tol float32) bool
- func CopyOrientation(f float32, p1, p2, p3 Vec) float32
- func Cos(p, q Vec) float32
- func Cross(p, q Vec) float32
- func Dot(p, q Vec) float32
- func EqualElem(a, b Vec, tol float32) bool
- func EqualMat2(a, b Mat2, tolerance float32) bool
- func Norm(p Vec) float32
- func Norm2(p Vec) float32
- type Box
- func (a Box) Add(v Vec) Box
- func (a Box) Area() float32
- func (a Box) Canon() Box
- func (a Box) Center() Vec
- func (a Box) Contains(point Vec) bool
- func (a Box) Diagonal() float32
- func (a Box) Empty() bool
- func (a Box) Equal(b Box, tol float32) bool
- func (a Box) IncludePoint(point Vec) Box
- func (a Box) Intersect(b Box) (intersect Box)
- func (a Box) Scale(scale Vec) Box
- func (a Box) ScaleCentered(scale Vec) Box
- func (a Box) Size() Vec
- func (a Box) Union(b Box) Box
- func (a Box) Vertices() [4]Vec
- type Line
- type Mat2
- type PolygonBuilder
- func (p *PolygonBuilder) Add(v Vec) *PolygonControlPoint
- func (p *PolygonBuilder) AddPolarRTheta(r, theta float32) *PolygonControlPoint
- func (p *PolygonBuilder) AddRelative(v Vec) *PolygonControlPoint
- func (p *PolygonBuilder) AddRelativeXY(x, y float32) *PolygonControlPoint
- func (p *PolygonBuilder) AddXY(x, y float32) *PolygonControlPoint
- func (p *PolygonBuilder) AppendVecs(buf []Vec) ([]Vec, error)
- func (p *PolygonBuilder) DropLast()
- func (p *PolygonBuilder) Nagon(n int, centerDistance float32)
- func (p *PolygonBuilder) NagonSmoothed(n int, centerDistance float32, facets int, radius float32)
- func (p *PolygonBuilder) Reset()
- type PolygonControlPoint
- type Spline3
- func (s Spline3) BasisFuncs() (bs [4]func(float32) float32)
- func (s Spline3) BasisFuncsDiff() (bs [4]func(float32) float32)
- func (s Spline3) BasisFuncsDiff2() (bs [4]func(float32) float32)
- func (s Spline3) BasisFuncsDiff3() (bs [4]func(float32) float32)
- func (s Spline3) Evaluate(t float32, v0, v1, v2, v3 Vec) (res Vec)
- func (s Spline3) Mat4Array() [16]float32
- type Spline3Sampler
- type Triangle
- type Vec
- func AbsElem(a Vec) Vec
- func Add(p, q Vec) Vec
- func AddScalar(f float32, v Vec) Vec
- func AppendGrid(dst []Vec, bounds Box, nx, ny int) []Vec
- func CeilElem(a Vec) Vec
- func ClampElem(v, Min, Max Vec) Vec
- func CosElem(a Vec) Vec
- func DivElem(a, b Vec) Vec
- func FloorElem(a Vec) Vec
- func InterpElem(x, y, a Vec) Vec
- func MaxElem(a, b Vec) Vec
- func MinElem(a, b Vec) Vec
- func MulElem(a, b Vec) Vec
- func MulMatVec(m Mat2, v Vec) (result Vec)
- func MulMatVecTrans(m Mat2, v Vec) (result Vec)
- func RoundElem(a Vec) Vec
- func Scale(f float32, p Vec) Vec
- func SignElem(a Vec) Vec
- func SinElem(a Vec) Vec
- func SincosElem(a Vec) (s, c Vec)
- func SmoothStepElem(e0, e1, x Vec) Vec
- func Sub(p, q Vec) Vec
- func Unit(p Vec) Vec
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CopyOrientation ¶
Orientation calculates the orientation in the plane of 3 points and applies it to f.
- f returned for counter-clockwise orientation
- -f returned for clockwise orientation
- 0 returned for 3 colinear points
Types ¶
type Box ¶
type Box struct {
Min, Max Vec
}
Box is a 2D bounding box. Well formed Boxes Min components are smaller than Max components. Max is the most positive/largest vertex, Min is the most negative/smallest vertex.
func NewBox ¶
NewBox is shorthand for Box{Min:Vec{x0,y0}, Max:Vec{x1,y1}}. The sides are swapped so that the resulting Box is well formed.
func NewCenteredBox ¶
NewCenteredBox returns a box centered around center with size dimensions.
func (Box) Add ¶
Add adds v to the bounding box components. It is the equivalent of translating the Box by v.
func (Box) Canon ¶
Canon returns the canonical version of a. The returned Box has minimum and maximum coordinates swapped if necessary so that it is well-formed.
func (Box) Empty ¶
IsEmpty returns true if a Box's volume is zero or if a Min component is greater than its Max component.
func (Box) Equal ¶
Equal returns true if a and b are within tol of eachother for each box limit component.
func (Box) IncludePoint ¶
IncludePoint returns a box containing both the receiver and the argument point.
func (Box) Scale ¶
Scale scales the box dimensions and positions in 3 directions. Does not preserve box center. Negative elements of scale can be used to mirror box dimension.
func (Box) ScaleCentered ¶
ScaleCentered returns a new Box scaled by a size vector around its center. The scaling is done element wise which is to say the Box's X dimension is scaled by scale.X. Negative elements of scale are interpreted as zero.
type Line ¶
type Line [2]Vec
Line is an infinite 3D Line defined by two points on the Line.
func (Line) Interpolate ¶
Interpolate takes a value between 0 and 1 to linearly interpolate a point on the line.
Interpolate(0) returns l[0] Interpolate(1) returns l[1]
type Mat2 ¶
type Mat2 struct {
// contains filtered or unexported fields
}
Mat2 is a 2x2 matrix.
func NewMat2 ¶
NewMat2 instantiates a new matrix from the first 4 floats, row major order. If v is of insufficient length NewMat2 panics.
func RotationMat2 ¶
Rotate returns an orthographic 2x2 rotation matrix (right hand rule).
func (Mat2) Determinant ¶
Determinant returns the determinant of a 2x2 matrix.
func (Mat2) Put ¶
Put stores the matrix values into slice b in row major order. If b is not of length 4 or greater Put panics.
type PolygonBuilder ¶
type PolygonBuilder struct {
// contains filtered or unexported fields
}
PolygonBuilder facilitates polygon construction with arcs, smoothing and chamfers with the PolygonControlPoint type.
func (*PolygonBuilder) Add ¶
func (p *PolygonBuilder) Add(v Vec) *PolygonControlPoint
Add adds a point in absolute cartesian coordinates to the polygon being built.
func (*PolygonBuilder) AddPolarRTheta ¶
func (p *PolygonBuilder) AddPolarRTheta(r, theta float32) *PolygonControlPoint
AddPolarRTheta adds a point in absolute polar coordinates to the polygon being built.
func (*PolygonBuilder) AddRelative ¶
func (p *PolygonBuilder) AddRelative(v Vec) *PolygonControlPoint
AddRelative adds a point in absolute cartesian coordinates to the polygon being built, relative to last vertex added. If no vertices present then takes origin (x=0,y=0) as reference.
func (*PolygonBuilder) AddRelativeXY ¶
func (p *PolygonBuilder) AddRelativeXY(x, y float32) *PolygonControlPoint
AddRelativeXY is shorthand for PolygonBuilder.AddRelative(Vec{x,y}).
func (*PolygonBuilder) AddXY ¶
func (p *PolygonBuilder) AddXY(x, y float32) *PolygonControlPoint
AddXY adds a point in absolute cartesian coordinates to the polygon being built.
func (*PolygonBuilder) AppendVecs ¶
func (p *PolygonBuilder) AppendVecs(buf []Vec) ([]Vec, error)
AppendVecs appends the Polygon's discretized representation to the argument Vec buffer and returns the result. It does not change the internal state of the PolygonBuilder and thus can be called repeatedly.
func (*PolygonBuilder) DropLast ¶
func (p *PolygonBuilder) DropLast()
DropLast drops the last vertex. Can be called multiple times to drop several vertices.
func (*PolygonBuilder) Nagon ¶
func (p *PolygonBuilder) Nagon(n int, centerDistance float32)
Nagon sets the vertices of p to that of a N sided regular polygon. If n<3 then Nagon does nothing.
func (*PolygonBuilder) NagonSmoothed ¶
func (p *PolygonBuilder) NagonSmoothed(n int, centerDistance float32, facets int, radius float32)
NagonSmoothed sets the vertices of p to that of a N sided regular polygon and smoothes the result. If n<3 or radius<centerDistance then Nagon does nothing.
func (*PolygonBuilder) Reset ¶
func (p *PolygonBuilder) Reset()
Reset resets all polygon builder state dropping all vertices.
type PolygonControlPoint ¶
type PolygonControlPoint struct {
// contains filtered or unexported fields
}
PolygonControlPoint represents a polygon point joined by two edges, or alternatively a smoothed control point, in which case the vertex does not lie in the polygon. It is used by the PolygonBuilder type and notably returned by the Add* methods so that the user may control the polygon's shape. By default represents a vertex joining two other neighboring vertices.
func (*PolygonControlPoint) Arc ¶
func (v *PolygonControlPoint) Arc(radius float32, facets int)
Arc creates an arc between this and the previous PolygonVertex discretised by facets starting at previous radius.
A positive radius specifies counter-clockwise path, a negative radius specifies a clockwise path.
func (*PolygonControlPoint) Chamfer ¶
func (v *PolygonControlPoint) Chamfer(size float32)
Chamfer is a smoothing of a single facet of length `size`.
func (*PolygonControlPoint) Smooth ¶
func (v *PolygonControlPoint) Smooth(radius float32, facets int)
Smooth smoothes this polygon vertex by a radius and discretises the smoothing in facets.
type Spline3 ¶
type Spline3 struct {
// contains filtered or unexported fields
}
Spline3 implements uniform cubic spline logic (degree 3). Keep in mind the iteration over the spline points and how the points are interpreted depend on the type of spline being worked with.
Bézier example:
const Nsamples = 64 // Number of times to sample each set of two Bézier points. var spline []ms2.Vec = makeBezierSpline() bz := ms2.SplineBezier() var curve []ms2.Vec for i := 0; i < len(spline); i += 4 { p0, cp0, cp1, p1 := spline[4*i], spline[4*i+1], spline[4*i+2], spline[4*i+3] for t := float32(0.0); t<1; t+=1./Nsamples { xy := bz.Evaluate(t, p0, cp0, cp1, p1) curve = append(curve, xy) } } plot(curve)
func NewSpline3 ¶
NewSpline3 returns a Spline3 ready for use. See Freya Holmér's video on splines for more information on how a matrix represents a uniform cubic spline.
func SplineBasis ¶
func SplineBasis() Spline3
SplineBasis returns a B-Spline interpreter. Result splines have the following characteristics:
- C² continuous.
- No point interpolation.
- Automatic tangents.
- Ideal for curvature-sensitive shapes and animations such as camera paths. Used in industrial design.
func SplineBezierCubic ¶
func SplineBezierCubic() Spline3
SplineBezierCubic returns a Bézier cubic spline interpreter. Result splines have the following characteristics:
- C¹/C⁰ continuous.
- Interpolates some points.
- Manual tangents, second and third vectors are control points.
- Uses in shapes and vector graphics.
Iterate every 3 points. Point0, ControlPoint0, ControlPoint1, Point1.
func SplineBezierQuadratic ¶
func SplineBezierQuadratic() Spline3
SplineBezierQuadratic returns a quadratic spline interpreter (fourth point is inneffective).
- C¹ continuous.
- Interpolates all points.
- Manual tangents.
- Used in fonts. Cubic beziers are superior.
Iterate every 2 points. Point0, ControlPoint, Point1. Keep in mind this is an innefficient implementation of a quadratic bezier. Is here for convenience.
func SplineCardinal ¶
SplineCardinal returns a cardinal cubic spline interpreter.
func SplineCatmullRom ¶
func SplineCatmullRom() Spline3
SplineCatmullRom returns a Catmull-Rom cubic spline interpreter, a special case of Cardinal spline when scale=0.5. Result splines have the following characteristics:
- C¹ continuous.
- Interpolates all points.
- Automatic tangents.
- Used for animation and path smoothing.
func SplineHermite ¶
func SplineHermite() Spline3
SplineHermite returns a Hermite cubic spline interpreter. Result splines have the following characteristics:
- C¹/C⁰ continuous.
- Interpolates all points.
- Explicit tangents. Second and fourth vector arguments specify velocities.
- Uses in animation, physics simulations and interpolation.
Iterate every 2 points, Point0, Velocity0, Point1, Velocity1.
func (Spline3) BasisFuncs ¶
BasisFuncs returns the basis functions of the cubic spline corresponding to each of 4 control points.
func (Spline3) BasisFuncsDiff ¶
BasisFuncs returns the differentiaed basis functions of the cubic spline.
func (Spline3) BasisFuncsDiff2 ¶
BasisFuncsDiff2 returns the twice-differentiaed basis functions of the cubic spline.
func (Spline3) BasisFuncsDiff3 ¶
BasisFuncsDiff3 returns the thrice-differentiaed basis functions of the cubic spline.
type Spline3Sampler ¶
type Spline3Sampler struct { Spline Spline3 // Tolerance sets the maximum permissible error for sampling the cubic spline. // That is to say the resulting sampled set of line segments will approximate the curve to within Tolerance. Tolerance float32 // contains filtered or unexported fields }
Spline3Sampler implements algorithms for sampling points of a cubic spline Spline3.
func (*Spline3Sampler) Evaluate ¶
func (s *Spline3Sampler) Evaluate(t float32) Vec
Evaluate evaluates a point on the spline with points set by Spline3Sampler.SetSplinePoints. It calls Spline3.Evaluate with t and the set points.
func (*Spline3Sampler) SampleBisect ¶
func (s *Spline3Sampler) SampleBisect(dst []Vec, maxDepth int) []Vec
SampleBisect samples the cubic spline using bisection method to find points which discretize the curve to within [Spline3Sampler.Tol] error These points are then appended to dst and the result returned.
It does not append points at extremes t=0 and t=1. maxDepth determines the max amount of times to subdivide the curve. The max amount of subdivisions (points appended) is given by 2**maxDepth.
func (*Spline3Sampler) SampleBisectWithExtremes ¶
func (s *Spline3Sampler) SampleBisectWithExtremes(dst []Vec, maxDepth int) []Vec
SampleBisectWithExtremes is same as Spline3Sampler.SampleBisect but adding start and end points at t=0, t=1.
func (*Spline3Sampler) SetSplinePoints ¶
func (s *Spline3Sampler) SetSplinePoints(v0, v1, v2, v3 Vec)
SetSplinePoints sets the 4 [Vec]s which define a cubic spline. They are passed to the Spline on Evaluate calls.
type Triangle ¶
type Triangle [3]Vec
Triangle represents a triangle in 2D space and is composed by 3 vectors corresponding to the position of each of the vertices. Ordering of these vertices decides the "normal" direction. Inverting ordering of two vertices inverts the resulting direction.
func (Triangle) Centroid ¶
Centroid returns the intersection of the three medians of the triangle as a point in space.
func (Triangle) IsDegenerate ¶
IsDegenerate returns true if all of triangle's vertices are within tol distance of its longest side.
type Vec ¶
type Vec struct {
X, Y float32
}
Vec is a 2D vector. It is composed of 2 float32 fields for x and y values in that order.
func AppendGrid ¶
AppendGrid splits the argument bounds Box x,y axes by nx,ny, respectively and generates points on the vertices generated by the division and appends them to dst, returning the result.
Indexing is x-major:
grid := ms2.AppendGrid(nil, bb, nx, ny) ix, iy := 1, 0 pos := grid[iy*nx + ix]
func DivElem ¶
DivElem returns the Hadamard product between vector a and the inverse components of vector b.
v = {a.X/b.X, a.Y/b.Y}
func InterpElem ¶
InterpElem performs a linear interpolation between x and y's elements, mapping with a's values in interval [0,1]. This function is also known as "mix" in OpenGL.
func MulMatVecTrans ¶
MulMatVecTrans Performs transposed matrix multiplication on v:
result = Mᵀ * v
func SignElem ¶
Sign returns sign function applied to each individual component of a. If a component is zero then zero is returned.
func SincosElem ¶
SincosElem returns (sin(a), cos(a)). Is more efficient than calling both SinElem and CosElem.
func SmoothStepElem ¶
SmoothStepElem performs element-wise smooth cubic hermite interpolation between 0 and 1 when e0 < x < e1.
func Unit ¶
Unit returns the unit vector colinear to p. Unit returns {NaN,NaN,NaN} for the zero vector.
func (Vec) AllNonzero ¶
AllNonzero returns true if all elements of a are nonzero.