Skip to content

gravitton/geometry

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

80 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Geometry

Latest Stable Version Build Status Coverage Status Go Report Card Go Dev Reference Software License

Generic immutable 2D geometry library for game development.

Uses a top-left origin with +Y down. This only affects directional getters (Top, Bottom, Up, Down).

Installation

go get github.com/gravitton/geometry

Usage

import geom "github.com/gravitton/geometry"

p1 := geom.Pt(1, 2)
p2 := geom.Pt(3, 4)

v := p2.Subtract(p1) // Vector
if v.Equal(geom.Vec(2, 2)) { ... }

r := geom.Rect(p1, geom.Sz(10, 5))
if r.Contains(p2) { ... }

hex := geom.Hexagon(p1, geom.SzU(20), geom.FlatTop)
for _, vertex := range hex.Vertices() { ... }

Matrix transforms:

m := geom.IdentityMatrix().Rotate(math.Pi / 4).Scale(2, 2)
p := geom.Pt(1.0, 0.0).Transform(m)

Type aliases for common numeric types (ints, floats):

import (
    "github.com/gravitton/geometry/types/floats"
    "github.com/gravitton/geometry/types/ints"
)

type Grid struct {
    Size     ints.Size
    CellSize floats.Size
}

API

All types are generic over the Number constraint:

type Number interface {
    ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~float32 | ~float64
}

Full documentation is available at pkg.go.dev/github.com/gravitton/geometry.

Types

Type Constructor Description
Point[T] Pt(x, y) 2D position
Vector[T] Vec(x, y) 2D displacement
Size[T] Sz(w, h), SzU(n) Width and height
Rectangle[T] Rect(center, size), RectFromMin, RectFromMinMax, RectFromSize Axis-aligned rectangle (center + size)
Circle[T] Circ(center, r) Circle (center + radius)
Line[T] Ln(start, end) Line segment
Polygon[T] Pol(vertices) Arbitrary polygon
RegularPolygon[T] RegPol(center, size, n, angle), Triangle, Square, Hexagon Regular polygon
Padding[T] Pad(t,r,b,l), PadU(n), PadXY(tb, lr) Top/Right/Bottom/Left padding
Matrix Mat(a,b,c,d,e,f), IdentityMatrix(), TranslationMatrix, RotationMatrix, ScaleMatrix 2D affine matrix (float64)

Conventions

All methods return new values — no mutation.

Every type exposes .Int(), .Float(), and implements String(), Equal(), IsZero().

Types with spatial extent also implement Bounds() Rectangle[T].

Collision

CollisionRectangles[T](a, b Rectangle[T]) bool
CollisionCircles[T](a, b Circle[T]) bool
CollisionRectangleCircle[T](r Rectangle[T], c Circle[T]) bool

Image interop

PointFromImage[T](p image.Point) Point[T]
SizeFromImage[T](r image.Rectangle) Size[T]
RectFromImage[T](r image.Rectangle) Rectangle[T]

(Point[T]).Point() image.Point
(Rectangle[T]).Rectangle() image.Rectangle

Math utilities

Lerp[T](a, b T, t float64) T
Clamp[T](v, min, max T) T
Equal[T](a, b T) bool          // within Delta (1e-6)
EqualDelta[T](a, b T, d float64) bool
Midpoint[T](a, b T) T
Abs[T](a T) T
Multiply[T](a T, factor float64) T
Divide[T](a T, factor float64) T
ToRadians(deg float64) float64
ToDegrees(rad float64) float64

Credits

License

The MIT License (MIT). Please see License File for more information.