geom

package
v0.0.0-...-e433919 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2025 License: MIT Imports: 3 Imported by: 1

Documentation

Overview

Package geom provides utilities for manipulating rectangular geometry.

It is patterned heavily after image.Rectangle and image.Point, but vastly extends their capabilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ArrangeVerticalStack

func ArrangeVerticalStack[T Scalar](rects []Rect[T])

ArrangeVerticalStack arranges the subsequent rectangles of rects underneath the first vertically, expanding all for which it is necessary so that they are all the same width including the first.

func TileEvenHorizontally

func TileEvenHorizontally[T Scalar](tiles []Rect[T], r Rect[T])

TileEvenHorizontally arranges and resizes the elements of tiles so that the result are a series of rectangles that comprise an even, horizontal splitting of r. In other words,

tiles := make([]geom.Rect[float64], 3)
TileEvenHorizontally(tiles, r)

will produce

---------- | | | | ----------

func TileEvenVertically

func TileEvenVertically[T Scalar](tiles []Rect[T], r Rect[T])

TileEvenVertically arranges and resizes the elements of tiles so that the result are a series of rectangles that comprise an even, vertical splitting of r. In other words,

tiles := make([]geom.Rect[float64], 3)
TileEvenVertically(tiles, r)

will produce

----------
|        |
----------
|        |
----------
|        |
----------

func TileRightThenDown

func TileRightThenDown[T Scalar](tiles []Rect[T], r Rect[T])

TileRightThenDown arranges and resizes the elements of tiles in order to split r into a series of rectangles that recursively split each section halfway to the right and then downwards. In other words,

tiles := make([]geom.Rect[float64], 4)
TileRightThenDown(tiles, r)

will produce

------------
|    |     |
|    -------
|    |  |  |
------------

func TileRows

func TileRows[T Scalar](tiles []Rect[T], r Rect[T], cols int)

TileRows arranges and resizes the elements of tiles to produce a series of rows and columns the union of which reproduces r. The final row of the table is split evenly into at most cols columns. When that number is exceeded, a new row is added below it instead.

func TileTwoThirdsSidebar

func TileTwoThirdsSidebar[T Scalar](tiles []Rect[T], r Rect[T])

TileTwoThirdsSidebar arranges and resizes the elements of tiles so that the result are a series of rectangles where the first is two-thirds the width of r and the rest are arranged vertically in an even split in the remaining space.

func TiledEvenHorizontally

func TiledEvenHorizontally[T Scalar](numtiles int, r Rect[T]) iter.Seq[Rect[T]]

func TiledEvenVertically

func TiledEvenVertically[T Scalar](numtiles int, r Rect[T]) iter.Seq[Rect[T]]

TiledEvenVertically is the same as TileEvenVertically except that it yields the tiles from an iterator.

func TiledRightThenDown

func TiledRightThenDown[T Scalar](numtiles int, r Rect[T]) iter.Seq[Rect[T]]

TiledRightThenDown is the same as TileRightThenDown but yields the successive tiles from an interator instead of inserting them into a slice.

func TiledRows

func TiledRows[T Scalar](numtiles int, r Rect[T], cols int) iter.Seq[Rect[T]]

TiledRows is the same as TileRows except that it yields the tiles from an iterator.

func TiledTwoThirdsSidebar

func TiledTwoThirdsSidebar[T Scalar](numtiles int, r Rect[T]) iter.Seq[Rect[T]]

TiledTwoThirdsSidebar is the same as TileTwoThirdsSidebar except that it yields the successive rectangles from an iterator instead of inserting them into a slice.

func VerticalStack

func VerticalStack[T Scalar](first Rect[T]) iter.Seq[Rect[T]]

VerticalStack returns an iterator that yields the rectangle provided and then identical copies shifted downwards by its height repeatedly, thus producing an infinite vertical stack of rectangles below the first.

Types

type Edges

type Edges uint32

Edges is a bitmask representing zero or more edges of a rectangle.

const (
	EdgeNone Edges = 0
	EdgeTop  Edges = 1 << (iota - 1)
	EdgeBottom
	EdgeLeft
	EdgeRight
)

type Integer

type Integer interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

Integer is a constraint for any integer type.

type Point

type Point[T Scalar] struct {
	X, Y T
}

func FromImagePoint

func FromImagePoint(p image.Point) Point[int]

func Max

func Max[T Scalar](points ...Point[T]) Point[T]

func Min

func Min[T Scalar](points ...Point[T]) Point[T]

func Mod

func Mod[T Integer](p Point[T], r Rect[T]) Point[T]

func PConv

func PConv[Out Scalar, In Scalar](p Point[In]) Point[Out]

func Pt

func Pt[T Scalar](X, Y T) Point[T]

func (Point[T]) Add

func (p Point[T]) Add(q Point[T]) Point[T]

func (Point[T]) Div

func (p Point[T]) Div(k T) Point[T]

func (Point[T]) ImagePoint

func (p Point[T]) ImagePoint() image.Point

func (Point[T]) In

func (p Point[T]) In(r Rect[T]) bool

func (Point[T]) IsZero

func (p Point[T]) IsZero() bool

func (Point[T]) Mul

func (p Point[T]) Mul(k T) Point[T]

func (Point[T]) Sub

func (p Point[T]) Sub(q Point[T]) Point[T]

type Rect

type Rect[T Scalar] struct {
	Min, Max Point[T]
}

A Rect contains the points with Min.X <= X < Max.X, Min.Y <= Y < Max.Y. It is well-formed if Min.X <= Max.X and likewise for Y. Points are always well-formed. A rectangle's methods always return well-formed outputs for well-formed inputs.

func Align

func Align[T Scalar](outer, inner Rect[T], edges Edges) Rect[T]

Align shifts the specified edges of inner to align with the corresponding edges of outer, stretching the rectangle as necessary if opposite edges are specified.

func FromImageRect

func FromImageRect(r image.Rectangle) Rect[int]

func RConv

func RConv[Out Scalar, In Scalar](r Rect[In]) Rect[Out]

RConv converts a Rect[In] to a Rect[Out] with possible loss of precision.

func Rt

func Rt[T Scalar](x0, y0, x1, y1 T) Rect[T]

Rt is shorthand for Rect{Pt(x0, y0), Pt(x1, y1)}. The returned rectangle has minimum and maximum coordinates swapped if necessary so that it is well-formed.

func (Rect[T]) Add

func (r Rect[T]) Add(p Point[T]) Rect[T]

func (Rect[T]) Aspect

func (r Rect[T]) Aspect() float64

func (Rect[T]) Canon

func (r Rect[T]) Canon() Rect[T]

func (Rect[T]) Center

func (r Rect[T]) Center() Point[T]

Center returns the point at the middle of r.

func (Rect[T]) CenterAt

func (r Rect[T]) CenterAt(p Point[T]) Rect[T]

CenterAt returns a new rectangle with the same dimensions as r but with a center point at p.

func (Rect[T]) ClosestIn

func (r Rect[T]) ClosestIn(s Rect[T]) Rect[T]

ClosestIn returns r shifted to be inside of s at the closest possible point to its starting position. If r is already entirely inside of s, r is returned unchanged. If r can not fit entirely inside of s, the zero Rect is returned.

func (Rect[T]) Dx

func (r Rect[T]) Dx() T

func (Rect[T]) Dy

func (r Rect[T]) Dy() T

func (Rect[T]) Empty

func (r Rect[T]) Empty() bool

func (Rect[T]) Eq

func (r Rect[T]) Eq(s Rect[T]) bool

func (Rect[T]) FitTo

func (r Rect[T]) FitTo(size Point[T]) Rect[T]

func (Rect[T]) ImageRect

func (r Rect[T]) ImageRect() image.Rectangle

func (Rect[T]) In

func (r Rect[T]) In(s Rect[T]) bool

func (Rect[T]) Inset

func (r Rect[T]) Inset(n T) Rect[T]

func (Rect[T]) Inset2

func (r Rect[T]) Inset2(n Point[T]) Rect[T]

func (Rect[T]) Intersect

func (r Rect[T]) Intersect(s Rect[T]) Rect[T]

func (Rect[T]) IsZero

func (r Rect[T]) IsZero() bool

func (Rect[T]) Overlaps

func (r Rect[T]) Overlaps(s Rect[T]) bool

func (Rect[T]) Pad

func (r Rect[T]) Pad(top, bottom, left, right T) Rect[T]

func (Rect[T]) PropShift

func (r Rect[T]) PropShift(to, from Rect[T]) Rect[T]

func (Rect[T]) Resize

func (r Rect[T]) Resize(size Point[T]) Rect[T]

func (Rect[T]) Size

func (r Rect[T]) Size() Point[T]

func (Rect[T]) Sub

func (r Rect[T]) Sub(p Point[T]) Rect[T]

func (Rect[T]) Union

func (r Rect[T]) Union(s Rect[T]) Rect[T]

func (Rect[T]) WithAspect

func (r Rect[T]) WithAspect(aspect float64) Rect[T]

type Scalar

type Scalar interface {
	~float32 | ~float64 | Integer
}

Scalar is a constraint for the types that geom types and functions can handle.

Jump to

Keyboard shortcuts

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