gopoints

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2021 License: MPL-2.0 Imports: 3 Imported by: 0

README

GoPoints

A very simple Go library for creating shapes on a 2D plane out of points, using only the Go stdlib. Supports some basic functions like:

  • storing points in a Plane structure
  • joining arbitrary points with lines (including diagonal)
  • filling shapes with points
  • flipping the whole plane or some points along the X or Y axis
  • fetching all points from a plane
  • fetching points from a plane based on the return value (boolean) of a given function (like a mathematical equation)
  • TODO: Applying an arbitrary transformation based on a function to the points on the plane

Use cases

Can be used for any number of things, but my primary use-case is generating images made of 2D shapes, such as the example image generated with examples/CreatePngImage.go:

example image

Usage examples

Some example code can be found in the examples directory in the root of this repository

Documentation

Index

Constants

View Source
const VERSION string = "v1.0.4"

Current gopoints version

Variables

This section is empty.

Functions

This section is empty.

Types

type Plane

type Plane struct {
	// contains filtered or unexported fields
}

Plane is essentially a wrapper around PointSet, but with a specific size for dimension X and Y (although these are not strictly enforced by any of the methods) and some methods to interact with points both on the plane and passed as an argument.

func (*Plane) ErasePoints

func (pln *Plane) ErasePoints(points []Point, strict bool) error

ErasePoints removes points passed to it in the first argument from the plane's PointSet. It can optionally reject points which are not already on the plane if the second argument is `true`.

func (*Plane) Flip

func (pln *Plane) Flip(dimension int)

Flip flips the plane along the X or the Y axis. Sometimes you may need to flip the points on the plane, for example when converting to an image where the Y (1) axis it flipped. The axis of the flip is given as an integer - 0 for X and 1 for Y.

func (*Plane) FlipPoints

func (pln *Plane) FlipPoints(points []Point, dimension int) []Point

FlipPoints is much the same as Flip, but acts on a given set of points as opposed to the whole plane. It also returns the flipped points as a slice.

func (*Plane) GetDimensions

func (pln *Plane) GetDimensions() [2]int

GetDimensions returns the size of the plane as an array of two integers

func (*Plane) Init

func (pln *Plane) Init(dimensions [2]int)

Init initialises the plane with dimensions passed as an array of 2 integers, and creates a PointSet to store the points on the plane.

func (*Plane) JoinAndFillPoints

func (pln *Plane) JoinAndFillPoints(points []Point) []Point

JoinAndFillPoints works much like JoinPoints except it also attempts to work out the "inside" and "outside" of the shape being created, and then fill it with points. This is a one-size-fits-all implementation works reasonably well for most simple shapes, but it's probably always better to make your own.

func (*Plane) JoinPoints

func (pln *Plane) JoinPoints(points []Point) []Point

JoinPoints accepts a slice of points and joins each consecutive point using a line of points. It then returns all the points together as a slice.

func (*Plane) ReadPoints

func (pln *Plane) ReadPoints() []Point

ReadPoints reads all points from the plane's PointSet and returns them as an array (technically a slice).

func (*Plane) ReadPointsByFilter

func (pln *Plane) ReadPointsByFilter(f filterFunction) []Point

ReadPointsByFilter accepts a function as an argument, and runs the function with the X and Y coordinates of each point on the plane as argument 1 and 2 respectively. For each point that the function returns `true` for, that point is returned as part of a slice of points.

func (*Plane) WritePoints

func (pln *Plane) WritePoints(points []Point, strict bool) error

WritePoints writes points passed as the first argument to the plane's PointSet. It can optionally reject points which are outside of the plane's dimensions if the second argument is `true`.

type Point

type Point struct {
	X, Y int
}

A simple 2D point struct holding an X and a Y coordinate, with no methods

type PointSet

type PointSet struct {
	// contains filtered or unexported fields
}

A simple set implementation for 2D points as Go does not provide one. Uses map keys as the storage as they do not repeat and are unordered. The value used is a blank struct as it causes little to no overead due to its 0-byte size.

func (*PointSet) Add

func (set *PointSet) Add(point Point)

Add adds a single point into the set (unless it already exists of course - it's a set after all)

func (*PointSet) AddArray

func (set *PointSet) AddArray(pointArray []Point)

AddArray works much like Add but accepts a slice of points (poor naming) and adds each element of it to the set

func (*PointSet) AsArray

func (set *PointSet) AsArray() []Point

AsArray returns the contents of the set as a slice

func (*PointSet) CheckFor

func (set *PointSet) CheckFor(point Point) bool

CheckFor checks if a given point is in the set

func (*PointSet) CheckForAll

func (set *PointSet) CheckForAll(points []Point) bool

CheckForAll checks if all points in the given slice are present in the set and returns a single boolean to indicate the result

func (*PointSet) Init

func (set *PointSet) Init()

Init initialises the set by creating the map which stores the data. This is called implicitly by all other methods of the set so does not need to be called explicitly. It does not overwrite the set if called multiple times.

func (*PointSet) Remove

func (set *PointSet) Remove(point Point)

Remove removes a single point from the set

func (*PointSet) RemoveArray

func (set *PointSet) RemoveArray(pointArray []Point)

RemoveArray works much like Remove but accepts a slice of points (poor naming) and removes each element of it from the set

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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