geom

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package geom provides the small geometric vocabulary shared by the Ebitengine tool family: float vectors for simulation space, integer coordinates and rectangles for tile grids, and clamping helpers.

Vec2 is the continuous-space vector, with the usual arithmetic plus the toroidal-world helpers (Vec2.WrapTo, Vec2.ShortestDelta, Vec2.ToroidalDist) the ecosystem sims rely on. Coord addresses a tile grid cell and Rect an axis-aligned span of cells; Coord.Vec2 bridges the two spaces at cell centres. Clamp and Clamp01 are the range limiters repeated across every settings screen and camera in the family.

Example

Example steers an agent one normalized step toward a target, the way the family's sims move things around.

package main

import (
	"fmt"

	"github.com/danielriddell21/crucible/geom"
)

func main() {
	pos := geom.Vec2{X: 1, Y: 1}
	target := geom.Vec2{X: 4, Y: 5}
	step := target.Sub(pos).Normalize()
	pos = pos.Add(step)
	fmt.Printf("moved to (%.1f, %.1f), %.0f left\n", pos.X, pos.Y, pos.Dist(target))
}
Output:
moved to (1.6, 1.8), 4 left

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clamp

func Clamp[T cmp.Ordered](v, lo, hi T) T

Clamp returns v limited to the range [lo, hi].

Example
package main

import (
	"fmt"

	"github.com/danielriddell21/crucible/geom"
)

func main() {
	fmt.Println(geom.Clamp(15, 0, 10), geom.Clamp(-0.5, 0.0, 1.0), geom.Clamp("m", "a", "f"))
}
Output:
10 0 f

func Clamp01

func Clamp01(v float64) float64

Clamp01 returns v limited to the range [0, 1].

Types

type Coord

type Coord struct {
	X, Y int
}

Coord is an integer cell position on a tile grid.

func (Coord) Add

func (c Coord) Add(o Coord) Coord

Add returns c translated by o.

func (Coord) Vec2

func (c Coord) Vec2() Vec2

Vec2 returns the centre of the cell in continuous space.

type Rect

type Rect struct {
	X, Y, W, H int
}

Rect is an axis-aligned integer rectangle on a tile grid, described by its top-left corner and size.

func (Rect) Center

func (r Rect) Center() Coord

Center returns the cell at the centre of the rectangle.

Example
package main

import (
	"fmt"

	"github.com/danielriddell21/crucible/geom"
)

func main() {
	room := geom.Rect{X: 2, Y: 4, W: 6, H: 8}
	c := room.Center()
	fmt.Println(c, room.Contains(c))
}
Output:
{5 8} true

func (Rect) Contains

func (r Rect) Contains(c Coord) bool

Contains reports whether the cell c lies inside the rectangle.

type Vec2

type Vec2 struct {
	X, Y float64
}

Vec2 is a two-dimensional vector in continuous (simulation or screen) space.

func FromAngle

func FromAngle(a float64) Vec2

FromAngle returns the unit vector pointing at angle a radians.

func (Vec2) Add

func (v Vec2) Add(o Vec2) Vec2

Add returns v + o.

func (Vec2) Angle

func (v Vec2) Angle() float64

Angle returns the angle of v in radians, in (-π, π].

func (Vec2) Dist

func (v Vec2) Dist(o Vec2) float64

Dist returns the Euclidean distance between v and o.

func (Vec2) DistSq

func (v Vec2) DistSq(o Vec2) float64

DistSq returns the squared distance between v and o. It avoids the square root, so prefer it for comparisons.

func (Vec2) Dot

func (v Vec2) Dot(o Vec2) float64

Dot returns the dot product of v and o.

func (Vec2) Len

func (v Vec2) Len() float64

Len returns the Euclidean length of v.

func (Vec2) Normalize

func (v Vec2) Normalize() Vec2

Normalize returns v scaled to unit length. The zero vector is returned unchanged.

func (Vec2) Scale

func (v Vec2) Scale(s float64) Vec2

Scale returns v with both components multiplied by s.

func (Vec2) ShortestDelta

func (v Vec2) ShortestDelta(o Vec2, w, h float64) Vec2

ShortestDelta returns the shortest vector from v to o on a torus of the given size.

func (Vec2) Sub

func (v Vec2) Sub(o Vec2) Vec2

Sub returns v - o.

func (Vec2) ToroidalDist

func (v Vec2) ToroidalDist(o Vec2, w, h float64) float64

ToroidalDist returns the shortest distance between v and o on a torus of the given size.

Example

ExampleVec2_ToroidalDist shows distance on a wrapping world: crossing the seam is shorter than walking across the map.

package main

import (
	"fmt"

	"github.com/danielriddell21/crucible/geom"
)

func main() {
	a := geom.Vec2{X: 1, Y: 5}
	b := geom.Vec2{X: 9, Y: 5}
	fmt.Println(a.Dist(b), a.ToroidalDist(b, 10, 10))
}
Output:
8 2

func (Vec2) WrapTo

func (v Vec2) WrapTo(w, h float64) Vec2

WrapTo wraps v into the rectangle [0, w) × [0, h), for toroidal worlds.

Jump to

Keyboard shortcuts

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