geometry

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2024 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LeftToRight HorizontalDirection = iota
	RightToLeft

	TopToBottom VerticalDirection = iota
	BottomToTop
)
View Source
const (
	Pt Length = 1

	Inch = 72 * Pt
	In   = Inch

	Centimeter = Inch / 2.54
	Cm         = Centimeter

	Millimeter = Centimeter / 10
	Mm         = Millimeter
)

Variables

View Source
var DefaultLengthUnit = Cm
View Source
var ErrRectInvalid = errors.New("rectangle is invalid")

Functions

func EquateLength

func EquateLength() cmp.Option

EquateLength returns a cmp.Comparer option checking whether two distances have a margin of less than one millimeter.

func EquateLengthApprox

func EquateLengthApprox(margin Length) cmp.Option

EquateLengthApprox returns a cmp.Comparer option checking whether two distances are differ less than the specified margin.

func VisitLengthUnits

func VisitLengthUnits(visitor func(LengthUnit))

Types

type HorizontalDirection

type HorizontalDirection int

type Length

type Length float64

Length is a measure of distance in points (1/72th of an inch).

func LengthFromProto

func LengthFromProto(l *geometrypb.Length) (Length, error)

LengthFromProto constructs a Length from a protobuf message. Nil messages are treated as lengths of zero.

func (Length) Abs

func (l Length) Abs() Length

Abs returns the absolute value.

func (Length) AsProto

func (l Length) AsProto(unit LengthUnit) *geometrypb.Length

func (Length) Cm

func (l Length) Cm() float64

Mm returns the distance in centimeters.

func (Length) Inch

func (l Length) Inch() float64

Mm returns the distance in inch (2.54 cm).

func (Length) Max

func (l Length) Max(other Length) Length

Max returns the larger of l and other.

func (Length) Min

func (l Length) Min(other Length) Length

Min returns the smaller of l and other.

func (Length) Mm

func (l Length) Mm() float64

Mm returns the distance in millimeters.

func (Length) Mul

func (l Length) Mul(factor float64) Length

Mul returns the scalar product of l and factor.

func (Length) Name

func (l Length) Name() string

Name returns the name of the length unit. Panics if the length is not a base value.

func (Length) Pt

func (l Length) Pt() float64

Mm returns the distance in points (1/72 inch).

func (Length) Round

func (l Length) Round(nearest Length) Length

Round returns the closest multiple of nearest.

func (Length) String

func (l Length) String() string

String returns a string representation of the length using the default unit set in DefaultLengthUnit.

func (Length) UnitString

func (l Length) UnitString(unit LengthUnit) string

UnitString returns a string representation of the length using the given unit.

type LengthUnit

type LengthUnit interface {
	Name() string
	// contains filtered or unexported methods
}

type Point

type Point struct {
	// Coordinates
	Left, Top Length
}

func PointFromProto

func PointFromProto(pb *geometrypb.Point) (Point, error)

func (Point) AsProto

func (p Point) AsProto(unit LengthUnit) *geometrypb.Point

func (Point) Shift

func (p Point) Shift(s Size) Point

func (Point) String

func (p Point) String() string

type Rect

type Rect struct {
	// X-coordinate of the upper-left corner.
	Left Length

	// Y-coordinate of the upper-left corner.
	Top Length

	// X-coordinate of the lower-right corner.
	Right Length

	// Y-coordinate of the lower-right corner.
	Bottom Length
}

func RectFromCentimeters

func RectFromCentimeters(left, top, right, bottom float64) Rect

RectFromCentimeters constructs a rectangle from coordinates in centimeters.

func RectFromPoints

func RectFromPoints(left, top, right, bottom float64) Rect

RectFromPoints contructs a rectangle from coordinates in points.

func RectFromProto

func RectFromProto(pb *geometrypb.Rect) (Rect, error)

func RectFromXYWH

func RectFromXYWH(left, top, width, height Length) Rect

func (Rect) AsProto

func (r Rect) AsProto(unit LengthUnit) *geometrypb.Rect

func (Rect) BottomLeft

func (r Rect) BottomLeft() Point

func (Rect) BottomRight

func (r Rect) BottomRight() Point

func (Rect) Center

func (r Rect) Center() Point

func (Rect) Contains

func (r Rect) Contains(other Rect) bool

Test whether the rectangle fully contains another.

func (Rect) Height

func (r Rect) Height() Length

func (Rect) Inside

func (r Rect) Inside(other Rect) bool

Test whether the rectangle is fully contained within another.

func (Rect) IsEmpty

func (r Rect) IsEmpty() bool

func (Rect) Normalize

func (r Rect) Normalize() Rect

Normalizes the rectangle so both the width and height are increasing.

func (Rect) String

func (r Rect) String() string

func (Rect) TopLeft

func (r Rect) TopLeft() Point

func (Rect) TopRight

func (r Rect) TopRight() Point

func (Rect) Union

func (r Rect) Union(other Rect) Rect

Union merges two rectangles. The union is the smallest rectangle which contains both r and other.

func (Rect) Validate

func (r Rect) Validate() error

func (Rect) Width

func (r Rect) Width() Length

type RectCompareFunc

type RectCompareFunc func(a, b Rect) int

func MakeRectRowColumnCompare

func MakeRectRowColumnCompare(v VerticalDirection, h HorizontalDirection) RectCompareFunc

MakeRectRowColumnCompare returns a function comparing the positions of two rectangles. Rectangles are organized into lines and columns with the orientations controlled by v and h. The comparer's return value is zero if the rectangles overlap at least 50% on one of the axes, -1 if a is to the left or above b, +1 for the opposite.

type RoundedLength

type RoundedLength struct {
	// Unit for formatting values. Defaults to [Nearest] if unset.
	Unit LengthUnit

	// All values are rounded to the nearest multiple of this length.
	Nearest Length
}

Unit rounding all length values for a friendlier presentation.

func (RoundedLength) Name

func (r RoundedLength) Name() string

type Size

type Size struct {
	Width, Height Length
}

func SizeFromProto

func SizeFromProto(pb *geometrypb.Size) (Size, error)

func (Size) AsProto

func (s Size) AsProto(unit LengthUnit) *geometrypb.Size

func (Size) String

func (s Size) String() string

type VerticalDirection

type VerticalDirection int

Jump to

Keyboard shortcuts

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