polygon

package module
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: May 27, 2024 License: BSD-3-Clause Imports: 5 Imported by: 0

README

polygon - a package to manipulate non self-intersecting polygons

Overview

This package provides routines for 2D polygons. That is non self-intersecting multi-edged shapes with more than 2 vertex points.

API

The API provided by this package is avalble using go doc zappem.net/pub/math/polygon. It can also be browsed on the go.dev website: package zappem.net/pub/math/polygon.

Example

See the https://github.com/tinkerator/polygons example.

License info

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

Reporting bugs

Use the github polygon bug tracker.

Documentation

Overview

Package polygon provides functions for manipulation of polygon structures. A polygon is an N straight sided, not self-intersecting, shape, where N is greater than 2.

The conventions for this package are x increases to the right, and y increases up the page (reverse of typical image formats). This convention gives meaning to clockwise and counter-clockwise.

Index

Constants

This section is empty.

Variables

View Source
var Zeroish = 1e-6

Zeroish is defined to merge points and avoid rounding error problems. The number is chosen to connect anything closer than 0.001 (which is a convenience default for values representing millimeters).

Functions

func MatchPoint

func MatchPoint(a Point, b ...Point) bool

MatchPoint recognizes when a is close enough to any of the points b...

func MinMax

func MinMax(a, b float64) (float64, float64)

Sort two numbers to be in ascending order.

Types

type Point

type Point struct {
	X, Y float64
}

Point holds a 2d coordinate value. X increases to the right. Y increases up the page. These are the conventions of mathematical graph paper and not those of typical image formats.

func BB

func BB(a, b Point) (ll, tr Point)

BB determines the bounding box LL and TR corner points.

func (Point) Dot added in v0.6.7

func (a Point) Dot(b Point) float64

Dot computes the dot product of two vectors.

type Shape

type Shape struct {
	// MinX etc represent the bounding box for a polygon.
	MinX, MinY, MaxX, MaxY float64
	// Hole indicates the polygon points are ordered (clockwise)
	// to represent a hole instead of an additive shape.
	Hole bool
	// Consecutive points on the perimeter of the polygon. There
	// is an implicit edge joining the last point to the first
	// point.
	PS []Point
}

Shape holds the points in a polygon and some convenience fields, such as the properties of its bounding box and whether the perimeter is clockwise (by convention a Hole) or counterclockwise (by convention a shape).

func (*Shape) BB added in v0.7.1

func (s *Shape) BB() (ll, tr Point)

Return the bounding box lower left and top right corner points for the shape.

type Shapes

type Shapes struct {
	P []*Shape
}

Shapes holds a set of polygon shapes each of arrays of (x,y) points.

func (*Shapes) Append

func (p *Shapes) Append(pts ...Point) (*Shapes, error)

Append appends a polygon shape constructed from a series of consecutive points. If p is nil, it is allocated. The return value is the appended collection of shapes.

func (*Shapes) BB added in v0.7.1

func (s *Shapes) BB() (ll, tr Point)

Return the bounding box lower left and top right corner points for the shapes.

func (*Shapes) Builder

func (p *Shapes) Builder(pts ...Point) *Shapes

Builder turns a set of points into a polygon shape and appends it to the provided value, p. If p is nil it is allocated. If the operation cannot be performed, the function panics. If you require more error control, call p.Append() instead.

func (*Shapes) Duplicate

func (p *Shapes) Duplicate() *Shapes

Duplicate makes an independent copy of a set of polygon shapes.

func (*Shapes) Inflate added in v0.7.1

func (s *Shapes) Inflate(n int, d float64) error

Inflate inflates an indexed shape by distance, d. Holes are deflated by this amount. If we inflate a circle by d, its diameter will increase by that much.

func (*Shapes) Invert

func (p *Shapes) Invert(i int) error

Invert reverses the clockwise <-> counter-clockwise orientation of the shape without changing its starting point. The conventions for the package are shapes are counter-clockwise and holes are clockwise, so the .Hole value for the shape is inverted.

func (*Shapes) Union

func (p *Shapes) Union()

Union tries to combine all of the shape outlines into union outlines.

Jump to

Keyboard shortcuts

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