Documentation ¶
Overview ¶
Package shape provides types to satisfy the Shape interface, which allows for containment and outline checks on two dimensional shapes.
Index ¶
Constants ¶
This section is empty.
Variables ¶
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 }) )
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 ¶
Types ¶
type Eq ¶
Eq represents a basic equation-- a mapping of x values to y values. Specifically, this equation is expected to be significant 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.
type In ¶
In functions return whether the given coordinate lies in a shape.
func AndIn ¶
AndIn will combine multiple In functions into one, where if any of the shapes are false the result is false.
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.
type Rect ¶
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 ¶
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.Point, 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