API リファレンスはソースコード内の Go ドキュメントコメントから生成されており、英語で記述されています。
package math2d
import "github.com/nsgo-dev/game/pkg/math2d"概要
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).
目次
- type Affine
- func FromCore(m core.Matrix3D) (Affine, bool)
- func Identity() Affine
- func Rotate(rad float32) Affine
- func RotateAround(rad, cx, cy float32) Affine
- func Scale(sx, sy float32) Affine
- func TRS(pos Vec2, rot float32, scale Vec2) Affine
- func Translate(x, y float32) Affine
- func (m Affine) ApproxEqual(o Affine, eps float32) bool
- func (m Affine) Invert() (Affine, bool)
- func (m Affine) Mul(other Affine) Affine
- func (m Affine) ToCore() core.Matrix3D
- func (m Affine) TransformPoint(p Vec2) Vec2
- func (m Affine) TransformRect(r Rect) Rect
- func (m Affine) TransformVector(v Vec2) Vec2
- type Circle
- type Rect
- func FromBounds(b core.Bounds) Rect
- func FromCenter(center, size Vec2) Rect
- func FromMinMax(lo, hi Vec2) Rect
- func (r Rect) ApproxEqual(o Rect, eps float32) bool
- func (r Rect) Bounds() core.Bounds
- func (r Rect) Center() Vec2
- func (r Rect) ClampPoint(p Vec2) Vec2
- func (r Rect) ClosestPoint(p Vec2) Vec2
- func (r Rect) Contains(p Vec2) bool
- func (r Rect) ContainsRect(o Rect) bool
- func (r Rect) Empty() bool
- func (r Rect) Expand(margin float32) Rect
- func (r Rect) Intersection(o Rect) (Rect, bool)
- func (r Rect) Max() Vec2
- func (r Rect) Min() Vec2
- func (r Rect) Normalized() Rect
- func (r Rect) Overlaps(o Rect) bool
- func (r Rect) Size() Vec2
- func (r Rect) Translate(v Vec2) Rect
- func (r Rect) Union(o Rect) Rect
- type Vec2
- func FromAngle(rad float32) Vec2
- func FromPoint(p core.Point) Vec2
- func FromSize(s core.Size) Vec2
- func One() Vec2
- func UnitX() Vec2
- func UnitY() Vec2
- func V2(x, y float32) Vec2
- func Zero() Vec2
- func (v Vec2) Add(o Vec2) Vec2
- func (v Vec2) Angle() float32
- func (v Vec2) ApproxEqual(o Vec2, eps float32) bool
- func (v Vec2) Clamp(lo, hi Vec2) Vec2
- func (v Vec2) ClampLen(maxLen float32) Vec2
- func (v Vec2) Cross(o Vec2) float32
- func (v Vec2) Dist(o Vec2) float32
- func (v Vec2) DistSq(o Vec2) float32
- func (v Vec2) Div(o Vec2) Vec2
- func (v Vec2) Dot(o Vec2) float32
- func (v Vec2) Len() float32
- func (v Vec2) LenSq() float32
- func (v Vec2) Lerp(o Vec2, t float32) Vec2
- func (v Vec2) Max(o Vec2) Vec2
- func (v Vec2) Min(o Vec2) Vec2
- func (v Vec2) MoveTowards(target Vec2, maxDelta float32) Vec2
- func (v Vec2) Mul(o Vec2) Vec2
- func (v Vec2) Neg() Vec2
- func (v Vec2) Normalize() Vec2
- func (v Vec2) Perp() Vec2
- func (v Vec2) Point() core.Point
- func (v Vec2) Project(onto Vec2) Vec2
- func (v Vec2) Reflect(normal Vec2) Vec2
- func (v Vec2) Rotate(rad float32) Vec2
- func (v Vec2) Scale(s float32) Vec2
- func (v Vec2) Size() core.Size
- func (v Vec2) Sub(o Vec2) Vec2
型
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 Rotate(rad float32) AffineRotate returns a rotation transform by rad radians from +X toward +Y.
func RotateAround(rad, cx, cy float32) AffineRotateAround returns a rotation transform by rad radians about pivot (cx, cy).
func TRS(pos Vec2, rot float32, scale Vec2) AffineTRS 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) AffineTranslate returns a pure translation transform by (x, y).
func (m Affine) ApproxEqual(o Affine, eps float32) boolApproxEqual 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) AffineMul returns the matrix product m * other (other applied first, then m).
func (m Affine) ToCore() core.Matrix3DToCore converts m to an NSGo core.Matrix3D losslessly.
func (m Affine) TransformPoint(p Vec2) Vec2TransformPoint applies m (including translation) to point p.
func (m Affine) TransformRect(r Rect) RectTransformRect 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) Vec2TransformVector 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) boolApproxEqual 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() RectBounds 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) boolContains reports whether p lies inside or on the boundary of c.
func (c Circle) Overlaps(o Circle) boolOverlaps reports whether c and o intersect (including tangent contact).
func (c Circle) OverlapsRect(r Rect) boolOverlapsRect reports whether c and non-empty rectangle r intersect (including boundary contact).
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) RectFromBounds converts an NSGo core.Bounds to a Rect losslessly.
func FromCenter(center, size Vec2) RectFromCenter returns a Rect centered at center with the given size.
func FromMinMax(lo, hi Vec2) RectFromMinMax 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) boolApproxEqual 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.BoundsBounds converts r to an NSGo core.Bounds losslessly.
func (r Rect) ClampPoint(p Vec2) Vec2ClampPoint clamps p to the closed region spanned by r. Negative W or H is normalized before clamping.
func (r Rect) ClosestPoint(p Vec2) Vec2ClosestPoint 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) boolContains reports whether p lies within r's half-open bounds [X, X+W) × [Y, Y+H).
func (r Rect) ContainsRect(o Rect) boolContainsRect reports whether non-empty rectangle o lies completely inside non-empty r. It returns false if either rectangle is empty.
func (r Rect) Empty() boolEmpty reports whether r has zero or negative area or contains any NaN component.
func (r Rect) Expand(margin float32) RectExpand 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) Normalized() RectNormalized 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) boolOverlaps 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) Union(o Rect) RectUnion 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) Vec2FromAngle returns the unit vector at rad radians from +X toward +Y.
func FromPoint(p core.Point) Vec2FromPoint converts an NSGo core.Point to a Vec2 losslessly.
func FromSize(s core.Size) Vec2FromSize converts an NSGo core.Size to a Vec2 losslessly.
func (v Vec2) Angle() float32Angle returns the angle of v in radians in [-π, π] from +X toward +Y.
func (v Vec2) ApproxEqual(o Vec2, eps float32) boolApproxEqual 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) Vec2Clamp clamps each component of v to [lo, hi].
func (v Vec2) ClampLen(maxLen float32) Vec2ClampLen 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) float32Cross returns the 2D scalar cross product (v.X*o.Y - v.Y*o.X).
func (v Vec2) Dist(o Vec2) float32Dist returns the Euclidean distance between v and o.
func (v Vec2) DistSq(o Vec2) float32DistSq returns the squared distance between v and o.
func (v Vec2) Div(o Vec2) Vec2Div returns the component-wise quotient of v divided by o.
func (v Vec2) Lerp(o Vec2, t float32) Vec2Lerp linearly interpolates between v (at t=0) and o (at t=1).
func (v Vec2) MoveTowards(target Vec2, maxDelta float32) Vec2MoveTowards 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) Normalize() Vec2Normalize 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) Point() core.PointPoint converts v to an NSGo core.Point losslessly.
func (v Vec2) Project(onto Vec2) Vec2Project 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) Vec2Reflect returns v reflected across a surface with unit normal n.
func (v Vec2) Rotate(rad float32) Vec2Rotate returns v rotated by rad radians from +X toward +Y.