vector

package
v0.23.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2024 License: BSD-3-Clause Imports: 4 Imported by: 87

Documentation

Overview

Package vector provides a rasterizer for 2-D vector graphics.

Example (Draw)
package main

import (
	"image"
	"image/draw"
	"os"

	"golang.org/x/image/vector"
)

func main() {
	const (
		width  = 30
		height = 20
	)

	// Define a closed shape with three edges: two linear and one quadratic.
	// One of its vertices is at the top-left corner of the (1, 2) pixel, which
	// is also the bottom-right corner of the (0, 1) pixel.
	//
	// Co-ordinates can be floating point numbers, not just integers. They can
	// also be outside the vector.Rasterizer's dimensions. The shapes will be
	// clipped during rasterization.
	r := vector.NewRasterizer(width, height)
	r.DrawOp = draw.Src
	r.MoveTo(1, 2)
	r.LineTo(20, 2)
	r.QuadTo(40.5, 15, 10, 20)
	r.ClosePath()

	// Finish the rasterization: the conversion from vector graphics (shapes)
	// to raster graphics (pixels). Co-ordinates are now integers.
	dst := image.NewAlpha(image.Rect(0, 0, width, height))
	r.Draw(dst, dst.Bounds(), image.Opaque, image.Point{})

	// Visualize the pixels.
	const asciiArt = ".++8"
	buf := make([]byte, 0, height*(width+1))
	for y := 0; y < height; y++ {
		for x := 0; x < width; x++ {
			a := dst.AlphaAt(x, y).A
			buf = append(buf, asciiArt[a>>6])
		}
		buf = append(buf, '\n')
	}
	os.Stdout.Write(buf)

}
Output:

..............................
..............................
.8888888888888888888+.........
.+88888888888888888888+.......
..888888888888888888888+......
..+888888888888888888888+.....
...8888888888888888888888+....
...+8888888888888888888888+...
....88888888888888888888888+..
....+88888888888888888888888..
.....88888888888888888888888..
.....+8888888888888888888888..
......8888888888888888888888..
......+88888888888888888888+..
.......8888888888888888888+...
.......+88888888888888888.....
........888888888888888+......
........+88888888888+.........
.........8888888++............
.........+8+++................

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Rasterizer

type Rasterizer struct {

	// DrawOp is the operator used for the Draw method.
	//
	// The zero value is draw.Over.
	DrawOp draw.Op
	// contains filtered or unexported fields
}

Raster is a 2-D vector graphics rasterizer.

The zero value is usable, in that it is a Rasterizer whose rendered mask image has zero width and zero height. Call Reset to change its bounds.

func NewRasterizer

func NewRasterizer(w, h int) *Rasterizer

NewRasterizer returns a new Rasterizer whose rendered mask image is bounded by the given width and height.

func (*Rasterizer) Bounds

func (z *Rasterizer) Bounds() image.Rectangle

Bounds returns the rectangle from (0, 0) to the width and height passed to NewRasterizer or Reset.

func (*Rasterizer) ClosePath

func (z *Rasterizer) ClosePath()

ClosePath closes the current path.

func (*Rasterizer) CubeTo

func (z *Rasterizer) CubeTo(bx, by, cx, cy, dx, dy float32)

CubeTo adds a cubic Bézier segment, from the pen via (bx, by) and (cx, cy) to (dx, dy), and moves the pen to (dx, dy).

The coordinates are allowed to be out of the Rasterizer's bounds.

func (*Rasterizer) Draw

func (z *Rasterizer) Draw(dst draw.Image, r image.Rectangle, src image.Image, sp image.Point)

Draw implements the Drawer interface from the standard library's image/draw package.

The vector paths previously added via the XxxTo calls become the mask for drawing src onto dst.

func (*Rasterizer) LineTo

func (z *Rasterizer) LineTo(bx, by float32)

LineTo adds a line segment, from the pen to (bx, by), and moves the pen to (bx, by).

The coordinates are allowed to be out of the Rasterizer's bounds.

func (*Rasterizer) MoveTo

func (z *Rasterizer) MoveTo(ax, ay float32)

MoveTo starts a new path and moves the pen to (ax, ay).

The coordinates are allowed to be out of the Rasterizer's bounds.

func (*Rasterizer) Pen

func (z *Rasterizer) Pen() (x, y float32)

Pen returns the location of the path-drawing pen: the last argument to the most recent XxxTo call.

func (*Rasterizer) QuadTo

func (z *Rasterizer) QuadTo(bx, by, cx, cy float32)

QuadTo adds a quadratic Bézier segment, from the pen via (bx, by) to (cx, cy), and moves the pen to (cx, cy).

The coordinates are allowed to be out of the Rasterizer's bounds.

func (*Rasterizer) Reset

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

Reset resets a Rasterizer as if it was just returned by NewRasterizer.

This includes setting z.DrawOp to draw.Over.

func (*Rasterizer) Size

func (z *Rasterizer) Size() image.Point

Size returns the width and height passed to NewRasterizer or Reset.

Jump to

Keyboard shortcuts

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