GitHubCommencer

La référence API provient des commentaires de documentation Go du code source et est rédigée en anglais.

package math2d

import "github.com/nsgo-dev/game/pkg/math2d"

Vue d’ensemble

Package math2d provides small, zero-allocation 2D value types shared by game packages: vectors (Vec2), axis-aligned rectangles (Rect), circles (Circle), and 3×2 affine transforms (Affine).

Coordinates use float32 to match NSGo's core geometry (core.Points, core.Point, core.Size, core.Bounds) and core.Drawing, with lossless conversions to and from core. Angles are in radians, turning from +X toward +Y (clockwise in NSGo's Y-down drawing coordinates).

Index

Types

type Affine struct {
	A, B   float32
	C, D   float32
	Tx, Ty float32
}

Affine is a 2D 3×2 affine transform stored in the same column order as core.Drawing.Transform and core.Matrix3D.Affine (Canvas 2D / SVG matrix(a, b, c, d, e, f)):

[ A  C  Tx ] [ x ]   [ A*x + C*y + Tx ]
[ B  D  Ty ] [ y ] = [ B*x + D*y + Ty ]
[ 0  0  1  ] [ 1 ]   [ 1              ]

The zero value maps every point to (0, 0); use Identity for the no-op transform.

func FromCore(m core.Matrix3D) (Affine, bool)

FromCore extracts a 2D Affine from an NSGo core.Matrix3D. It returns (Affine{}, false) if m is not a pure 2D affine transform.

func Identity() Affine

Identity returns the identity affine transform.

func Rotate(rad float32) Affine

Rotate returns a rotation transform by rad radians from +X toward +Y.

func RotateAround(rad, cx, cy float32) Affine

RotateAround returns a rotation transform by rad radians about pivot (cx, cy).

func Scale(sx, sy float32) Affine

Scale returns a pure scaling transform by (sx, sy).

func TRS(pos Vec2, rot float32, scale Vec2) Affine

TRS returns a transform that scales by scale, rotates by rot radians, and translates by pos: Translate(pos.X, pos.Y).Mul(Rotate(rot)).Mul(Scale(scale.X, scale.Y)).

func Translate(x, y float32) Affine

Translate returns a pure translation transform by (x, y).

func (m Affine) ApproxEqual(o Affine, eps float32) bool

ApproxEqual reports whether each element of m and o differs by at most eps. It returns false if eps < 0 or any value is NaN.

func (m Affine) Invert() (Affine, bool)

Invert returns the inverse of m, or (Affine{}, false) if m is singular, degenerate, or contains non-finite values.

func (m Affine) Mul(other Affine) Affine

Mul returns the matrix product m * other (other applied first, then m).

func (m Affine) ToCore() core.Matrix3D

ToCore converts m to an NSGo core.Matrix3D losslessly.

func (m Affine) TransformPoint(p Vec2) Vec2

TransformPoint applies m (including translation) to point p.

func (m Affine) TransformRect(r Rect) Rect

TransformRect transforms the four corners of r by m and returns the tightest axis-aligned bounding Rect enclosing them. It returns the zero Rect if r is empty.

func (m Affine) TransformVector(v Vec2) Vec2

TransformVector applies only the linear part of m (ignoring translation) to vector v.

type Circle struct {
	Center Vec2
	Radius float32
}

Circle is a 2D circle defined by its Center and Radius.

A circle with Radius <= 0 (or NaN) is empty and contains or overlaps nothing.

func (c Circle) ApproxEqual(o Circle, eps float32) bool

ApproxEqual reports whether c and o have centers and radii within eps. It returns false if eps < 0 or any value is NaN.

func (c Circle) Bounds() Rect

Bounds returns the axis-aligned bounding Rect of c, or the zero Rect if Radius < 0 or any component is NaN.

func (c Circle) Contains(p Vec2) bool

Contains reports whether p lies inside or on the boundary of c.

func (c Circle) Overlaps(o Circle) bool

Overlaps reports whether c and o intersect (including tangent contact).

func (c Circle) OverlapsRect(r Rect) bool

OverlapsRect reports whether c and non-empty rectangle r intersect (including boundary contact).

func (c Circle) Translate(v Vec2) Circle

Translate returns c shifted by v.

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

Rect is an axis-aligned 2D rectangle given by its origin (X, Y) and size (W, H).

A rectangle with W <= 0 or H <= 0 (or any NaN field) is empty. Containment uses half-open intervals [X, X+W) × [Y, Y+H), matching NSGo's core.Bounds.

func FromBounds(b core.Bounds) Rect

FromBounds converts an NSGo core.Bounds to a Rect losslessly.

func FromCenter(center, size Vec2) Rect

FromCenter returns a Rect centered at center with the given size.

func FromMinMax(lo, hi Vec2) Rect

FromMinMax returns a Rect spanning from lo to hi. If lo has a component greater than hi, the resulting Rect has negative W or H; call Normalized to flip negative extents.

func (r Rect) ApproxEqual(o Rect, eps float32) bool

ApproxEqual reports whether each field of r and o differs by at most eps. It returns false if eps < 0 or any value is NaN.

func (r Rect) Bounds() core.Bounds

Bounds converts r to an NSGo core.Bounds losslessly.

func (r Rect) Center() Vec2

Center returns the midpoint of r.

func (r Rect) ClampPoint(p Vec2) Vec2

ClampPoint clamps p to the closed region spanned by r. Negative W or H is normalized before clamping.

func (r Rect) ClosestPoint(p Vec2) Vec2

ClosestPoint returns the point in the closed rectangle r closest to p. For points inside r, it returns p; for points outside, it returns the nearest point on r's perimeter.

func (r Rect) Contains(p Vec2) bool

Contains reports whether p lies within r's half-open bounds [X, X+W) × [Y, Y+H).

func (r Rect) ContainsRect(o Rect) bool

ContainsRect reports whether non-empty rectangle o lies completely inside non-empty r. It returns false if either rectangle is empty.

func (r Rect) Empty() bool

Empty reports whether r has zero or negative area or contains any NaN component.

func (r Rect) Expand(margin float32) Rect

Expand returns r expanded outward by margin on all four sides (negative shrinks).

func (r Rect) Intersection(o Rect) (Rect, bool)

Intersection returns the overlapping region of r and o, or (Rect{}, false) if they do not overlap or either is empty.

func (r Rect) Max() Vec2

Max returns the far corner (X+W, Y+H) of r.

func (r Rect) Min() Vec2

Min returns the origin corner (X, Y) of r.

func (r Rect) Normalized() Rect

Normalized returns an equivalent Rect with non-negative W and H, shifting X or Y when W or H is negative.

func (r Rect) Overlaps(o Rect) bool

Overlaps reports whether r and o have a non-empty intersection. Rectangles that touch only along an edge or corner do not overlap.

func (r Rect) Size() Vec2

Size returns the dimensions (W, H) of r.

func (r Rect) Translate(v Vec2) Rect

Translate returns r shifted by v.

func (r Rect) Union(o Rect) Rect

Union returns the smallest Rect containing both r and o. An empty rectangle acts as the identity element.

type Vec2 struct {
	X, Y float32
}

Vec2 is a 2D vector or point in float32 coordinates.

func FromAngle(rad float32) Vec2

FromAngle returns the unit vector at rad radians from +X toward +Y.

func FromPoint(p core.Point) Vec2

FromPoint converts an NSGo core.Point to a Vec2 losslessly.

func FromSize(s core.Size) Vec2

FromSize converts an NSGo core.Size to a Vec2 losslessly.

func One() Vec2

One returns the vector (1, 1).

func UnitX() Vec2

UnitX returns the positive X unit vector (1, 0).

func UnitY() Vec2

UnitY returns the positive Y unit vector (0, 1).

func V2(x, y float32) Vec2

V2 returns Vec2{X: x, Y: y}.

func Zero() Vec2

Zero returns the zero vector (0, 0).

func (v Vec2) Add(o Vec2) Vec2

Add returns v + o.

func (v Vec2) Angle() float32

Angle returns the angle of v in radians in [-π, π] from +X toward +Y.

func (v Vec2) ApproxEqual(o Vec2, eps float32) bool

ApproxEqual reports whether each component of v and o differs by at most eps. It returns false if eps < 0 or any value is NaN.

func (v Vec2) Clamp(lo, hi Vec2) Vec2

Clamp clamps each component of v to [lo, hi].

func (v Vec2) ClampLen(maxLen float32) Vec2

ClampLen returns v scaled down so its length does not exceed maxLen. If maxLen <= 0 or NaN, or v has NaN or infinite length, it returns the zero vector.

func (v Vec2) Cross(o Vec2) float32

Cross returns the 2D scalar cross product (v.X*o.Y - v.Y*o.X).

func (v Vec2) Dist(o Vec2) float32

Dist returns the Euclidean distance between v and o.

func (v Vec2) DistSq(o Vec2) float32

DistSq returns the squared distance between v and o.

func (v Vec2) Div(o Vec2) Vec2

Div returns the component-wise quotient of v divided by o.

func (v Vec2) Dot(o Vec2) float32

Dot returns the dot product of v and o.

func (v Vec2) Len() float32

Len returns the Euclidean length of v.

func (v Vec2) LenSq() float32

LenSq returns the squared length of v.

func (v Vec2) Lerp(o Vec2, t float32) Vec2

Lerp linearly interpolates between v (at t=0) and o (at t=1).

func (v Vec2) Max(o Vec2) Vec2

Max returns the component-wise maximum of v and o.

func (v Vec2) Min(o Vec2) Vec2

Min returns the component-wise minimum of v and o.

func (v Vec2) MoveTowards(target Vec2, maxDelta float32) Vec2

MoveTowards moves v toward target by at most maxDelta without overshooting. If maxDelta <= 0 or NaN, or the distance is NaN, it returns v unchanged.

func (v Vec2) Mul(o Vec2) Vec2

Mul returns the component-wise product of v and o.

func (v Vec2) Neg() Vec2

Neg returns -v.

func (v Vec2) Normalize() Vec2

Normalize returns a unit vector in the direction of v. If v has zero length, NaN, or infinite length, it returns the zero vector.

func (v Vec2) Perp() Vec2

Perp returns v rotated by +π/2 radians: (-v.Y, v.X).

func (v Vec2) Point() core.Point

Point converts v to an NSGo core.Point losslessly.

func (v Vec2) Project(onto Vec2) Vec2

Project returns the orthogonal projection of v onto onto. If onto has zero length, NaN, or infinite length, it returns the zero vector.

func (v Vec2) Reflect(normal Vec2) Vec2

Reflect returns v reflected across a surface with unit normal n.

func (v Vec2) Rotate(rad float32) Vec2

Rotate returns v rotated by rad radians from +X toward +Y.

func (v Vec2) Scale(s float32) Vec2

Scale returns v multiplied by scalar s.

func (v Vec2) Size() core.Size

Size converts v to an NSGo core.Size losslessly.

func (v Vec2) Sub(o Vec2) Vec2

Sub returns v - o.