Documentation

Index

Constants

View Source
const MaxInt = int(^uint(0) >> 1)
View Source
const MinInt = -MaxInt - 1

Variables

This section is empty.

Functions

func Approx

func Approx(a, b float64) bool

approximately using precision = 0.000001

func Approximately

func Approximately(a, b, precision float64) bool

func Between

func Between(v, m1, m2 float64) bool

func BoundingBox

func BoundingBox(curve CubicCurve) (topLeft Point, bottomRight Point)

the smallest box that the curve fits into

func BoundingBoxOverlaps

func BoundingBoxOverlaps(box1TopLeft, box1BottomRight, box2TopLeft, box2BottomRight Point) bool

func Crt

func Crt(v float64) float64

cube root function yielding real roots

func CurveOverlaps

func CurveOverlaps(c1, c2 CubicCurve) bool

checks the Bounding Boxes of the two curves and tests whether they overlap

func Distance

func Distance(p1 Point, p2 Point) float64

Distance finds the straightline distance between the two points

func Float64ArrayContains

func Float64ArrayContains(a []float64, x float64) bool

func Float64ArrayDeDup

func Float64ArrayDeDup(a []float64) []float64

removes any duplicates from the array order may or may not be maintained

func Float64ArrayInsertIfAbsent

func Float64ArrayInsertIfAbsent(a []float64, x float64) []float64

inserts the requested item if it does not already exist

func IntersectsLine

func IntersectsLine(curve CubicCurve, p1 Point, p2 Point) []float64

Finds the intersections between this curve an some line {p1: {x:... ,y:...}, p2: ... }. The intersections are an array of t values on this curve. Curves are first aligned (translation/rotation) such that the curve's first coordinate is (0,0), and the curve is rotated so that the intersecting line coincides with the x-axis. Doing so turns "intersection finding" into plain "root finding". As a root finding solution, the roots are computed symbolically for both quadratic and cubic curves, using the standard square root function which you might remember from high school, and the absolutely not standard Cardano's algorithm for solving the cubic root function.

func IntersectsProjectedLine

func IntersectsProjectedLine(curve CubicCurve, p1 Point, p2 Point) []float64

func IntersectsSelf

func IntersectsSelf(curve CubicCurve, curveIntersectionThreshold float64) (intersect bool, t1 float64, t2 float64)

this function checks for self-intersection. Intersections are yielded as an array of float/float strings, where the two floats are separated by the character / and both floats corresponding to t values on the curve at which the intersection is found.

Note this will only return a single intersection. Afaik a cubic curve could not intersect itself more than once, so this is likely a non-issue. (Anyone know different? Math is hard.)

func IsClockwise

func IsClockwise(curve CubicCurve) bool

returns true if this curve is clockwise

func Roots

func Roots(points []Point, lineP1 Point, lineP2 Point) []float64

func SplitCurve

func SplitCurve(curve CubicCurve, t float64) (CubicCurve, CubicCurve)

func StringArrayDeDup

func StringArrayDeDup(a []string) []string

removes any duplicates maintaining ordering

Types

type CubicCurve

type CubicCurve struct {
	Start        Point
	StartControl Point
	End          Point
	EndControl   Point
}

func Offset

func Offset(curve CubicCurve, d float64) []CubicCurve

This function creates a new curve, offset along the curve normals, at distance d. Note that deep magic lies here and the offset curve of a Bezier curve cannot ever be another Bezier curve. As such, this function "cheats" and yields an array of curves which, taken together, form a single continuous curve equivalent to what a theoretical offset curve would be.

func Scale

func Scale(curve CubicCurve, d float64) (CubicCurve, error)

func ScaleByFunc

func ScaleByFunc(curve CubicCurve, distanceFn distanceFunc) (CubicCurve, error)

scales by the passed in distance function

func SplitCurveSection

func SplitCurveSection(curve CubicCurve, tStart float64, tEnd float64) CubicCurve

splits the curve into a section

type Point

type Point struct {
	X float64
	Y float64
}

func Align

func Align(points []Point, lineP1 Point, lineP2 Point) []Point

func DeCasteljau

func DeCasteljau(curve CubicCurve, t float64) Point

decasteljaus algorithm to find point on a curve. This will find the point t on the curve (where t >=0 && t <= 1) that is, t is the percentage along the line of the curve.

func Derivative

func Derivative(curve CubicCurve, t float64) Point

Calculates the curve tangent at the specified t value. Note that this yields a not-normalized vector {x: dx, y: dy}

func FindPoint

func FindPoint(curve CubicCurve, t float64) Point

finds the given t value on the curve. Where t is between 0 and 1

func NewPoint

func NewPoint(x, y float64) Point

func OffsetPoint

func OffsetPoint(curve CubicCurve, t, d float64) Point

the point on the curve at t=..., offset along its normal by a distance d.

func Project

func Project(curve CubicCurve, point Point) (pt Point, distance float64, t float64)

Finds the on-curve point closest to the specific off-curve point, using a two-pass projection test based on the curve's LUT. A distance comparison finds the closest match, after which a fine interval around that match is checked to see if a better projection can be found.

func (Point) String

func (p Point) String() string

type ValuePair

type ValuePair struct {
	Left  float64
	Right float64
}

func IntersectsCurve

func IntersectsCurve(c1, c2 CubicCurve, curveIntersectionThreshold float64) []ValuePair

Finds the intersection points between the two curves. The resulting pairs are the T values on left (i.e c1) and right (c2)

Note curveIntersectionThreshold defaults to 0.5 in the javascript impl

func ValuePairDedup

func ValuePairDedup(a []ValuePair) []ValuePair

dedups the ValuePair array, based on the String() method (currently truncated to 5 digits)

func (ValuePair) String

func (c ValuePair) String() string

Source Files