shape

package
v3.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package shape provides 2D shaping utilities.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Square will return true for any [x][y]
	Square = JustIn(func(x, y int, sizes ...int) bool {
		return true
	})

	// Rectangle will return true for any [x][y] in w, h
	Rectangle = JustIn(func(x, y int, sizes ...int) bool {
		w := sizes[0]
		h := sizes[0]
		if len(sizes) > 1 {
			h = sizes[1]
		}
		if x < w && y < h && x >= 0 && y >= 0 {
			return true
		}
		return false
	})
	// Diamond has a shape like the following:
	// . . t . .
	// . t t t .
	// t t t t t
	// . t t t .
	// . . t . .
	Diamond = JustIn(func(x, y int, sizes ...int) bool {
		radius := sizes[0] / 2
		return math.Abs(float64(x-radius))+math.Abs(float64(y-radius)) < float64(radius)
	})
	// Circle has a shape like the following:
	// . . . . . . .
	// . . t t t . .
	// . t t t t t .
	// . t t t t t .
	// . t t t t t .
	// . . t t t . .
	// . . . . . . .
	Circle = JustIn(func(x, y int, sizes ...int) bool {
		radius := sizes[0] / 2
		dx := math.Abs(float64(x - radius))
		dy := math.Abs(float64(y - radius))
		radiusf64 := float64(radius)
		if dx+dy <= radiusf64 {
			return true
		}
		return math.Pow(dx, 2)+math.Pow(dy, 2) < math.Pow(radiusf64, 2)
	})
	// Checkered has a shape like the following:
	// t . t . t .
	// . t . t . t
	// t . t . t .
	// . t . t . t
	// t . t . t .
	// . t . t . t
	Checkered = JustIn(func(x, y int, sizes ...int) bool {
		return (x+y)%2 == 0
	})
)
View Source
var (

	// Heart has an shape like the following:
	// . . t . t . .
	// . t t t t t .
	// t t t t t t t
	// t t t t t t t
	// . t t t t t .
	// . . t t t . .
	// . . . . . . .
	Heart = JustIn(OrIn(
		AndIn(
			XRange(0, 0.5), hf1.Below(), hf3.Above()),
		AndIn(
			XRange(0.5, 1), hf2.Below(), hf4.Above()),
	))
)

Functions

func Condense

func Condense(sh Shape, w, h int) []intgeom.Rect2

Condense finds a set of rectangles that covers the shape. Used to return a minimal set of rectangles in an appropriate time.

func GetBorderHoles

func GetBorderHoles(sh Shape, w, h int) [][]intgeom.Point2

GetBorderHoles finds sets of points which are not In this shape that are adjacent in addition to the space around the shape (ie points that border the shape)

func GetHoles

func GetHoles(sh Shape, w, h int) [][]intgeom.Point2

GetHoles finds sets of points which are not In this shape that are adjacent.

func ToOutline

func ToOutline(shape Shape) func(...int) ([]intgeom.Point2, error)

ToOutline returns the set of points along the input shape's outline, if one exists.

func ToOutline4

func ToOutline4(shape Shape) func(...int) ([]intgeom.Point2, error)

ToOutline4 returns the set of points along the input shape's outline, if one exists, but will move only up, left, right, or down to form this outline.

Types

type Bezier

type Bezier interface {
	Pos(progress float64) (x, y float64)
}

A Bezier has a function indicating how far along a curve something is given some float64 progress between 0 and 1. This allows points, lines, and limitlessly complex bezier curves to be represented under this interface.

Beziers should not break if given an input outside of 0 to 1, but the results shouldn't be relied upon.

func BezierCurve

func BezierCurve(coords ...float64) (Bezier, error)

BezierCurve will form a Bezier on the given coordinates, expected in (x,y) pairs. If the inputs have an odd length, an error noting so is returned, and the Bezier returned is nil.

type BezierNode

type BezierNode struct {
	Left, Right Bezier
}

A BezierNode ties together and find points between two other Beziers

func (BezierNode) Pos

func (bn BezierNode) Pos(progress float64) (x, y float64)

Pos returns the a point progress percent between this node's left and right progress percent points.

type BezierPoint

type BezierPoint floatgeom.Point2

A BezierPoint covers cases where only 1 point is supplied, and serve as roots.

func (BezierPoint) Pos

func (bp BezierPoint) Pos(float64) (x, y float64)

Pos returns this point.

func (BezierPoint) X

func (bp BezierPoint) X() float64

X returns bp's value on the X axis.

func (BezierPoint) Y

func (bp BezierPoint) Y() float64

Y returns bp's value on the Y axis.

type Eq

type Eq func(x float64) (y float64)

Eq represents a basic equation-- a mapping of x values to y values. This equation is expected to represent some part or all of a shape from -1 to 1. This range is chosen because it's often easier to write shape equations around the center of a graph.

func (Eq) Above

func (eq Eq) Above() In

Above returns an In which reports true for all x,y coordinates above the equation curve.

func (Eq) Below

func (eq Eq) Below() In

Below returns an In which reports true for all x,y coordinates below the equation curve.

type In

type In func(x, y int, sizes ...int) bool

In functions return whether the given coordinate lies in a shape.

func AndIn

func AndIn(is ...In) In

AndIn will combine multiple In functions into one, where if any of the shapes are false the result is false.

func NotIn

func NotIn(i In) In

NotIn returns the opposite of a given In function for any query.

func OrIn

func OrIn(is ...In) In

OrIn will combine multiple In functions into one, where if any of the shapes are true the result is true.

func XRange

func XRange(a, b float64) In

XRange is an example In utility which returns values within a given relative range (where 0 = 0 and 1 = size).

type JustIn

type JustIn In

A JustIn lets an In function serve as a shape by automatically wrapping it in assistant functions for other utilites.

func (JustIn) In

func (ji JustIn) In(x, y int, sizes ...int) bool

In acts as the underlying In function

func (JustIn) Outline

func (ji JustIn) Outline(sizes ...int) ([]intgeom.Point2, error)

Outline calls ToOutline on a JustIn

func (JustIn) Rect

func (ji JustIn) Rect(sizes ...int) [][]bool

Rect calls InToRect on a JustIn's In

type Points

type Points map[intgeom.Point2]struct{}

Points is a shape defined by a set of points. It ignores input width and height given to it as it only cares about its points.

func (Points) In

func (p Points) In(x, y int, sizes ...int) bool

In returns whether the input x and y are a point in the point map

func (Points) Outline

func (p Points) Outline(sizes ...int) ([]intgeom.Point2, error)

Outline returns the set of points along the point map's outline, if one exists

func (Points) Rect

func (p Points) Rect(sizes ...int) [][]bool

Rect returns a 2D slice of booleans representing the output of the In function in that rectangle

type Rect

type Rect func(sizes ...int) [][]bool

A Rect is a function that returns a 2d boolean array of booleans for a given size, where true represents that the bounded shape contains the point [x][y].

func InToRect

func InToRect(i In) Rect

InToRect converts an In function into a Rect function. Know that, if you are planning on looping over this only once, it's better to just use the In function. The use case for this is if the same size rect will be queried on some function multiple times, and just having the booleans to re-access is needed.

type Shape

type Shape interface {
	In(x, y int, sizes ...int) bool
	Outline(sizes ...int) ([]intgeom.Point2, error)
	Rect(sizes ...int) [][]bool
}

A Shape represents a rectangle of width/height size where for each x,y coordinate, either that value lies inside the shape or outside of the shape, represented by true or false. Shapes can be fuzzed along their border to create gradients of floats, and shapes can be queried to just produce a 2d boolean array of width/height size. Todo: consider if the number of coordinate arguments should be variadic, if width/height should not be combined and/or variadic, for additional dimension support

func NewPoints

func NewPoints(ps ...intgeom.Point2) Shape

NewPoints creates a Points shape from any number of intgeom Points

type StrictRect

type StrictRect [][]bool

A StrictRect is a shape that ignores input width and height given to it.

func NewStrictRect

func NewStrictRect(w, h int) StrictRect

NewStrictRect returns a StrictRect with the given strict dimensions, all values set to false.

func (StrictRect) In

func (sr StrictRect) In(x, y int, sizes ...int) bool

In returns whether the input x and y are within this StrictRect's shape. If the shape is undefined for the input values, it returns false.

func (StrictRect) Outline

func (sr StrictRect) Outline(sizes ...int) ([]intgeom.Point2, error)

Outline returns this StrictRect's outline, ignoring the input dimensions.

func (StrictRect) Rect

func (sr StrictRect) Rect(sizes ...int) [][]bool

Rect returns the StrictRect itself.

Jump to

Keyboard shortcuts

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