geom

package module
v0.0.0-...-77f7b3a Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2026 License: LGPL-3.0 Imports: 3 Imported by: 0

README

geom

Go Reference CI

Small 2D geometry helpers for vectors, rectangles, and angles.

Install

go get github.com/elemir/geom

Quick start

package main

import (
	"fmt"

	"github.com/elemir/geom"
)

func main() {
	v := geom.Vec2{3, 4}
	u := geom.Vec2{1, 2}

	fmt.Println(v.Add(u))
	fmt.Println(v.Length())
}

Documentation

Overview

Package geom provides small 2D geometry helpers for vectors, rectangles, and angles.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clamp

func Clamp(x, low, high float64) float64

Clamp limits x to the inclusive range [low, high].

func IsInsidePoly

func IsInsidePoly(v Vec2, poly []Vec2) bool

IsInsidePoly reports whether v lies inside polygon p using the ray casting method. Points on the polygon boundary are treated as inside.

func SmoothStep

func SmoothStep(edge0, edge1, x float64) float64

SmoothStep returns a smoothly interpolated value between 0 and 1 across [edge0, edge1].

Types

type Angle

type Angle float64

Angle represents an angle in radians. Angles are typically in the range [-π, π] after normalization.

func RandAngle

func RandAngle() Angle

RandAngle creates a random normalized angle.

func (Angle) Abs

func (a Angle) Abs() Angle

Abs returns the absolute value of the angle. This is useful when the magnitude of the angle is needed without regard to direction.

func (Angle) Normalize

func (a Angle) Normalize() Angle

Normalize returns an equivalent angle in the range [-π, π]. This ensures consistent representation of angles regardless of how they were calculated.

type Rectangle

type Rectangle struct {
	Min, Max Vec2
}

Rectangle represents a rectangle defined by its minimum and maximum corners. The rectangle is defined such that Min contains the lower bounds and Max contains the upper bounds.

func FromRectangle

func FromRectangle(r image.Rectangle) Rectangle

FromRectangle converts an image.Rectangle to a geom.Rectangle. The integer coordinates are converted to float64 values.

func Rect

func Rect(x, y, w, h float64) Rectangle

Rect creates a rectangle given position (x,y) and size (w,h). The minimum corner is set to (x,y) and the maximum corner is set to (x+w, y+h).

func RectangleBySize

func RectangleBySize(pos Vec2, size Vec2) Rectangle

RectangleBySize creates a rectangle from a position and size. The position defines the a center of the rectangle.

func (Rectangle) Add

func (r Rectangle) Add(v Vec2) Rectangle

Add returns a rectangle translated by adding the given vector to both corners. This effectively moves the rectangle in the direction of the vector.

func (Rectangle) Center

func (r Rectangle) Center() Vec2

Center returns the center point of the rectangle. The center is calculated as the midpoint between the minimum and maximum corners.

func (Rectangle) Empty

func (r Rectangle) Empty() bool

Empty reports whether the rectangle has zero or negative area. A rectangle is considered empty if its width or height is less than or equal to zero.

func (Rectangle) HadamardDivide

func (r Rectangle) HadamardDivide(v Vec2) Rectangle

HadamardDivide returns element-wise division of the rectangle's corners by a vector. Both the Min and Max corners are divided component-wise by the given vector. Note: Division by zero components will result in infinity or NaN values.

func (Rectangle) HadamardProduct

func (r Rectangle) HadamardProduct(v Vec2) Rectangle

HadamardProduct returns element-wise product of the rectangle's corners with a vector. Both the Min and Max corners are multiplied component-wise by the given vector.

func (Rectangle) IntersectsCircle

func (r Rectangle) IntersectsCircle(center Vec2, radius float64) bool

IntersectsCircle checks if the rectangle intersects with a circle. The circle is defined by its center point and radius. Returns true if the rectangle and circle share any points.

func (Rectangle) Mul

func (r Rectangle) Mul(k float64) Rectangle

Mul returns a rectangle with both corners scaled by the scalar k. Each coordinate of both corners is multiplied by k.

func (Rectangle) Normalize

func (r Rectangle) Normalize() Rectangle

Normalize returns a rectangle with properly ordered corners. The minimum corner will contain the smaller values and the maximum corner will contain the larger values.

func (Rectangle) Overlaps

func (r Rectangle) Overlaps(s Rectangle) bool

Overlaps reports whether two rectangles intersect. Two rectangles overlap if they share any interior points. Empty rectangles never overlap.

func (Rectangle) Round

func (r Rectangle) Round() Rectangle

Round returns a rectangle with both corners rounded to the nearest integer coordinates.

func (Rectangle) Size

func (r Rectangle) Size() Vec2

Size returns the dimensions of the rectangle as a vector. The size is calculated as the difference between the maximum and minimum corners.

func (Rectangle) Sub

func (r Rectangle) Sub(v Vec2) Rectangle

Sub returns a rectangle translated by subtracting the given vector from both corners. This effectively moves the rectangle in the opposite direction of the vector.

type Vec2

type Vec2 [2]float64

Vec2 represents a 2D vector with X and Y components. The vector can be used to represent positions, directions, or offsets in 2D space.

func FromPoint

func FromPoint(pt image.Point) Vec2

FromPoint converts an image.Point to a Vec2. The integer coordinates of the point are converted to float64 values.

func FromPolar

func FromPolar(r float64, a Angle) Vec2

FromPolar creates a vector from polar coordinates. r is the vector length and a is the angle in radians measured from the positive X-axis.

func MaxVec2

func MaxVec2(u, v Vec2) Vec2

MaxVec2 returns a vector with the maximum components from two vectors. The result vector has components (max(u[0], v[0]), max(u[1], v[1])).

func MinVec2

func MinVec2(u, v Vec2) Vec2

MinVec2 returns a vector with the minimum components from two vectors. The result vector has components (min(u[0], v[0]), min(u[1], v[1])).

func RandVec2

func RandVec2() Vec2

RandVec2 creates a random vector with components in the range [0, 1). The resulting vector lies within the unit square from (0,0) to (1,1).

func (Vec2) Add

func (v Vec2) Add(u Vec2) Vec2

Add returns the sum of two vectors. The result vector has components (v[0]+u[0], v[1]+u[1]).

func (Vec2) Angle

func (v Vec2) Angle() Angle

Angle returns the angle of the vector relative to the positive X-axis. The angle is measured in radians, counter-clockwise, in the range [-π, π]. For a zero vector, the angle is defined as 0.

func (Vec2) Cross

func (v Vec2) Cross(u Vec2) float64

Cross returns the 2D cross product (z-component) of two vectors. The result is positive if u is counter-clockwise from v.

func (Vec2) Distance

func (v Vec2) Distance(u Vec2) float64

Distance returns the Euclidean distance between two vectors. This is equivalent to (v.Sub(u)).Length().

func (Vec2) Div

func (v Vec2) Div(k float64) Vec2

Div returns a new vector scaled down on the scalar k. Each component of the vector is divided on k.

func (Vec2) Dot

func (v Vec2) Dot(u Vec2) float64

Dot returns the dot product of two vectors. Calculated as v[0]*u[0] + v[1]*u[1].

func (Vec2) HadamardDevide

func (v Vec2) HadamardDevide(u Vec2) Vec2

HadamardDevide returns the element-wise division of two vectors. The result vector has components (v[0]/u[0], v[1]/u[1]). Note: Division by zero will result in infinity or NaN values.

func (Vec2) HadamardProduct

func (v Vec2) HadamardProduct(u Vec2) Vec2

HadamardProduct returns the element-wise product of two vectors. The result vector has components (v[0]*u[0], v[1]*u[1]).

func (Vec2) IsInsideRect

func (v Vec2) IsInsideRect(rect Rectangle) bool

IsInsideRect reports whether the vector lies within the rectangle bounds.

func (Vec2) Length

func (v Vec2) Length() float64

Length returns the Euclidean length (magnitude) of the vector. Calculated as sqrt(v[0]^2 + v[1]^2).

func (Vec2) Mul

func (v Vec2) Mul(k float64) Vec2

Mul returns a new vector scaled by the scalar k. Each component of the vector is multiplied by k.

func (Vec2) Normalize

func (v Vec2) Normalize() Vec2

Normalize returns a unit vector in the same direction as v. If v is a zero vector, the result will be a zero vector.

func (Vec2) Round

func (v Vec2) Round() Vec2

Round returns a new vector with each component rounded to the nearest integer.

func (Vec2) Sub

func (v Vec2) Sub(u Vec2) Vec2

Sub returns the difference between two vectors. The result vector has components (v[0]-u[0], v[1]-u[1]).

func (Vec2) ToImagePoint

func (v Vec2) ToImagePoint() image.Point

ToImagePoint converts the vector to an image.Point. Each component is rounded to the nearest integer and then cast to int.

func (Vec2) Unpack

func (v Vec2) Unpack() (float64, float64)

Unpack returns the X and Y components of the vector as separate float64 values.

Jump to

Keyboard shortcuts

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