stroke

package module
v0.0.0-...-24ef450 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2023 License: MIT, Unlicense Imports: 3 Imported by: 4

README

This is a Go package to generate stroke outlines for cubic Bezier paths.

This package was originally developed for use with Gio, but it is not tied to any particular graphics library or GUI toolkit.

Instead of "flattening" cubic curves to sequences of quadratic curves or line segments like many path-stroking implementations do, it uses cubic curves all the way through. Linear and quadratic segments are converted to cubic for uniformity.

A path is represented by a [][]Segment. Each []Segment represents a single contour of the path (open or closed), with each Segment in the contour starting where the previous one ended. (In the [][]Segment returned by Stroke, starting points and ending points are not guaranteed to coincide exactly. Callers should fill in the gaps with straight lines, if they occur.)

Graphics libraries generally consider the line's dash pattern to be part of the stroke style, but this package treats breaking a path into dashes as a separate operation:

dashed := stroke.Dash(p, []float32{1, 2}, 0)
outline := stroke.Stroke(dashed, stroke.Options{Width: 2})

Documentation

Overview

The stroke package provides functions for stroking cubic bezier paths.

Unlike many path-stroking implementations, which "flatten" cubic curves to sequences of quadratic curves, or even of straight line segments, it works with cubic curves all the way through. This results in significantly fewer segments in the output.

Many of the algorithms come from https://pomax.github.io/bezierinfo/

Index

Constants

View Source
const (
	FlatCap       CapStyle = 0
	RoundCap               = 1
	SquareCap              = 2
	TriangularCap          = 3
)
View Source
const (
	MiterJoin JoinStyle = 0
	RoundJoin           = 1
	BevelJoin           = 2
)

Variables

This section is empty.

Functions

func Dash

func Dash(path [][]Segment, pattern []float32, phase float32) [][]Segment

Dash returns a dashed version of path, according to the pattern and phase specified.

func Stroke

func Stroke(path [][]Segment, opt Options) [][]Segment

Stroke returns outlines for the contours in path. Both in the parameter and in the return value, each element of the slice is a contour (a connected series of segments).

Types

type CapStyle

type CapStyle int

type JoinStyle

type JoinStyle int

type Options

type Options struct {
	Width      float32
	Cap        CapStyle
	Join       JoinStyle
	MiterLimit float32
}

type Point

type Point struct {
	X, Y float32
}

A Point is a two dimensional point.

func Pt

func Pt(x, y float32) Point

Pt is shorthand for Point{X: x, Y: y}.

func (Point) Add

func (p Point) Add(p2 Point) Point

Add return the point p+p2.

func (Point) Div

func (p Point) Div(s float32) Point

Div returns the vector p/s.

func (Point) Mul

func (p Point) Mul(s float32) Point

Mul returns p scaled by s.

func (Point) String

func (p Point) String() string

String return a string representation of p.

func (Point) Sub

func (p Point) Sub(p2 Point) Point

Sub returns the vector p-p2.

type Segment

type Segment struct {
	Start    Point
	CP1, CP2 Point
	End      Point
}

A Segment is a cubic bezier curve (or a line segment that has been converted into a bezier curve).

func AppendArc

func AppendArc(dst []Segment, start, center Point, angle float32) []Segment

AppendArc appends one or more segments to dst, forming an arc of a circle starting at start, centered at center, and extending angle radians counterclockwise. For a clockwise arc, use a negative angle.

func AppendEllipticalArc

func AppendEllipticalArc(dst []Segment, start, f1, f2 Point, angle float32) []Segment

AppendEllipticalArc appends one or more segments to dst, forming an arc of an ellipse starting at start, with foci at f1 and f2, and extending angle radians counterclockwise. For a clockwise arc, use a negative angle.

func ArcSegment

func ArcSegment(start, center Point, angle float32) Segment

ArcSegment returns a Segment that approximates an arc of a circle, starting at start, centered at center, and extending angle radians counterclockwise. For a clockwise arc, use a negative angle. The accuracy of the approximation drops off as the angle increases; using a single segment for an arc longer than half a circle (angle > π) is not recommended (use AppendArc instead).

func LinearSegment

func LinearSegment(a, b Point) Segment

LinearSegment returns a line segment connecting a and b, in the form of a cubic bezier curve with collinear control points.

func QuadraticSegment

func QuadraticSegment(start, cp, end Point) Segment

QuadraticSegment converts a quadratic bezier segment to a cubic one.

func (Segment) Split

func (s Segment) Split(t float32) (Segment, Segment)

Split splits s into two segments with de Casteljau's algorithm, at t.

func (Segment) Split2

func (s Segment) Split2(t1, t2 float32) Segment

Split2 returns the section of s that lies between t1 and t2.

Jump to

Keyboard shortcuts

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