Documentation
¶
Overview ¶
Package util contains various geometry functions used by the graphics2d package.
Index ¶
- Constants
- Variables
- func AngleBetweenLines(p1, p2, p3, p4 []float64) (float64, float64, float64)
- func AngleBisect(p1, p2, p3, p4 []float64) (float64, float64)
- func AngleInRange(a, r, b float64) bool
- func BBCombine(bb [][]float64, bbs ...[][]float64) [][]float64
- func BBContains(p []float64, bb [][]float64) bool
- func BBFilter(pts [][]float64, bb [][]float64) [][]float64
- func BBIntersection(bb1, bb2 [][]float64) [][]float64
- func BBOutline(bb [][]float64) [][]float64
- func BBOverlap(bb1, bb2 [][]float64) bool
- func BBToRect(bb [][]float64) image.Rectangle
- func Barycentric(p, tp1, tp2, tp3 []float64) (float64, float64, float64)
- func Bezier1(pts [][]float64, t float64) []float64
- func Bezier2(pts [][]float64, t float64) []float64
- func Bezier3(pts [][]float64, t float64) []float64
- func Bezier3ToCatmull(p1, p2, p3, p4 []float64) []float64
- func BoundingBox(pts ...[]float64) [][]float64
- func CalcDerivativeWeights(w [][]float64) [][]float64
- func CalcExtremities(points [][]float64) []float64
- func CalcNextOrderWeights(w [][]float64) [][]float64
- func CalcPointsForArc(theta float64) [][]float64
- func CatmullToBezier3(tau float64, p1, p2, p3, p4 []float64) []float64
- func Centroid(pts ...[]float64) []float64
- func Circumcircle(p1, p2, p3 []float64) []float64
- func Coincident(lp1, lp2, lp3, lp4 []float64) bool
- func Collinear(lp1, lp2, p []float64) bool
- func CrossProduct(p1, p2, p3 []float64) float64
- func DeCasteljau(pts [][]float64, t float64) []float64
- func Dejitter(closed bool, pts ...[]float64) [][]float64
- func DistanceE(p1, p2 []float64) float64
- func DistanceESquared(p1, p2 []float64) float64
- func DistanceESquaredN(p1, p2 []float64) float64
- func DistanceToLineSquared(lp1, lp2, p []float64) (float64, []float64, float64)
- func DotProduct(p1, p2, p3, p4 []float64) float64
- func DotProductAngle(p1, p2, p3, p4 []float64) float64
- func Equals(d1, d2 float64) bool
- func Equals32(d1, d2 float32) bool
- func EqualsP(v1, v2 []float64) bool
- func IntersectionTVals(x1, y1, x2, y2, x3, y3, x4, y4 float64) ([]float64, error)
- func IntersectionTValsP(p1, p2, p3, p4 []float64) ([]float64, error)
- func InverseBarycentric(w1, w2, w3 float64, tp1, tp2, tp3 []float64) []float64
- func Kappa(dpt, d2pt []float64) float64
- func Kappa1(dw, d2w [][]float64, t float64) float64
- func KappaC(p1, p2, p3 []float64) float64
- func KappaM(p1, p2, p3 []float64) float64
- func Left(lp1, lp2, p []float64) bool
- func LeftOn(lp1, lp2, p []float64) bool
- func Lerp(t, start, end float64) float64
- func LineAngle(p1, p2 []float64) float64
- func LineNormalToPoint(lp1, lp2, p []float64) ([]float64, float64, float64)
- func MinD(pts ...[]float64) int
- func NRM(start float64, f, df func(float64) float64) (float64, error)
- func Parallel(lp1, lp2, lp3, lp4 []float64) bool
- func PointInPoly(pt []float64, poly ...[]float64) bool
- func PointInTriangle(p, tp1, tp2, tp3 []float64) bool
- func PointInTriangleStrict(p, tp1, tp2, tp3 []float64) bool
- func PointOnLine(lp1, lp2, p []float64) (bool, float64)
- func PointOnTriangle(p, tp1, tp2, tp3 []float64) bool
- func RectToBB(rect image.Rectangle) [][]float64
- func Right(lp1, lp2, p []float64) bool
- func RightOn(lp1, lp2, p []float64) bool
- func SplitCurve(pts [][]float64, t float64) [][][]float64
- func ToF32(pts ...float64) []float32
- func ToF64(pts ...float32) []float64
- func TriArea(p1, p2, p3 []float64) float64
- func Vec(p1, p2 []float64) []float64
- func VecMag(v []float64) float64
- func VecNormalize(v []float64) []float64
- func Within(d1, d2, e float64) bool
- type BezierCurve
- func (bc *BezierCurve) CurveDt2X(t float64) float64
- func (bc *BezierCurve) CurveDt2Y(t float64) float64
- func (bc *BezierCurve) CurveDt3X(t float64) float64
- func (bc *BezierCurve) CurveDt3Y(t float64) float64
- func (bc *BezierCurve) CurveDtX(t float64) float64
- func (bc *BezierCurve) CurveDtY(t float64) float64
- func (bc *BezierCurve) CurveX(t float64) float64
- func (bc *BezierCurve) CurveY(t float64) float64
- func (bc *BezierCurve) Kappa(t float64) float64
Constants ¶
const ( Pi = math.Pi TwoPi = Pi * 2 )
const ( OddEven wrule = iota NonZero )
const (
Epsilon float64 = 0.000001 // 1:1,000,000
)
%f formats to 6dp by default
Variables ¶
var (
SideOfLine = TriArea
)
var WindingRule = OddEven
WindingRule defines how edges in an edge crossing polygon are treated.
Functions ¶
func AngleBetweenLines ¶
AngleBetweenLines using Atan2 vs calculating the dot product (2xSqrt+Acos). Retains the directionality of the rotation from l1 to l2, unlike dot product. The result is in the range [-Pi,Pi]. The angle of each line is also returned.
func AngleBisect ¶
AngleBisect calcuates the angle of bisection between two lines and returns the absolute and relative angles.
func AngleInRange ¶
AngleInRange returns true if angle b is within [a,a+r].
func BBContains ¶
BBContains returns true if p is in bb at the smallest dimensionality.
func BBFilter ¶
BBFilter returns only points from pts that are in bb at the smallest dimensionality.
func BBIntersection ¶
BBIntersection returns the bb formed by the overlap or nil.
func Barycentric ¶
Barycentric converts a point on the plane into Barycentric weights given three non-coincident points, tp1, tp2 and tp3.
func Bezier3ToCatmull ¶
Bezier3ToCatmull converts a cubic bezier to a catmul curve. p1, c1, c2, p2 => t1, p1, p2, t2
func BoundingBox ¶
BoundingBox returns the minimum and maximum dimensional values in a set of points. Bounds are inclusive. The dimensionality of the smallest dimension point is used for all points.
func CalcDerivativeWeights ¶
CalcDerivativeWeights calculates the derivative of the supplied curve. Bezier gradient (differentiation): Order of curve drops by one and new weights are the difference of the original weights scaled by the original order
func CalcExtremities ¶
CalcExtremities finds the extremes of a curve in terms of t.
func CalcNextOrderWeights ¶
CalcNextOrderWeights calculates the weights necessary to represent the supplied curve at next highest order, i.e. curve promotion b2 to b3. Note, there's no inverse.
func CalcPointsForArc ¶
CalcPointsForArc takes an arc angle (less than or equal to Pi), and calculates the points for a Bezier cubic to describe it on a circle centered on (0,0) with radius 1. Mid-point of the curve is (1,0) Error increases for values > Pi/2 Returns nil if the angle is zero Ref: https://www.tinaja.com/glib/bezcirc2.pdf
func CatmullToBezier3 ¶
CatmullToBezier3 converts a catmul curve to a cubic bezier. t1, p1, p2, t2 => p1, c1, c2, p2
func Circumcircle ¶
Circumcircle returns the circle (center and radius) that passes through the three points.
func Coincident ¶
True if lines are on the same infinite line
func Collinear ¶
Collinear returns true if three points are on a line (i.e. if the area of the resultant triangle is 0)
func CrossProduct ¶
CrossProduct returns the cross product of the three points. Since the inputs are all in the x-y plane (i.e. z = 0), only the magnitude of the resultant z vector is returned (the x and y vectors are both 0).
func DeCasteljau ¶
DeCasteljau uses de Casteljau's algorithm for degree n curves and returns the point and the tangent of the line it's traversing. {p1, c1, c2, c3, ..., p2}
func Dejitter ¶
Dejitter takes a list of points, greater than three long, and removes the one that forms the smallest area triangle with its adjacent points. If closed is true then the end points are candidates for elimination too.
func DistanceESquared ¶
DistanceESquared returns the squared Euclidean distance between two points.
func DistanceESquaredN ¶
DistanceESquaredN returns the squared Euclidean distance between two points.
func DistanceToLineSquared ¶
DistanceToLineSquared calculates the squared Euclidean length of the normal from a point to the line. Returns the distance squared, the line intercept and the t value.
func DotProduct ¶
DotProduct returns the dot product of the two lines, p1-p2 and p3-p4.
func DotProductAngle ¶
DotProductAngle returns the angle between two lines using the dot product method. The result is in the range [0,Pi].
func IntersectionTVals ¶
IntersectionTVals obtains values of t for each line for where they intersect. Actual intersection => both are in [0,1]
func IntersectionTValsP ¶
IntersectionTValsP obtains values of t for each line for where they intersect. Actual intersection => both are in [0,1]
func InverseBarycentric ¶
InverseBarycentric converts Barycentric weights plus the three non-coincident points back into a point on the plane.
func Kappa1 ¶
Kappa1 calculates curvature - note curve must have 2nd order derivative. Radius of curvature at t is 1/kappa(t)
func KappaC ¶
KappaC estimates kappa from three points by calculating the center of the circumcircle.
func KappaM ¶
KappaM calculates Menger curvature: 4*area / (d(p1, p2).d(p2s, p3).d(p3, p1)) Same result as KappaC but with more square roots...
func LineNormalToPoint ¶
LineNormalToPoint returns the point on the line that's normal to the specified point, in absolute terms and as a t value. The distance from the point to the line is returned too.
func NRM ¶
NRM is a modified Newton-Raphson root search that bails if t falls outside of the range [0,1] since the curve isn't defined there.
func PointInPoly ¶
PointInPoly returns true is a point is within the polygon defined by the list of vertices according to the setting of WindingRule (OddEven or NonZero).
func PointInTriangle ¶
PointInTriangle returns true if p is in the triangle formed by tp1, tp2 and tp3.
func PointInTriangleStrict ¶
PointInTriangleStrict returns true if p is strictly in the triangle formed by tp1, tp2 and tp3.
func PointOnLine ¶
PointOnLine returns if p is on the line and t for p if it is.
func PointOnTriangle ¶
PointOnTriangle returns true if p is on an edge of the triangle formed by tp1, tp2 and tp3.
func SplitCurve ¶
SplitCurve splits curve at t into two new curves such that the end of the lhs is the start of the rhs {p1, c1, c2, c3, ..., p2}
func TriArea ¶
TriArea returns the signed area of a triangle by finding the determinant of M = {{p1[0] p1[1] 1}
{p2[0] p2[1] 1}
{p3[0] p3[1] 1}}
func VecNormalize ¶
VecNormalize scales a vector to unit length.
Types ¶
type BezierCurve ¶
type BezierCurve struct {
Weights [][]float64
WeightsDt [][]float64
WeightsDt2 [][]float64
WeightsDt3 [][]float64
}
BezierCurve stores the derivative curve weights.
func NewBezierCurve ¶
func NewBezierCurve(weights [][]float64) *BezierCurve
NewBezierCurve creates a new BezierCurve with the weights of the first, second and third order derivatives of the supplied curve.
func (*BezierCurve) CurveDt2X ¶
func (bc *BezierCurve) CurveDt2X(t float64) float64
CurveDt2X returns the X value for the second order derivative of the curve at t.
func (*BezierCurve) CurveDt2Y ¶
func (bc *BezierCurve) CurveDt2Y(t float64) float64
CurveDt2Y returns the Y value for the second order derivative of the curve at t.
func (*BezierCurve) CurveDt3X ¶
func (bc *BezierCurve) CurveDt3X(t float64) float64
CurveDt3X returns the X value for the third order derivative of the curve at t.
func (*BezierCurve) CurveDt3Y ¶
func (bc *BezierCurve) CurveDt3Y(t float64) float64
CurveDt3Y returns the Y value for the third order derivative of the curve at t.
func (*BezierCurve) CurveDtX ¶
func (bc *BezierCurve) CurveDtX(t float64) float64
CurveDtX returns the X value for the derivative of the curve at t.
func (*BezierCurve) CurveDtY ¶
func (bc *BezierCurve) CurveDtY(t float64) float64
CurveDtY returns the Y value for the derivative of the curve at t.
func (*BezierCurve) CurveX ¶
func (bc *BezierCurve) CurveX(t float64) float64
CurveX returns the X value for the curve at t.
func (*BezierCurve) CurveY ¶
func (bc *BezierCurve) CurveY(t float64) float64
CurveY returns the Y value for the curve at t.
func (*BezierCurve) Kappa ¶
func (bc *BezierCurve) Kappa(t float64) float64
Kappa calculates the curvature at t. Radius of curvature at t is 1/kappa(t)