raster

package module
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2025 License: BSD-3-Clause Imports: 6 Imported by: 0

README

raster - a convenience package to render vector graphics as images

Overview

The raster package helps visualize vector graphics in the form of a PNG image. It can be used with the "golang.org/x/image/vector" package. Using primatives in that package, the raster package provides support for rendering solid lines and approximate circles. It also provides a convenience function to render the vector paths to an image. Documentation for the raster package should be available on go.dev.

The package provides a simple examples/trace.go program that uses the package to render something resembling a PCB copper trace.

$ go run examples/trace.go --dest image.png

creates the following image.png file:

trace rendered

The optional --fast argument pieces together the final image from smaller renderings. I've not done extensive comparisons, so the performance difference may not be significant. This method of rendering employs different approximations, so the renderings are not pixel for pixel the same.

trace rendered --fast

License info

The raster program is distributed with the same BSD 3-clause license as that used by golang itself.

Reporting bugs and feature requests

The package raster has been developed purely out of curiosity for and as part of a project to create simple PCBs using a Snapmaker 2 3D printer (with laser and CNC tool heads). If you find a bug or want to suggest a feature addition, please use the bug tracker.

Documentation

Overview

Package raster contains some convenience functions for creating "golang.org/x/image/vector" paths and rendering them into images.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DrawAt

func DrawAt(im draw.Image, r *vector.Rasterizer, x, y float64, col color.Color)

DrawAt places the r into an image aligning (x,y) of r with the (0,0) coordinate of the image.

func LineTo

func LineTo(r Scriber, capped bool, oX, oY, nX, nY, width float64)

LineTo renders a line segment from (oX,oY) to (nX,nY) with the specified perpendicular width. The capped value adds rounded end-caps to the line of radius half of the width (as approximated with Bezier curves).

func PointAt

func PointAt(r Scriber, x, y, width float64)

PointAt renders an approximate "circle" via 4 cubic Bezier curves describing the arc of the 4 quadrants.

func SquareAt

func SquareAt(r Scriber, x, y, width float64)

SquareAt renders a width by width square centered at (x,y).

Types

type Entry added in v0.6.1

type Entry struct {
	Closed                 bool
	Path                   []Segment
	MinX, MaxX, MinY, MaxY float64
}

Entry captures the signature of a series of segments.

type Operator added in v0.6.1

type Operator int

func (Operator) String added in v0.6.1

func (op Operator) String() string

type Rasterizer added in v0.6.0

type Rasterizer struct {
	R       *vector.Rasterizer
	Entries []Entry
}

Rasterizer is a wrapper for the golang.org/x/image/vector.Rasterizer type which maps float64 arguments to float32 bit calls.

func NewRasterizer added in v0.6.0

func NewRasterizer(w, h int) *Rasterizer

NewRasterizer allocates a new rasterizer with a fixed size.

func (*Rasterizer) ClosePath added in v0.6.0

func (r *Rasterizer) ClosePath()

ClosePath forms a loop back line from the pen to the start of the path.

func (*Rasterizer) CubeTo added in v0.6.0

func (r *Rasterizer) CubeTo(a, b, c, d, e, f float64)

CubeTo constructs a cubic Bezier curve using the supplied parameters, from the pen location to point (e,f), which becomes the updated pen location.

func (*Rasterizer) LineTo added in v0.6.0

func (r *Rasterizer) LineTo(x, y float64)

LineTo constructs a straight line from the pen to the target (x,y) coordinate, and updates the pen to this location.

func (*Rasterizer) MoveTo added in v0.6.0

func (r *Rasterizer) MoveTo(x, y float64)

MoveTo sets the rasterizer pen to the coordinate (x,y).

func (*Rasterizer) Render added in v0.6.1

func (r *Rasterizer) Render(im draw.Image, x, y float64, col color.Color)

Render places the entries of r into the im at (x,y) offset.

func (*Rasterizer) Reset added in v0.6.0

func (r *Rasterizer) Reset(w, h int)

Reset resets the memory of the rasterizer and sets the size of its clipping rectangle.

type Scriber added in v0.6.0

type Scriber interface {
	MoveTo(x, y float64)
	LineTo(x, y float64)
	CubeTo(a, b, c, d, e, f float64)
	ClosePath()
}

Scriber is the interface for drawing vector graphics.

type Segment added in v0.6.1

type Segment struct {
	Op   Operator
	Args []float64
}

Segment is a single line.

Directories

Path Synopsis
Program trace creates a trace-like rendering of something you might see on a PCB.
Program trace creates a trace-like rendering of something you might see on a PCB.

Jump to

Keyboard shortcuts

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