Documentation ¶
Overview ¶
Package graphics2d contains types and functions for 2D graphics rendering.
If all you want to do is render a point, line, arc or path of a particular color into an image, then use the draw functions which hide all of the mechanics described below from you:
DrawPoint DrawLine DrawArc DrawPath
However, to take full advantage of the package, here's a few more things you'll need to know.
The lowest type is a Path. Paths start from a location and add steps using a number of control points. The number of control points determines the polynomial order of the step: 0 - line; 1 - quadratic; 2 - cubic; 3 - quartic; ... A path can be closed which forces a line from the start location to the last. Once a path is closed, no more steps can be added. Unlike other implementations, a path here represents a single stroke (pen down). There is no move (pen up) step.
The Shape type is a container for paths and represents something that can be filled and rendered. When paths are rendered, they must be fillable (i.e. closed), so they are forced closed by the renderer.
This can lead to unexpected results...
If a shape is rendered with a pen (see DrawShape) and the pen has a stroke associated with it, then open paths are not an issue since strokes return closed paths.
A pen is a combination of a color (or image), a stroke and a transformation from shape space to image space. If you're defining operations in the same space as the image, then the transformation is simply the identity transformation. If, however, say your operations are in a space [0, 1]^2, then you'd specify a transformation that maps [0, 1] => [0, width-1] etc. (i.e. scale by image width and height). Note that this transformation is applied *after* the stroke, so for a 1 pixel wide stroke the stroke width would be 1 / width. You don't have to use pens, you can use the RenderShape, PathProcessor, image filler and transformation functions directly. Pens just provide a convenient abstraction that hides path processing.
Note that if the image is written to every graphics operation (as it is with the Draw*() functions), this can kill performance as the image defined by the shape's bounds is written every time. It's better to collect all the operations associated with a color in a shape and then render that shape once.
The PathProcessor interface is where the magic happens. Given a path, a function implementing this interface returns a collection of paths derived from it. This allows for stroking, dashing and a variety of other possibilities:
CapsProc - adds shapes at the start and end of a path CompoundProc - allows multiple path processors to be run in sequence CurvesToLinesProc - replaces curved steps with lines between the points DashProc - wraps SnipProc to produce a dashed path FlattenProc - wraps Path.Flatten JitterProc - randomly move segment endpoints by some percentage of the segment's length LineProc - wraps Path.Line MunchProc - converts a path into length sized line paths OpenProc - wraps Path.Open PointsProc - adds shapes at the start of each step and the end of the last step ReverseProc - wraps Path.Reverse RoundedProc - rounds the corners of adjacent line segments in a path SimplifyProc - wraps Path.Simplify ShapesProc - distributes shapes along a path separated by some distance SnipProc - cuts up a path into smaller pieces according to a pattern SplitProc - splits each step into its own path StrokeProc - creates a fixed width outline of a path with options for the cap and join styles TraceProc - creates a new path by tracing the normals of the path at a fixed distance TransformProc - wraps Path.Transform
Shapes are rendered with the render functions. Paths are forced closed when rendered (see shapes above). Convenience methods are provided for rendering with a single color or an image (see also pens, above). The full render function allows a clip mask to be supplied, and the draw.Op to be specified.
The Aff3 type provides the ability to specify affine transforms on Paths and Shapes.
Utility functions are provided to generate common forms of paths:
Point Line PolyLine Curve PolyCurve Arc ArcFromPoint PolyArcFromPoint Circle Ellipse EllipticalArc EllipticalArcFromPoint Lune RegularPolygon ReentrantPolygon IrregularPolygon
A shape function is provided to capture glyphs:
GlyphToShape
Index ¶
- Constants
- Variables
- func CPSafe(part [][]float64) bool
- func CapButt(p1 [][]float64, p []float64, p2 [][]float64) [][][]float64
- func CapHead(p1 [][]float64, p []float64, p2 [][]float64) [][][]float64
- func CapInvRound(p1 [][]float64, p []float64, p2 [][]float64) [][][]float64
- func CapRound(p1 [][]float64, p []float64, p2 [][]float64) [][][]float64
- func CapSquare(p1 [][]float64, p []float64, p2 [][]float64) [][][]float64
- func CapTail(p1 [][]float64, p []float64, p2 [][]float64) [][][]float64
- func DrawArc(dst draw.Image, start, center []float64, radians float64, pen *Pen)
- func DrawClippedShape(dst draw.Image, shape, clip *Shape, pen *Pen)
- func DrawLine(dst draw.Image, start, end []float64, pen *Pen)
- func DrawPath(dst draw.Image, path *Path, pen *Pen)
- func DrawPoint(dst draw.Image, at []float64, pen *Pen)
- func DrawShape(dst draw.Image, shape *Shape, pen *Pen)
- func FillClippedShape(dst draw.Image, shape, clip *Shape, pen *Pen)
- func FillPath(dst draw.Image, path *Path, pen *Pen)
- func FillShape(dst draw.Image, shape *Shape, pen *Pen)
- func FlattenPart(d float64, part [][]float64) [][][]float64
- func I266ToF64(fi fixed.Int26_6) float64
- func InvalidPoint(p []float64) bool
- func JoinBevel(p1 [][]float64, p []float64, p2 [][]float64) [][][]float64
- func JoinRound(p1 [][]float64, p []float64, p2 [][]float64) [][][]float64
- func Lerp(t float64, p1, p2 []float64) []float64
- func MakeArcParts(cx, cy, r, offs, ang float64) [][][]float64
- func PartLength(d float64, part [][]float64) float64
- func PartsIntersection(part1, part2 [][]float64, d float64) []float64
- func PointCircle(pt []float64, w float64) [][][]float64
- func PointDiamond(pt []float64, w float64) [][][]float64
- func PointSquare(pt []float64, w float64) [][][]float64
- func RenderClippedShape(dst draw.Image, shape, clip *Shape, filler image.Image)
- func RenderColoredShape(dst draw.Image, shape *Shape, fill color.Color)
- func RenderShape(dst draw.Image, shape *Shape, filler image.Image)
- func RenderShapeAlpha(shape *Shape) *image.Alpha
- func RenderShapeExt(dst draw.Image, drect image.Rectangle, shape *Shape, filler image.Image, ...)
- func ReverseParts(parts [][][]float64) [][][]float64
- func ReversePoints(cp [][]float64) [][]float64
- func SimplifyExtremities(part [][]float64) [][][]float64
- func SimplifyPart(part [][]float64) [][][]float64
- func StringToShape(tfont *sfnt.Font, str string) (*Shape, []*Shape, error)
- type Aff3
- func BoxTransform(x1, y1, x2, y2, h, x1p, y1p, x2p, y2p, hp float64) *Aff3
- func CreateTransform(x, y, scale, rotation float64) *Aff3
- func FlipY(height float64) *Aff3
- func LineTransform(x1, y1, x2, y2, x1p, y1p, x2p, y2p float64) *Aff3
- func NewAff3() *Aff3
- func Reflect(x1, y1, x2, y2 float64) *Aff3
- func Rotate(th float64) *Aff3
- func RotateAbout(th, ax, ay float64) *Aff3
- func Scale(sx, sy float64) *Aff3
- func ScaleAbout(sx, sy, ax, ay float64) *Aff3
- func ScaleAndInset(width, height, iwidth, iheight float64, fix bool, bb [][]float64) *Aff3
- func Shear(shx, shy float64) *Aff3
- func ShearAbout(shx, shy, ax, ay float64) *Aff3
- func Translate(x, y float64) *Aff3
- func (a *Aff3) Apply(pts ...[]float64) [][]float64
- func (a *Aff3) Concatenate(aff Aff3) *Aff3
- func (a *Aff3) Copy() *Aff3
- func (a *Aff3) Determinant() float64
- func (a *Aff3) Identity() bool
- func (a *Aff3) InverseOf() (*Aff3, error)
- func (a *Aff3) Invert() error
- func (a *Aff3) PreConcatenate(aff Aff3) *Aff3
- func (a *Aff3) QuadrantRotate(n int) *Aff3
- func (a *Aff3) QuadrantRotateAbout(n int, ax, ay float64) *Aff3
- func (a *Aff3) Reflect(x1, y1, x2, y2 float64) *Aff3
- func (a *Aff3) Rotate(th float64) *Aff3
- func (a *Aff3) RotateAbout(th, ax, ay float64) *Aff3
- func (a *Aff3) Scale(sx, sy float64) *Aff3
- func (a *Aff3) ScaleAbout(sx, sy, ax, ay float64) *Aff3
- func (a *Aff3) Shear(shx, shy float64) *Aff3
- func (a *Aff3) ShearAbout(shx, shy, ax, ay float64) *Aff3
- func (a *Aff3) String() string
- func (a *Aff3) Translate(x, y float64) *Aff3
- type ArcStyle
- type BoxerProc
- type CapsProc
- type CompoundProc
- type CurveProc
- type CurveStyle
- type CurvesToLinesProc
- type DashProc
- type FSnipProc
- type FlattenProc
- type HandDrawnProc
- type JitterProc
- type LineProc
- type LinesProc
- type MPDProc
- type MiterJoin
- type MunchProc
- type OpenProc
- type OvalCap
- type Path
- func Arc(c []float64, r, offs, ang float64, s ArcStyle) *Path
- func ArcFromPoint(pt, c []float64, ang float64, s ArcStyle) *Path
- func ArcFromPoints(a, b, c []float64, s ArcStyle) *Path
- func Circle(c []float64, r float64) *Path
- func ConcatenatePaths(paths ...*Path) (*Path, error)
- func Curve(pts ...[]float64) *Path
- func Egg(c []float64, w, h, d, xang float64) *Path
- func Ellipse(c []float64, rx, ry, xang float64) *Path
- func EllipseFromPoints(p1, p2, c []float64) *Path
- func EllipticalArc(c []float64, rx, ry, offs, ang, xang float64, s ArcStyle) *Path
- func EllipticalArcFromPoint(pt, c []float64, rxy, ang, xang float64, s ArcStyle) *Path
- func EllipticalArcFromPoints(p1, p2, c []float64, s ArcStyle) *Path
- func EllipticalArcFromPoints2(p1, p2 []float64, rx, ry, xang float64, arc, swp bool, s ArcStyle) *Path
- func Equilateral(c []float64, s float64) *Path
- func ExtendLine(pt1, pt2 []float64, bounds [][]float64) *Path
- func IrregularEllipse(c []float64, rx1, rx2, ry1, ry2, disp, xang float64) *Path
- func IrregularPolygon(cp []float64, r float64, n int, nr bool) *Path
- func Line(pt1, pt2 []float64) *Path
- func Lune(c []float64, r0, r1, r2, th float64) *Path
- func Nellipse(l float64, foci ...[]float64) *Path
- func NewPath(start []float64) *Path
- func PartsToPath(parts ...[][]float64) *Path
- func Point(pt []float64) *Path
- func PolyArcFromPoint(pt []float64, cs [][]float64, angs []float64) *Path
- func PolyCurve(pts ...[][]float64) *Path
- func PolyLine(pts ...[]float64) *Path
- func Polygon(pts ...[]float64) *Path
- func Rectangle(c []float64, w, h float64) *Path
- func ReentrantPolygon(c []float64, r float64, n int, t, ang float64) *Path
- func RegularPolygon(pt1, pt2 []float64, n int) *Path
- func RightEgg(c []float64, h, d, xang float64) *Path
- func Square(c []float64, s float64) *Path
- func StringToPath(str string) *Path
- func (p *Path) AddStep(points ...[]float64) error
- func (p *Path) AddSteps(steps ...[][]float64) error
- func (p *Path) BoundingBox() [][]float64
- func (p *Path) Bounds() image.Rectangle
- func (p *Path) Close()
- func (p *Path) Closed() bool
- func (p *Path) Concatenate(paths ...*Path) error
- func (p *Path) Copy() *Path
- func (p *Path) Flatten(d float64) *Path
- func (p *Path) Length(flat float64) float64
- func (p *Path) Line() *Path
- func (p *Path) Lines(inccp bool) *Path
- func (p *Path) MarshalJSON() ([]byte, error)
- func (p *Path) Open() *Path
- func (p *Path) Parent() *Path
- func (p *Path) Parts() [][][]float64
- func (p *Path) PointInPath(pt []float64) bool
- func (p *Path) Poly() [][]float64
- func (p *Path) Process(proc PathProcessor) []*Path
- func (p *Path) ProjectPoint(pt []float64) ([]float64, float64, float64)
- func (p *Path) Reverse() *Path
- func (p *Path) Simplify() *Path
- func (p *Path) Steps() [][][]float64
- func (p *Path) String() string
- func (p *Path) Tangents() [][][]float64
- func (p *Path) Transform(xfm Transform) *Path
- func (p *Path) UnmarshalJSON(b []byte) error
- type PathProcessor
- type PathSnipProc
- type Pen
- func NewColoredPens(colors []color.Color, width float64) []*Pen
- func NewFilledPen(grad image.Image, width float64) *Pen
- func NewNamedPen(name string, width float64) *Pen
- func NewPen(color color.Color, width float64) *Pen
- func NewPens(color color.Color, n int, width, winc float64) []*Pen
- func NewProcessorPen(color color.Color, width float64, proc PathProcessor) *Pen
- func NewRandomHuePen(width float64) *Pen
- func NewRandomPen(width float64) *Pen
- type PointRot
- type PointsProc
- type RSCap
- type Renderable
- func (r *Renderable) AddClippedColoredShape(shape, clip *Shape, col color.Color, xfm *Aff3) *Renderable
- func (r *Renderable) AddClippedPennedShape(shape, clip *Shape, pen *Pen, xfm *Aff3) *Renderable
- func (r *Renderable) AddClippedShape(shape, clip *Shape, filler image.Image, xfm *Aff3) *Renderable
- func (r *Renderable) AddColoredShape(shape *Shape, col color.Color, xfm *Aff3) *Renderable
- func (r *Renderable) AddPennedShape(shape *Shape, pen *Pen, xfm *Aff3) *Renderable
- func (r *Renderable) AddRenderable(rend *Renderable, xfm *Aff3) *Renderable
- func (r *Renderable) AddShape(shape *Shape, filler image.Image, xfm *Aff3) *Renderable
- func (r *Renderable) Bounds() image.Rectangle
- func (r *Renderable) Image() *image.RGBA
- func (r *Renderable) Render(img draw.Image, xfm *Aff3)
- type ReverseProc
- type RoundedEdgeProc
- type RoundedProc
- type Shape
- func (s *Shape) AddPaths(paths ...*Path)
- func (s *Shape) AddShapes(shapes ...*Shape)
- func (s *Shape) BoundingBox() [][]float64
- func (s *Shape) Bounds() image.Rectangle
- func (s *Shape) Contains(pts ...[]float64) bool
- func (s *Shape) Copy() *Shape
- func (s *Shape) MarshalJSON() ([]byte, error)
- func (s *Shape) Mask() *image.Alpha
- func (s *Shape) Paths() []*Path
- func (s *Shape) PointInShape(pt []float64) bool
- func (s *Shape) Process(proc ShapeProcessor) []*Shape
- func (s *Shape) ProcessPaths(proc PathProcessor) *Shape
- func (s *Shape) String() string
- func (s *Shape) Transform(xfm Transform) *Shape
- func (s *Shape) UnmarshalJSON(b []byte) error
- type ShapeProc
- type ShapeProcessor
- type ShapesProc
- type SimpleStrokeProc
- type SimplifyProc
- type SnipProc
- type SplitProc
- type SquareWaveProc
- type StepsProc
- type StrokeProc
- type TraceProc
- type Transform
- type TransformProc
- type TriangleWaveProc
Constants ¶
const ( TwoPi = 2 * math.Pi HalfPi = math.Pi / 2 )
Mathematical constants.
const DefaultRenderFlatten = 0.6
DefaultRenderFlatten is the standard curve flattening value.
const Sqrt3 = 1.7320508075688772935274463415058723669428052538103806280558069794519330169088
Sqrt3 is the square root of 3
Variables ¶
var ( BlackPen = NewPen(color.Black, 1) DarkGrayPen = NewPen(color.DarkGray, 1) GrayPen = NewPen(color.MidGray, 1) LightGrayPen = NewPen(color.LightGray, 1) WhitePen = NewPen(color.White, 1) RedPen = NewPen(color.Red, 1) GreenPen = NewPen(color.Green, 1) BluePen = NewPen(color.Blue, 1) YellowPen = NewPen(color.Yellow, 1) MagentaPen = NewPen(color.Magenta, 1) CyanPen = NewPen(color.Cyan, 1) OrangePen = NewPen(color.Orange, 1) BrownPen = NewPen(color.Brown, 1) )
Predefined pens.
var RenderFlatten = DefaultRenderFlatten
RenderFlatten is the curve flattening value used when rendering.
var SafeFraction float64 = -1
SafeFraction if greater than 0 causes Simplify to perform a check of the mid-point against the part centroid. If the two are within SafeFraction of the distance from p[0] to the centroid then no further subdivision of the curve is performed.
Functions ¶
func CPSafe ¶
CPSafe returns true if all the control points are on the same side of the line formed by start and the last part points and the point at t = 0.5 is close to the centroid of the part.
func CapInvRound ¶
CapInvRound extends e1 and s1 and draws a semicircle that passes through p.
func CapTail ¶
CapTail draws an extended arrow tail (stroke width/2) from extended e1 to p and then to extended s1.
func DrawArc ¶
DrawArc renders an arc with the pen into the destination image. radians +ve CCW, -ve CW
func DrawClippedShape ¶
DrawClippedShape renders a shape with the pen against a clip shape into the destination image.
func FillClippedShape ¶
FillClippedShape renders a shape with the pen filler against a clipe shape and transform into the destination image.
func FillPath ¶
FillPath renders a path with the pen filler image and transform into the destination image.
func FillShape ¶
FillShape renders a shape with the pen filler and transform into the destination image.
func FlattenPart ¶
FlattenPart works by subdividing the curve until its control points are within d^2 (d squared) of the line through the end points.
func InvalidPoint ¶
InvalidPoint checks that both values are valid (i.e. not NaN)
func MakeArcParts ¶
MakeArcParts creates at least one cubic bezier that describes a curve from offs to offs+ang centered on {cx, cy} with radius r.
func PartLength ¶
PartLength returns the approximate length of a part by flattening it to the supplied degree of flatness.
func PartsIntersection ¶
PartsIntersection returns the location of where the two parts intersect or nil. Assumes the parts are the result of simplification. Uses a brute force approach for curves with d as the flattening value.
func PointCircle ¶
PointCircle renders points as circles/
func PointDiamond ¶
PointDiamond renders points as diamonds aligned in x/y.
func PointSquare ¶
PointSquare renders points as squares aligned in x/y.
func RenderClippedShape ¶
RenderClippedShape renders the supplied shape with the fill image into the destination image as masked by the clip shape.
func RenderColoredShape ¶
RenderColoredShape renders the supplied shape with the fill color into the destination image.
func RenderShape ¶
RenderShape renders the supplied shape with the fill image into the destination image.
func RenderShapeAlpha ¶
RenderShapeAlpha creates and returns the shape's alpha mask. The mask size and location are determined by the shape's bounds.
func RenderShapeExt ¶
func RenderShapeExt(dst draw.Image, drect image.Rectangle, shape *Shape, filler image.Image, fp image.Point, clip *image.Alpha, cp image.Point, op draw.Op)
RenderShapeExt renders the supplied shape with the fill and clip images into the destination image region using op.
func ReverseParts ¶
ReverseParts reverses the order (and points) of the supplied part slice.
func SimplifyExtremities ¶
SimplifyExtremities chops curve into pieces based on maxima, minima and inflections in x and y.
func SimplifyPart ¶
SimplifyPart recursively cuts the curve in half until CPSafe is satisfied.
Types ¶
type Aff3 ¶
type Aff3 [6]float64
Aff3 is a 3x3 affine transformation matrix in row major order, where the bottom row is implicitly [0 0 1].
m[3*r+c] is the element in the r'th row and c'th column.
func BoxTransform ¶
BoxTransform produces a transform that maps the line {p1, p2} to {p1', p2'} and scales the perpendicular by hp / h. Assumes neither of the lines nor h are degenerate.
func CreateTransform ¶
CreateTransform returns a transform that performs the requested translation, scaling and rotation based on {0, 0}.
func FlipY ¶
FlipY is a convenience function to create a transform that has +ve Y point up rather than down.
func LineTransform ¶
LineTransform produces a transform that maps the line {p1, p2} to {p1', p2'}. Assumes neither of the lines are degenerate.
func RotateAbout ¶
RotateAbout creates a rotation transform about a point.
func ScaleAbout ¶
ScaleAbout creates a scale transform about a point.
func ScaleAndInset ¶
ScaleAndInset produces a transform that will scale and translate a set of points bounded by bb so they fit inside the inset box described by width, height, iwidth, iheight located at {0, 0}. If fix is true then the aspect ratio is maintained.
func ShearAbout ¶
ShearAbout creates a shear transform about a point.
func (*Aff3) Concatenate ¶
Concatenate concatenates a transform to the transform.
func (*Aff3) Determinant ¶
Determinant calculates the transform's matrix determinant.
func (*Aff3) PreConcatenate ¶
PreConcatenate preconcatenates a transform to the transform.
func (*Aff3) QuadrantRotate ¶
QuadrantRotate adds a rotation (n * 90 degrees) to the transform. The rotation is about {0, 0}. It avoids rounding issues with the trig functions.
func (*Aff3) QuadrantRotateAbout ¶
QuadrantRotateAbout adds a rotation (n * 90 degrees) about a point to the transform. It avoids rounding issues with the trig functions.
func (*Aff3) Reflect ¶
Reflect performs a reflection along the axis defined by the two non-coincident points.
func (*Aff3) RotateAbout ¶
RotateAbout adds a rotation about a point to the transform.
func (*Aff3) ScaleAbout ¶
ScaleAbout adds a scale about a point to the transform.
func (*Aff3) ShearAbout ¶
ShearAbout adds a shear about a point to the transform.
type ArcStyle ¶
type ArcStyle int
ArcStyle defines the type of arc - open, chord (closed) and pie (closed).
type BoxerProc ¶
type BoxerProc struct { Width float64 // Width of box Offs float64 // Offset from path of box Flat float64 // Flatten value }
BoxerProc is a path processor that converts a path into a set of boxes along the path with the specified width. If the offset is 0 then the box is centered on the path.
func NewBoxerProc ¶
NewBoxerProc returns a new BoxerProc path processor.
type CapsProc ¶
CapsProc contains a trio of shapes, one which will be placed using the start at the path, one at each step and the last, at the end. If either shape is nil, then it is skipped. The rotation flag indicates if the shapes should be rotated relative to the path's tangent at that point.
type CompoundProc ¶
type CompoundProc struct { Procs []PathProcessor Concatenate bool // If set, concatenate processed paths into a single path after every processor }
CompoundProc applies a collection of PathProcessors to a path.
func NewCompoundProc ¶
func NewCompoundProc(procs ...PathProcessor) *CompoundProc
NewCompoundProc creates a new CompoundProcessor with the supplied path processors.
func (*CompoundProc) Process ¶
func (cp *CompoundProc) Process(p *Path) []*Path
Process implements the PathProcessor interface.
type CurveProc ¶
type CurveProc struct { Scale float64 Style CurveStyle }
CurveProc replaces the steps on a path with cubics. The locations of the control points are controlled by the Style setting and whether or not the path is closed.
type CurveStyle ¶
type CurveStyle int
CurveStyle determines how the curve behaves relative to the path points. With Bezier, the path will intersect the mid-point of each path step. With Catmul, the path will intersect point.
const ( Bezier CurveStyle = iota Quad CatmullRom )
Constants for curve styles.
type CurvesToLinesProc ¶
type CurvesToLinesProc struct {
SkipCP bool
}
CurvesToLinesProc takes a path and converts all of the points to lines.
func (*CurvesToLinesProc) Process ¶
func (clp *CurvesToLinesProc) Process(p *Path) []*Path
Process implements the PathProcessor interface.
type DashProc ¶
type DashProc struct {
Snip *FSnipProc
}
DashProc contains the dash pattern and offset. The dash pattern represents lengths of pen down, pen up, ... and is in the same coordinate system as the path. The offset provides the ability to start from anywhere in the pattern.
func NewDashProc ¶
NewDashProc creates a new dash path processor with the supplied pattern and offset. If the pattern is odd in length then it is replicated to create an even length pattern.
type FSnipProc ¶
type FSnipProc struct { N int Pattern []float64 Flatten float64 State int // contains filtered or unexported fields }
FSnipProc contains the snip pattern and offset. The snip pattern represents lengths of state0, state1, ... stateN-1, and is in the same coordinate system as the path. The offset provides the ability to start from anywhere in the pattern.
func NewFSnipProc ¶
NewFSnipProc creates a new snip path processor with the supplied pattern and offset. If the pattern is not N in length then it is replicated to create a mod N length pattern.
type FlattenProc ¶
type FlattenProc struct {
Flatten float64
}
FlattenProc is a wrapper around Path.Flatten() and contains the minimum required distance to the control points.
func (*FlattenProc) Process ¶
func (fp *FlattenProc) Process(p *Path) []*Path
Process implements the PathProcessor interface.
type HandDrawnProc ¶
type HandDrawnProc struct {
Comp *CompoundProc
}
HandDrawnProc contains the compound path processor used to create a hand drawn look.
func NewHandDrawnProc ¶
func NewHandDrawnProc(l float64) *HandDrawnProc
NewHandDrawnProc takes the segment length to apply the MPD path processor to and returns a new HandDrawnProc path processor.
func (*HandDrawnProc) Process ¶
func (h *HandDrawnProc) Process(p *Path) []*Path
Process implements the PathProcessor interface.
type JitterProc ¶
type JitterProc struct {
Perc float64 // Percentage
}
JitterProc contains the percentage degree to which segment endpoints will be moved to their left or right (relative to their tangents) based on their length. Can be used with MunchProc to get a hand drawn look.
func (*JitterProc) Process ¶
func (j *JitterProc) Process(p *Path) []*Path
Process implements the PathProcessor interface.
type LinesProc ¶
type LinesProc struct {
IncludeCP bool
}
LinesProc replaces a path step with a line.
type MPDProc ¶
type MPDProc struct { Perc float64 // Percentage of step length used as initial displacement Itrs int // Number of iterations to perform Scale float64 // Multiplier (Hurst) used on displacement per iteration Rel bool // if set, normal is the relative one and not the original }
MPDProc contains the variables that control the degree to which a step is chopped up into smaller line segments. Unlike JitterProc, the step end points don't vary. Can be used with MunchProc to get a hand drawn look.
func NewMPDProc ¶
NewMPDProc creates an MPDProc with sensible parameters for iterations and Hurst.
type MiterJoin ¶
type MiterJoin struct { MiterLimit float64 MiterAltFunc func([][]float64, []float64, [][]float64) [][][]float64 }
MiterJoin describes the limit and alternative function to use when the limit is exceeded for a miter join.
func NewMiterJoin ¶
func NewMiterJoin() *MiterJoin
NewMiterJoin creates a default MiterJoin with the limit set to 10 degrees and the alternative function to JoinBevel.
type MunchProc ¶
type MunchProc struct {
Comp *CompoundProc
}
MunchProc contains the munching compound path processor.
func NewMunchProc ¶
NewMunchProc creates a munching path processor. It calculates points along a path spaced l apart and creates new paths that join the points with lines.
type OvalCap ¶
type OvalCap struct { Rxy float64 // Ratio of Rx to Ry Offs float64 // Offset from center line [-1,1] -1 = LHS, 0 = centerline, 1 = RHS }
OvalCap contains the ratio of rx to ry for the oval and a center line offset
func (*OvalCap) CapInvOval ¶
CapInvOval creates an inverted half oval with rx = w/2 and ry = rxy * rx Offs is ignored
type Path ¶
type Path struct {
// contains filtered or unexported fields
}
Path contains the housekeeping necessary for path building.
func Arc ¶
Arc returns a path with an arc centered on c with radius r from offs in the direction and length of ang.
func ArcFromPoint ¶
ArcFromPoint returns a path describing an arc starting from pt based on c and ang.
func ArcFromPoints ¶
ArcFromPoints returns a path describing an arc passing through a, b and c such that the arc starts at a, passes through b and ends at c.
func ConcatenatePaths ¶
ConcatenatePaths concatenates all the paths into a new path. If any path is closed then an error is returned. If the paths aren't coincident, then they are joined with a line.
func Egg ¶
Egg uses IrregularEllipse to generate an egg shape with the specified width and height. The waist is specified as a percentage distance along the height axis (from the base). The egg is rotated by xang.
func Ellipse ¶
Ellipse returns a closed path describing an ellipse with rx and ry rotated by xang from the x axis.
func EllipseFromPoints ¶
EllipseFromPoints returns a path describing the smallest ellipse containing points p1 and p2. If p1, p2 and c are colinear and not equidistant then nil is returned.
func EllipticalArc ¶
EllipticalArc returns a path describing an arc starting at offs and ending at offs+ang on the ellipse defined by rx and ry rotated by xang from the x axis.
func EllipticalArcFromPoint ¶
EllipticalArcFromPoint returns a path describing an ellipse arc from a point. The ratio of rx to ry is specified by rxy.
func EllipticalArcFromPoints ¶
EllipticalArcFromPoints returns a path describing the smallest ellipse arc from a point p1 to p2 (ccw). If p1, p2 and c are colinear and not equidistant then nil is returned.
func EllipticalArcFromPoints2 ¶
func EllipticalArcFromPoints2(p1, p2 []float64, rx, ry, xang float64, arc, swp bool, s ArcStyle) *Path
EllipticalArcFromPoints2 provides a specification similar to that found in the SVG11 standard where the center is calculated from the given rx and ry values and flag specifications. See https://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
func Equilateral ¶
Equilateral returns a closed path describing an equliateral triangle with side s, centered on c.
func ExtendLine ¶
ExtendLine returns the line that passes through the bounds (or nil) defined by the line equation of pt1 and pt2.
func IrregularEllipse ¶
IrregularEllipse uses different rx and ry values for each quadrant of an ellipse. disp (-1,1) determines how far along either rx1 (+ve) or rx2 (-ve), ry2 extends from (ry1 extends from c).
func IrregularPolygon ¶
IrregularPolygon returns an n sided polgon guaranteed to be located within a circle of radius r centered on cp. If nr is set to true then polygon is forced to be non-reentrant.
func Lune ¶
Lune returns a closed path made up of two arcs with end points at c plus/minus r0 in y, all rotated by th. The arcs are calculated from the circumcircles of the two triangles defined by the end points, and c displaced by r1 or r2 in x.
func Nellipse ¶
Nellipse takes a slice of ordered foci, assumed to be on the hull of a convex polygon, and a length, and uses them to construct a Gardener's ellipse (an approximation that ignores foci within the hull). The closed path will be made up of twice as many arcs as there are foci. If the length isn't sufficient to wrap the foci, then nil is returned.
func PartsToPath ¶
PartsToPath constructs a new path by concatenating the parts.
func PolyArcFromPoint ¶
PolyArcFromPoint returns a path concatenating the arcs.
func Rectangle ¶
Rectangle returns a closed path describing a rectangle with sides w and h, centered on c.
func ReentrantPolygon ¶
ReentrantPolygon returns a closed path describing an n pointed star.
func RegularPolygon ¶
RegularPolygon returns a closed path describing an n-sided polygon given the initial edge.
func RightEgg ¶
RightEgg uses IrregularEllipse to generate an egg shape with the specified height and a semicircular base. The waist is specified as a percentage distance along the height axis (from the base). The egg is rotated by xang.
func StringToPath ¶
StringToPath converts a string created using path.String() back into a path. Returns nil if the string isn't parsable into a path.
func (*Path) AddStep ¶
AddStep takes an array of points and treats n-1 of them as control points and the last as a point on the curve.
func (*Path) BoundingBox ¶
BoundingBox calculates a bounding box that the Path is guaranteed to fit within. It's unlikely to be the minimal bounding box for the path since the control points are also included. If a tight bounding box is required then use CalcExtremities().
func (*Path) Bounds ¶
Bounds calculates a rectangle that the Path is guaranteed to fit within. It's unlikely to be the minimal bounding rectangle for the path since the control points are also included. If a tight bounding rectangle is required then use CalcExtremities().
func (*Path) Concatenate ¶
Concatenate adds the paths to this path. If any path is closed then an error is returned. If the paths aren't coincident, then they are joined with a line.
func (*Path) Flatten ¶
Flatten works by recursively subdividing the path until the control points are within d of the line through the end points.
func (*Path) Length ¶
Length returns the approximate length of a path by flattening it to the desired degree and summing the line steps.
func (*Path) Line ¶
Line reduces a path to a line between its endpoints. For a closed path or one where the start and endpoints are coincident, a single point is returned.
func (*Path) Lines ¶
Lines reduces a path to a line for every step. If inccp is set then the control points are included.
func (*Path) MarshalJSON ¶
MarshalJSON implements the encoding.json.Marshaler interface
func (*Path) PointInPath ¶
PointInPath returns if a point is contained within a closed path according to the setting of util.WindingRule. If the path is not closed then false is returned, regardless.
func (*Path) Poly ¶
Poly converts a path into a flat sided polygon. Returns an empty slice if the path isn't closed.
func (*Path) Process ¶
func (p *Path) Process(proc PathProcessor) []*Path
Process applies a processor to a path.
func (*Path) ProjectPoint ¶
ProjectPoint returns the point, it's t on the path closest to pt and the distance^2. Note t can be very non-linear.
func (*Path) Reverse ¶
Reverse returns a new path describing the current path in reverse order (i.e start and end switched).
func (*Path) Simplify ¶
Simplify breaks up a path into steps where for any step, its control points are all on the same side and its midpoint is well behaved. If a step doesn't meet the criteria, it is recursively subdivided in half until it does.
func (*Path) Tangents ¶
Tangents returns the normalized start and end tangents of every part in the path. [part]start/end[normalized x/y]
func (*Path) Transform ¶
Transform applies an affine transform to the points in a path to create a new one.
func (*Path) UnmarshalJSON ¶
UnmarshalJSON implements the encoding.json.Unmarshaler interface
type PathProcessor ¶
PathProcessor defines the interface required for function passed to the Process function in Path.
type PathSnipProc ¶
PathSnipProc contains the snip path.
func NewPathSnipProc ¶
func NewPathSnipProc(path *Path) *PathSnipProc
NewPathSnipProc creates a new path snip path processor with the supplied path.
func (*PathSnipProc) Process ¶
func (psp *PathSnipProc) Process(p *Path) []*Path
Process implements the PathProcessor interface.
type Pen ¶
type Pen struct { Filler image.Image Stroke PathProcessor Xfm *Aff3 }
Pen describes the color/image, stroke and shape to image transform to use when rendering shapes. If Stroke is nil then the shape's paths are used as is and forced closed (i.e. this is a fill). If Xfm is nil then the identity xfm is assumed.
func NewColoredPens ¶
NewColoredPens constructs a slice of n pens with the given colors and width.
func NewFilledPen ¶
NewFilledPen returns a pen that will render a shape with the given pen width and a random hued color into an image.
func NewNamedPen ¶
NewNamedPen returns a pen that will render a shape with the given width and named color into an image. If the name is not matched then a black pen will be returned.
func NewPen ¶
NewPen returns a pen that will render a shape with the given pen width and color into an image.
func NewPens ¶
NewPens constructs a slice of n pens with the given color starting at width and increasing by winc.
func NewProcessorPen ¶
func NewProcessorPen(color color.Color, width float64, proc PathProcessor) *Pen
NewProcessorPen returns a pen that will render a shape with the given pen width and color into an image after applying the supplied path processor.
func NewRandomHuePen ¶
NewRandomHuePen returns a pen that will render a shape with the given pen width and a random hued color into an image.
func NewRandomPen ¶
NewRandomPen returns a pen that will render a shape with the given pen width and a random color into an image.
func (*Pen) ChangeWidth ¶
ChangeWidth returns a new pen the width while preserving the other aspects of the pen's stroke. Note this only works for pens with a StrokeProc path processor.
func (*Pen) ScaleWidth ¶
ScaleWidth returns a new pen the scaled width while preserving the other aspects of the pen's stroke. Note this only works for pens with a StrokeProc path processor.
type PointsProc ¶
PointsProc contains a slice of shapes, one of which will be placed at the start of each step in the path and at the path end, if not closed. If any shape is nil, then it is skipped. The rotation flag indicates if the shapes should be rotated relative to the path's tangent at that point.
func NewPointsProc ¶
func NewPointsProc(shapes []*Shape, rot PointRot) *PointsProc
NewPointsProc creates a new points path processor with the supplied shapes and rotation flag.
func (*PointsProc) Process ¶
func (pp *PointsProc) Process(p *Path) []*Path
Process implements the PathProcessor interface.
type RSCap ¶
type RSCap struct {
Perc float64
}
RSCap contains the percentage [0,1] of the corner taken up by an arc. Perc = 1 is equivalent to CapRound Perc = 0, to CapSquare.
type Renderable ¶
Renderable represents a set of shapes and the images to fill them. In other words, enough information to be able to render something. This structure is used to build complex multicolored objects in a composable way.
func NewRenderable ¶
func NewRenderable(shape *Shape, filler image.Image, xfm *Aff3) *Renderable
NewRenderable creates a new instance with the given shape and filler image.
func (*Renderable) AddClippedColoredShape ¶
func (r *Renderable) AddClippedColoredShape(shape, clip *Shape, col color.Color, xfm *Aff3) *Renderable
AddClippedColoredShape adds the given shape, clip and color to the Renderable after being transformed.
func (*Renderable) AddClippedPennedShape ¶
func (r *Renderable) AddClippedPennedShape(shape, clip *Shape, pen *Pen, xfm *Aff3) *Renderable
AddClippedPennedShape adds the given shape, clip and pen to the Renderable after being transformed.
func (*Renderable) AddClippedShape ¶
func (r *Renderable) AddClippedShape(shape, clip *Shape, filler image.Image, xfm *Aff3) *Renderable
AddClippedShape adds the given shape, clip and filler to the Renderable after being transformed.
func (*Renderable) AddColoredShape ¶
func (r *Renderable) AddColoredShape(shape *Shape, col color.Color, xfm *Aff3) *Renderable
AddColoredShape adds the given shape and color to the Renderable after being transformed.
func (*Renderable) AddPennedShape ¶
func (r *Renderable) AddPennedShape(shape *Shape, pen *Pen, xfm *Aff3) *Renderable
AddPennedShape adds the given shape and pen to the Renderable after being transformed.
func (*Renderable) AddRenderable ¶
func (r *Renderable) AddRenderable(rend *Renderable, xfm *Aff3) *Renderable
AddRenderable allows another renderable to be concatenated (post transform) to the current one.
func (*Renderable) AddShape ¶
func (r *Renderable) AddShape(shape *Shape, filler image.Image, xfm *Aff3) *Renderable
AddShape adds the given shape and filler to the Renderable after being transformed.
func (*Renderable) Bounds ¶
func (r *Renderable) Bounds() image.Rectangle
Bounds returns the extent of the renderable.
func (*Renderable) Image ¶
func (r *Renderable) Image() *image.RGBA
Image renders the shapes in the renderable with their respective fillers.
type ReverseProc ¶
type ReverseProc struct{}
ReverseProc replaces a path with its reverse.
func (*ReverseProc) Process ¶
func (rp *ReverseProc) Process(p *Path) []*Path
Process implements the PathProcessor interface.
type RoundedEdgeProc ¶
RoundedEdgeProc is a path processor that replaces the parts of a path with an arc defined by the end points of the path and a third point normal to the part midpoint at either an absolute or relative (to the part length) distance from the midpoint. If Elip is set, then an elliptical arc of ry = d, rx = edge length / 2 is used instead.
func (*RoundedEdgeProc) Process ¶
func (rp *RoundedEdgeProc) Process(p *Path) []*Path
Process implements the PathProcessor interface.
type RoundedProc ¶
type RoundedProc struct {
Radius float64
}
RoundedProc replaces adjacent line segments in a path with line-arc-line where the radius of the arc is the minimum of Radius or the maximum allowable for the length of the shorter line segment. This ensures that the rounded corner doesn't end beyond the mid point of either line.
func (*RoundedProc) Process ¶
func (rp *RoundedProc) Process(p *Path) []*Path
Process implements the PathProcessor interface.
type Shape ¶
type Shape struct {
// contains filtered or unexported fields
}
Shape is a fillable collection of paths. For a path to be fillable, it must be closed, so paths added to the shape are forced closed on rendering.
func GlyphIndexToShape ¶
GlyphIndexToShape returns a shape containing the paths for glyph index x as found in the font. The path is in font units. Use font.UnitsPerEm() to calculate scale factors.
func GlyphToShape ¶
GlyphToShape returns a shape containing the paths for rune r as found in the font. The path is in font units. Use font.UnitsPerEm() to calculate scale factors.
func (*Shape) BoundingBox ¶
BoundingBox calculates a bounding box that the Shape is guaranteed to fit within.
func (*Shape) Contains ¶
Contains returns true if the points are contained within the shape, false otherwise.
func (*Shape) MarshalJSON ¶
MarshalJSON implements the encoding.json.Marshaler interface
func (*Shape) Mask ¶
Mask returns an Alpha image defined by the shape's bounds, containing the result of rendering the shape.
func (*Shape) PointInShape ¶
PointInShape returns true if the point is contained within any path within the shape.
func (*Shape) Process ¶
func (s *Shape) Process(proc ShapeProcessor) []*Shape
Process applies a shape processor to the shape and returns a collection of new shapes.
func (*Shape) ProcessPaths ¶
func (s *Shape) ProcessPaths(proc PathProcessor) *Shape
ProcessPaths applies a path processor to the shape and returns a new shape containing the processed paths.
func (*Shape) Transform ¶
Transform applies an affine transform to all the paths in the shape and returns a new shape.
func (*Shape) UnmarshalJSON ¶
UnmarshalJSON implements the encoding.json.Unmarshaler interface
type ShapeProcessor ¶
ShapeProcessor defines the interface required for function passed to the Process function in Shape.
type ShapesProc ¶
type ShapesProc struct { Comp *CompoundProc Shapes *PointsProc }
ShapesProc contains a slice of shapes, which will be placed sequentially along the path, starting at the beginning and spaced there after by the spacing value, and at the path end, if not closed. If any shape is nil, then it is skipped. The rotation flag indicates if the shapes should be rotated relative to the path's tangent at that point.
func NewShapesProc ¶
func NewShapesProc(shapes []*Shape, spacing float64, rot PointRot) *ShapesProc
NewShapesProc creates a new shapes path processor with the supplied shapes, spacing and rotation flag.
func (*ShapesProc) Process ¶
func (sp *ShapesProc) Process(p *Path) []*Path
Process implements the PathProcessor interface.
type SimpleStrokeProc ¶
type SimpleStrokeProc struct {
Width float64
}
SimpleStrokeProc implements the simplest stroke path processor - flatten everything to RenderFlatten tolerance and turn each flattened line step into a rectangle Width wide. No joins or caps, just lots of little rectangles.
func (*SimpleStrokeProc) Process ¶
func (cp *SimpleStrokeProc) Process(p *Path) []*Path
Process implements the PathProcessor interface.
type SimplifyProc ¶
type SimplifyProc struct{}
SimplifyProc is a wrpper around Path.Simplify().
func (*SimplifyProc) Process ¶
func (sp *SimplifyProc) Process(p *Path) []*Path
Process implements the PathProcessor interface.
type SnipProc ¶
type SnipProc struct { N int Pattern []float64 Flatten float64 State int // contains filtered or unexported fields }
SnipProc contains the snip pattern and offset. The snip pattern represents lengths of state0, state1, ... stateN-1, and is in the same coordinate system as the path. The offset provides the ability to start from anywhere in the pattern.
func NewSnipProc ¶
NewSnipProc creates a new snip path processor with the supplied pattern and offset. If the pattern is not N in length then it is replicated to create a mod N length pattern.
type SplitProc ¶
type SplitProc struct{}
SplitProc breaks up a path into a collection of paths, one for each step in the original path.
type SquareWaveProc ¶
type SquareWaveProc struct { HalfLambda float64 // Half wave length Scale float64 // Ratio of amplitude to lambda KeepZero bool // Keeps internal zero-point crossings if set Flip bool // Flips the wave phase by 180 (pi) if set }
SquareWaveProc applies a square wave along a path with a defined wave length and amplitude. The wave starts and ends on zero-crossing points and the last half wave is truncated to the path length remaining. The internal zero-crossing points can be optionally preserved.
func NewSquareWaveProc ¶
func NewSquareWaveProc(lambda, amplitude float64) *SquareWaveProc
NewSquareWaveProc creates a new SquareWaveProc with the supplied wave length and amplitude.
func (*SquareWaveProc) Process ¶
func (sp *SquareWaveProc) Process(p *Path) []*Path
Process implements the PathProcessor interface.
type StrokeProc ¶
type StrokeProc struct { RTraceProc *TraceProc LTraceProc *TraceProc PostTraceProc PathProcessor // (pt, r) []part PointFunc func([]float64, float64) [][][]float64 // (part1, pt, part2) []part CapStartFunc func([][]float64, []float64, [][]float64) [][][]float64 CapEndFunc func([][]float64, []float64, [][]float64) [][][]float64 }
StrokeProc defines the width, join and cap types of the stroke.
func NewStrokeProc ¶
func NewStrokeProc(w float64) *StrokeProc
NewStrokeProc creates a stroke path processor with width w, the bevel join and butt cap types.
func NewStrokeProcExt ¶
func NewStrokeProcExt(rw, lw float64, jf func([][]float64, []float64, [][]float64) [][][]float64, d float64, cf func([][]float64, []float64, [][]float64) [][][]float64) *StrokeProc
NewStrokeProcExt creates a stroke path processor where the widths are specified separately for each side of the stroke. This allows the stroke to be offset to the left or right of the path being processed.
func (*StrokeProc) Process ¶
func (sp *StrokeProc) Process(p *Path) []*Path
Process implements the PathProcessor interface and will return either one or two paths depending on whether the path is open or closed.
type TraceProc ¶
type TraceProc struct { Width float64 Flatten float64 JoinFunc func([][]float64, []float64, [][]float64) [][][]float64 }
TraceProc defines the width and join types of the trace. The gap between two adjacent steps must be greater than MinGap for the join function to be called.
func NewTraceProc ¶
NewTraceProc creates a trace path processor with width w, the bevel join and butt cap types.
func (*TraceProc) ProcessParts ¶
ProcessParts returns the processed path as a slice of parts, rather a path so other path processors don't have to round trip path -> parts -> path -> parts (e.g. StrokeProc).
type TransformProc ¶
type TransformProc struct {
Transform *Aff3
}
TransformProc is a wrapper around Path.Transform() and contains the Aff3 transform to be applied.
func (*TransformProc) Process ¶
func (tp *TransformProc) Process(p *Path) []*Path
Process implements the PathProcessor interface.
type TriangleWaveProc ¶
type TriangleWaveProc struct { HalfLambda float64 // Half wave length Scale float64 // Ratio of amplitude to lambda KeepZero bool // Keeps internal zero-point crossings if set Flip bool // Flips the wave phase by 180 (pi) if set }
TriangleWaveProc applies a triangle wave along a path with a defined wave length and amplitude. The wave starts and ends on zero-crossing points and the last half wave is truncated to the path length remaining. The internal zero-crossing points can be optionally preserved.
func NewTriangleWaveProc ¶
func NewTriangleWaveProc(lambda, amplitude float64) *TriangleWaveProc
NewTriangleWaveProc creates a new TriangleWaveProc with the supplied wave length and amplitutde.
func (*TriangleWaveProc) Process ¶
func (tp *TriangleWaveProc) Process(p *Path) []*Path
Process implements the PathProcessor interface.
Source Files ¶
- affine.go
- boxerproc.go
- capproc.go
- curveproc.go
- doc.go
- draw.go
- ellipsepaths.go
- fsnipproc.go
- glyph.go
- jitterproc.go
- joincap.go
- lineproc.go
- mpdproc.go
- path.go
- pathprocess.go
- paths.go
- pathsnipproc.go
- pen.go
- pointproc.go
- redgeproc.go
- render.go
- renderable.go
- roundedproc.go
- shape.go
- shapeproc.go
- shapeprocess.go
- simplestrokeproc.go
- snipproc.go
- sqrwproc.go
- strokeproc.go
- traceproc.go
- transform.go
- triwproc.go
Directories ¶
Path | Synopsis |
---|---|
Package color contains types and functions for color management.
|
Package color contains types and functions for color management. |
Package image contains functions that mostly operate on image.Gray.
|
Package image contains functions that mostly operate on image.Gray. |
texture
Package texture contains functions that populate images with texture.
|
Package texture contains functions that populate images with texture. |
Package util contains functions for linear and non-linear interpolations and their inversions.
|
Package util contains functions for linear and non-linear interpolations and their inversions. |