path

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2020 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultPrecision = 3 // how many decimal places do we want to consider
)
View Source
const MaxInt = int(^uint(0) >> 1)
View Source
const MinInt = -MaxInt - 1

Variables

This section is empty.

Functions

func DegreesToRadians

func DegreesToRadians(degrees float64) float64

func Distance

func Distance(p1 Point, p2 Point) float64

Distance finds the straightline distance between the two points distance will never be negative

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 IsEmptyPath

func IsEmptyPath(pth Path) bool

returns true if the path either has no segments, or contains only Move segments

func IsMove

func IsMove(seg Segment) bool

returns true if this segment is a move

func IsPoint00

func IsPoint00(p Point) bool

returns true if this point is at 0,0

func PathAttribute

func PathAttribute(attr string, p Path, so SegmentOperators) (interface{}, error)

func PointInBoundingBox

func PointInBoundingBox(topL, bottomR, point Point) bool

returns true if the givin point is within the bounding box points points that lie directly on the bounding box are considered inside

func PrecisionCompare

func PrecisionCompare(f1, f2 float64, precision int) int

compares two floats based on the passed in precision f1 == f2 => 0 f1 < f2 => -1 f1 > f2 => 1

func PrecisionEquals

func PrecisionEquals(f1, f2 float64, precision int) bool

determines equality based on the number of digits of precision

func PrecisionPointInBoundingBox

func PrecisionPointInBoundingBox(topL, bottomR, point Point, precision int) bool

func StringArrayDeDup

func StringArrayDeDup(a []string) []string

removes any duplicates maintaining ordering

func StringElipses

func StringElipses(str string, maxChars int) string

will trim a string to the requested length and add elipses

func SvgString

func SvgString(path Path, numDecimals int) string

Types

type BezierCurveOperators

type BezierCurveOperators struct {
	// 0.5?
	CurveIntersectionThreshold float64
}

the default implementation of curve operators.

func NewBezierCurveOperators

func NewBezierCurveOperators() BezierCurveOperators

func (BezierCurveOperators) BoundingBox

func (b BezierCurveOperators) BoundingBox(curve Curve) (topLeft, bottomRight Point, err error)

func (BezierCurveOperators) IntersectCurve

func (b BezierCurveOperators) IntersectCurve(curve1 Curve, curve2 Curve) ([]Point, error)

func (BezierCurveOperators) IntersectLine

func (b BezierCurveOperators) IntersectLine(curve Curve, line LineSegment) ([]Point, error)

func (BezierCurveOperators) IntersectProjectedLine

func (b BezierCurveOperators) IntersectProjectedLine(curve Curve, line LineSegment) ([]Point, error)

find the points where the given line *would* intersect if it were projected in either direction.

func (BezierCurveOperators) Offset

func (b BezierCurveOperators) Offset(curve Curve, distance float64) ([]Curve, error)

func (BezierCurveOperators) Split

func (b BezierCurveOperators) Split(curve Curve, point Point) ([]Curve, error)

type Curve

type Curve interface {
	Start() Point
	End() Point
	ControlStart() Point
	ControlEnd() Point
}

type CurveOperators

type CurveOperators interface {
	BoundingBox(curve Curve) (topLeft, bottomRight Point, err error)
	IntersectLine(curve Curve, line LineSegment) ([]Point, error)
	IntersectProjectedLine(curve Curve, line LineSegment) ([]Point, error)
	IntersectCurve(c1 Curve, c2 Curve) ([]Point, error)
	Split(curve Curve, point Point) ([]Curve, error)
	Offset(curve Curve, distance float64) ([]Curve, error)
}

type CurveSegment

type CurveSegment struct {
	StartPoint        Point
	ControlPointStart Point
	EndPoint          Point
	ControlPointEnd   Point
}

func (CurveSegment) Clone

func (c CurveSegment) Clone() Segment

func (CurveSegment) ControlEnd

func (c CurveSegment) ControlEnd() Point

func (CurveSegment) ControlStart

func (c CurveSegment) ControlStart() Point

func (CurveSegment) End

func (c CurveSegment) End() Point

func (CurveSegment) SetStart

func (c CurveSegment) SetStart(p Point) Segment

func (CurveSegment) Start

func (c CurveSegment) Start() Point

func (CurveSegment) SvgString

func (c CurveSegment) SvgString(numDecimals int) string

func (CurveSegment) UniqueString

func (c CurveSegment) UniqueString(numDecimals int) string

type DefaultSegmentOperators

type DefaultSegmentOperators struct {
	CurveOperators CurveOperators
	Precision      int
}

contains the segment operators for all the segments except some special Curve operators

func (DefaultSegmentOperators) BoundingBox

func (do DefaultSegmentOperators) BoundingBox(segment Segment) (topLeft, bottomRight Point, err error)

func (DefaultSegmentOperators) Intersect

func (do DefaultSegmentOperators) Intersect(s1, s2 Segment) ([]Point, error)

func (DefaultSegmentOperators) Join

func (do DefaultSegmentOperators) Join(s1, s2 Segment) ([]Segment, error)

func (DefaultSegmentOperators) JoinCurveAndLine

func (do DefaultSegmentOperators) JoinCurveAndLine(c1 CurveSegment, c2 LineSegment) ([]Segment, error)

func (DefaultSegmentOperators) JoinCurves

func (do DefaultSegmentOperators) JoinCurves(c1, c2 CurveSegment) ([]Segment, error)

func (DefaultSegmentOperators) JoinLineAndCurve

func (do DefaultSegmentOperators) JoinLineAndCurve(c1 LineSegment, c2 CurveSegment) ([]Segment, error)

joins the line and curve, by projecting the line until it intersects with the curve

func (DefaultSegmentOperators) JoinLines

func (do DefaultSegmentOperators) JoinLines(l1, l2 LineSegment) ([]Segment, error)

Joins the two lines by finding the intersection

func (DefaultSegmentOperators) Move

func (do DefaultSegmentOperators) Move(s Segment, amount Point) (Segment, error)

func (DefaultSegmentOperators) Offset

func (do DefaultSegmentOperators) Offset(segment Segment, distance float64) (ret []Segment, err error)

func (DefaultSegmentOperators) Reverse

func (do DefaultSegmentOperators) Reverse(segment Segment) (Segment, error)

func (DefaultSegmentOperators) Split

func (do DefaultSegmentOperators) Split(segment Segment, point Point) (ret []Segment, err error)

func (DefaultSegmentOperators) TransformPoints

func (do DefaultSegmentOperators) TransformPoints(s Segment, pt PointTransform) (Segment, error)

type Draw

type Draw struct {
	// contains filtered or unexported fields
}

a draw is decorator of a Path which has convenient drawing operations

func NewDraw

func NewDraw() *Draw

func (*Draw) AddSegment

func (d *Draw) AddSegment(seg Segment)

tries to safely add a new segment if this segment doesnt start from the current position, then the segment start is mutated to the current position

func (*Draw) AddSegments

func (d *Draw) AddSegments(segs []Segment)

func (*Draw) Circle

func (d *Draw) Circle(r float64)

Draws an approximation of a circle at the current location This uses bezier curves so a perfect circle is impossible. origin is top left

func (*Draw) CurrentPosition

func (d *Draw) CurrentPosition() Point

func (*Draw) CurveTo

func (d *Draw) CurveTo(controlPointStart, controlPointEnd, point Point)

draws a Curve this is the Cubic Bezier

func (*Draw) HLineTo

func (d *Draw) HLineTo(dx float64)

Draws a horizontal line from the current position to the passed in position

func (*Draw) LineByAngle

func (d *Draw) LineByAngle(length, angle float64)

Draws a line from the current position based on length and angle

func (*Draw) LineTo

func (d *Draw) LineTo(point Point)

line to

func (*Draw) MoveTo

func (d *Draw) MoveTo(point Point)

func (*Draw) Path

func (d *Draw) Path() Path

Returns the underlying Path

func (*Draw) QCurveTo

func (d *Draw) QCurveTo(controlPoint, point Point)

Quadratic curve. Note - we convert to cubic curves

func (*Draw) Rect

func (d *Draw) Rect(w, h float64)

draws a rectangle from the current location, ending in the current location

func (*Draw) RelCurveTo

func (d *Draw) RelCurveTo(controlPointStartDxDy, controlPointEndDxDy, dxdy Point)

draws a Relative Curve this is the Cubic Bezier

func (*Draw) RelHLineTo

func (d *Draw) RelHLineTo(dx float64)

Draws a horizontal line relative to the current position

func (*Draw) RelLineTo

func (d *Draw) RelLineTo(dxdy Point)

Relative line to

func (*Draw) RelMoveTo

func (d *Draw) RelMoveTo(dxdy Point)

moves relative to current cursor position

func (*Draw) RelQCurveTo

func (d *Draw) RelQCurveTo(controlPointEndDxDy, dxdy Point)

relative Quadratic curve. Note - we convert to cubic curves

func (*Draw) RelRoundedCornerTo

func (d *Draw) RelRoundedCornerTo(dxdy Point, dxdyCorner Point, radius float64)

func (*Draw) RelSmoothCurveTo

func (d *Draw) RelSmoothCurveTo(controlPointEndDxDy, dxdy Point)

draws a smooth curve. Note that this is converted to a Cubic Bezier

func (*Draw) RelVLineTo

func (d *Draw) RelVLineTo(dy float64)

Draws a vertical line relative to the current position

func (*Draw) RoundedCornerTo

func (d *Draw) RoundedCornerTo(to Point, corner Point, radius float64)

Round corner draws a 90 deg corner using a curve

func (*Draw) SmoothCurveTo

func (d *Draw) SmoothCurveTo(controlPointEnd, point Point)

draws a smooth curve. Note that this is converted to a Cubic Bezier

func (*Draw) SvgPath

func (d *Draw) SvgPath(svg string) error

adds all segments from an svg path string

func (*Draw) ToAbsPosition

func (d *Draw) ToAbsPosition(dxdy Point) Point

func (*Draw) VLineTo

func (d *Draw) VLineTo(dy float64)

Draws a vertical line from the current position to the passed in position

type LineSegment

type LineSegment struct {
	StartPoint Point
	EndPoint   Point
}

func NewLineSegmentAngle

func NewLineSegmentAngle(start Point, length, angle float64) LineSegment

creates a line segment based on start point, length and angle where a positive horizontal line is 0 degrees

func Parallel

func Parallel(segment LineSegment, distance float64) LineSegment

Given a line segment, and distance this gives a parrallel line, at 90 deg and distance d

func (LineSegment) Angle

func (l LineSegment) Angle() float64

the angle of the line in degrees. where a positive horizontal line is 0

func (LineSegment) Clone

func (l LineSegment) Clone() Segment

func (LineSegment) End

func (l LineSegment) End() Point

func (LineSegment) EvalX

func (l LineSegment) EvalX(x float64) float64

gets the value of Y for the given X

func (LineSegment) IsHorizontalPrecision

func (l LineSegment) IsHorizontalPrecision(precision int) bool

func (LineSegment) IsVerticalPrecision

func (l LineSegment) IsVerticalPrecision(precision int) bool

returns true if this line is vertical

func (LineSegment) Length

func (l LineSegment) Length() float64

func (LineSegment) PointAtDistance

func (l LineSegment) PointAtDistance(distance float64) Point

Finds the point at the specified distance from the start point in the direction of the end point..

func (LineSegment) SetStart

func (l LineSegment) SetStart(p Point) Segment

func (LineSegment) Slope

func (l LineSegment) Slope() float64

func (LineSegment) Start

func (l LineSegment) Start() Point

func (LineSegment) SvgString

func (l LineSegment) SvgString(numDecimals int) string

func (LineSegment) UniqueString

func (l LineSegment) UniqueString(numDecimals int) string

func (LineSegment) YIntercept

func (l LineSegment) YIntercept() float64

type MoveSegment

type MoveSegment struct {
	StartPoint Point
	EndPoint   Point
}

func (MoveSegment) Clone

func (m MoveSegment) Clone() Segment

func (MoveSegment) End

func (m MoveSegment) End() Point

func (MoveSegment) SetStart

func (m MoveSegment) SetStart(p Point) Segment

func (MoveSegment) Start

func (m MoveSegment) Start() Point

func (MoveSegment) SvgString

func (m MoveSegment) SvgString(numDecimals int) string

func (MoveSegment) UniqueString

func (m MoveSegment) UniqueString(numDecimals int) string

type Params

type Params interface {
	GetString(key string) string
}

type Path

type Path interface {
	Segments() []Segment
	AddSegments(seg ...Segment)
	Clone() Path
}

func MultiTransform

func MultiTransform(p Path, transforms ...PathTransform) (Path, error)

executes multiple transforms

func NewPath

func NewPath() Path

func NewPathFromSegments

func NewPathFromSegments(segments []Segment) Path

func NewPathFromSegmentsWithoutMove

func NewPathFromSegmentsWithoutMove(segments []Segment) Path

creates a new Path from the passed in segments without adding a move at the beginning.

func ParsePathFromSvg

func ParsePathFromSvg(path string) (Path, error)

parses a path from the SVG style string

func SplitPathOnMove

func SplitPathOnMove(pth Path) []Path

splits the path into an array of paths, where each path begins with a move operation

type PathAttr

type PathAttr string

defines the special points on a box

const (
	// positions based on the bounding box of the drawn shape
	// Note this will take whitespace into account
	TopLeft      PathAttr = "$TOP_LEFT"
	TopRight     PathAttr = "$TOP_RIGHT"
	TopMiddle    PathAttr = "$TOP_MIDDLE"
	BottomLeft   PathAttr = "$BOTTOM_LEFT"
	BottomRight  PathAttr = "$BOTTOM_RIGHT"
	BottomMiddle PathAttr = "$BOTTOM_MIDDLE"
	MiddleLeft   PathAttr = "$MIDDLE_LEFT"
	MiddleRight  PathAttr = "$MIDDLE_RIGHT"
	MiddleMiddle PathAttr = "$MIDDLE_MIDDLE"

	// the first and last visible pixel
	StartPoint PathAttr = "$START_POINT"
	EndPoint   PathAttr = "$END_POINT"

	// current cursor location
	Cursor PathAttr = "$CURSOR"

	// the first point in the path, this
	// differs from StartPoint because this could be
	// a Move.
	StartPosition PathAttr = "$START_POSITION"
	EndPosition   PathAttr = "$END_POSITION"

	Width  PathAttr = "$WIDTH"
	Height PathAttr = "$HEIGHT"

	Origin PathAttr = "$ORIGIN"
)

func ToPathAttr

func ToPathAttr(str string) (PathAttr, error)

func ToPathAttrFromPoint

func ToPathAttrFromPoint(point Point, precision int) PathAttr

type PathImpl

type PathImpl struct {
	// contains filtered or unexported fields
}

func (*PathImpl) AddSegments

func (p *PathImpl) AddSegments(seg ...Segment)

func (*PathImpl) Clone

func (p *PathImpl) Clone() Path

func (*PathImpl) Segments

func (p *PathImpl) Segments() []Segment

func (*PathImpl) SvgString

func (p *PathImpl) SvgString(decimals int) string

type PathTransform

type PathTransform interface {
	PathTransform(path Path) (Path, error)
}

transform a Path into a new Path

type Point

type Point struct {
	X float64
	Y float64
	// contains filtered or unexported fields
}

func BoundingBoxTrimWhitespace

func BoundingBoxTrimWhitespace(p Path, so SegmentOperators) (topLeft, bottomRight Point, err error)

func BoundingBoxWithWhitespace

func BoundingBoxWithWhitespace(p Path, so SegmentOperators) (topLeft, bottomRight Point, err error)

gets the bounding box, including any trailing or leading whitespace

func GetStartAndEnd

func GetStartAndEnd(segments []Segment) (start, end Point)

returns the start point and the end point of the segment array note that the start point is the target point of the first segment I.e. if the segments start with a move then the start point is where it moves TO

func HorizontalIntercepts

func HorizontalIntercepts(p Path, y float64, so SegmentOperators) ([]Point, error)

The ordered points (by increasing x) of where this path intercepts the given y axis

func LineIntersection

func LineIntersection(l1, l2 LineSegment, precision int) (p Point, success bool)

returns the point where the two lines would intersect, success is false if the lines are parrallel

func NewPoint

func NewPoint(x float64, y float64) Point

Creates a new point, will convert -0.0 to 0.0

func NewPointRounded

func NewPointRounded(x float64, y float64) Point

creates a new point, with the values rounded to 3 decimal places

func PathCursor

func PathCursor(p Path) Point

returns the current cursor location of the path.

func PointPathAttribute

func PointPathAttribute(pos PathAttr, p Path, so SegmentOperators) (Point, error)

func PolarToCartesian

func PolarToCartesian(r, theta float64) Point

func Rotate

func Rotate(degree float64, point Point) Point

Rotate the points by the given degrees. (clockwise)

func (Point) Clone

func (p Point) Clone() Point

func (Point) Equals

func (p Point) Equals(other Point) bool

checks if the points are equal this check for exact equality (the floats must be the same)

func (Point) EqualsPrecision

func (p Point) EqualsPrecision(other Point, numDecimals int) bool

checks if the points are equal based to the requested number of decimal places

func (Point) String

func (p Point) String() string

func (Point) StringPrecision

func (p Point) StringPrecision(numDecimals int) string

func (Point) StringRounded

func (p Point) StringRounded() string

func (Point) ToDynMap

func (p Point) ToDynMap() *dynmap.DynMap

type PointTransform

type PointTransform func(p Point) Point

type Segment

type Segment interface {
	// SetStart(p Point) Segment
	Start() Point
	End() Point
	SvgString(numDecimals int) string
	UniqueString(numDecimals int) string
	Clone() Segment
}

func FixHeadMove

func FixHeadMove(seg []Segment) []Segment

if the first move does not start at 0,0 then fix it

func HeadMove

func HeadMove(seg []Segment) []Segment

func KnifeCut

func KnifeCut(seg Segment, knife Segment, so SegmentOperators) ([]Segment, error)

will split the segment wherever the knife segment intersects

func SetSegmentStart

func SetSegmentStart(segment Segment, start Point) (Segment, error)

func Tail

func Tail(seg []Segment) Segment

returns the last Segment in the list, or nil

func TrimFirst

func TrimFirst(seg []Segment) []Segment

func TrimLast

func TrimLast(seg []Segment) []Segment

returns a segment list without the last element if empty, will return empty

func TrimMove

func TrimMove(seg []Segment) []Segment

removes any Moves in the front or tail of the list.

func TrimTailMove

func TrimTailMove(seg []Segment) []Segment

removes any Move segments from the tail of the list

type SegmentOperators

type SegmentOperators interface {
	Reverse(segment Segment) (Segment, error)
	BoundingBox(segment Segment) (topLeft, bottomRight Point, err error)
	// find the intersection between the two segments
	Intersect(segment1, segment2 Segment) ([]Point, error)
	// Splits the segment at the requested point
	// if point does not exist on the segment then ??
	Split(segment Segment, point Point) ([]Segment, error)
	Offset(segment Segment, distance float64) ([]Segment, error)
	// Joins the two disjoint segments into on continuous path
	// possibly returns multiple segments
	Join(segment1, segment2 Segment) ([]Segment, error)
	// transforms all the points associated with the segment
	TransformPoints(segment Segment, pt PointTransform) (Segment, error)
}

various operations we should be able to perform on segments

func NewSegmentOperators

func NewSegmentOperators() SegmentOperators

type SegmentTransform

type SegmentTransform interface {
	SegmentTransform(segment Segment) Segment
}

transforma a segment

type SvgCommand

type SvgCommand rune
const (
	// These are the only supported Command types
	Move         SvgCommand = 'M'
	Line         SvgCommand = 'L'
	CurveCommand SvgCommand = 'C'
	ClosePath    SvgCommand = 'Z'
)

Jump to

Keyboard shortcuts

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