Documentation
¶
Overview ¶
Package geom provides small 2D geometry helpers for vectors, rectangles, and angles.
Index ¶
- func Clamp(x, low, high float64) float64
- func IsInsidePoly(v Vec2, poly []Vec2) bool
- func SmoothStep(edge0, edge1, x float64) float64
- type Angle
- type Rectangle
- func (r Rectangle) Add(v Vec2) Rectangle
- func (r Rectangle) Center() Vec2
- func (r Rectangle) Empty() bool
- func (r Rectangle) HadamardDivide(v Vec2) Rectangle
- func (r Rectangle) HadamardProduct(v Vec2) Rectangle
- func (r Rectangle) IntersectsCircle(center Vec2, radius float64) bool
- func (r Rectangle) Mul(k float64) Rectangle
- func (r Rectangle) Normalize() Rectangle
- func (r Rectangle) Overlaps(s Rectangle) bool
- func (r Rectangle) Round() Rectangle
- func (r Rectangle) Size() Vec2
- func (r Rectangle) Sub(v Vec2) Rectangle
- type Vec2
- func (v Vec2) Add(u Vec2) Vec2
- func (v Vec2) Angle() Angle
- func (v Vec2) Cross(u Vec2) float64
- func (v Vec2) Distance(u Vec2) float64
- func (v Vec2) Div(k float64) Vec2
- func (v Vec2) Dot(u Vec2) float64
- func (v Vec2) HadamardDevide(u Vec2) Vec2
- func (v Vec2) HadamardProduct(u Vec2) Vec2
- func (v Vec2) IsInsideRect(rect Rectangle) bool
- func (v Vec2) Length() float64
- func (v Vec2) Mul(k float64) Vec2
- func (v Vec2) Normalize() Vec2
- func (v Vec2) Round() Vec2
- func (v Vec2) Sub(u Vec2) Vec2
- func (v Vec2) ToImagePoint() image.Point
- func (v Vec2) Unpack() (float64, float64)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsInsidePoly ¶
IsInsidePoly reports whether v lies inside polygon p using the ray casting method. Points on the polygon boundary are treated as inside.
func SmoothStep ¶
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.
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 ¶
FromRectangle converts an image.Rectangle to a geom.Rectangle. The integer coordinates are converted to float64 values.
func Rect ¶
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 ¶
RectangleBySize creates a rectangle from a position and size. The position defines the a center of the rectangle.
func (Rectangle) Add ¶
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 ¶
Center returns the center point of the rectangle. The center is calculated as the midpoint between the minimum and maximum corners.
func (Rectangle) Empty ¶
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 ¶
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 ¶
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 ¶
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 ¶
Mul returns a rectangle with both corners scaled by the scalar k. Each coordinate of both corners is multiplied by k.
func (Rectangle) Normalize ¶
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 ¶
Overlaps reports whether two rectangles intersect. Two rectangles overlap if they share any interior points. Empty rectangles never overlap.
func (Rectangle) Round ¶
Round returns a rectangle with both corners rounded to the nearest integer coordinates.
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 ¶
FromPoint converts an image.Point to a Vec2. The integer coordinates of the point are converted to float64 values.
func FromPolar ¶
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 ¶
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 ¶
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 ¶
Add returns the sum of two vectors. The result vector has components (v[0]+u[0], v[1]+u[1]).
func (Vec2) 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 ¶
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 ¶
Distance returns the Euclidean distance between two vectors. This is equivalent to (v.Sub(u)).Length().
func (Vec2) Div ¶
Div returns a new vector scaled down on the scalar k. Each component of the vector is divided on k.
func (Vec2) HadamardDevide ¶
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 ¶
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 ¶
IsInsideRect reports whether the vector lies within the rectangle bounds.
func (Vec2) Length ¶
Length returns the Euclidean length (magnitude) of the vector. Calculated as sqrt(v[0]^2 + v[1]^2).
func (Vec2) Mul ¶
Mul returns a new vector scaled by the scalar k. Each component of the vector is multiplied by k.
func (Vec2) Normalize ¶
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) Sub ¶
Sub returns the difference between two vectors. The result vector has components (v[0]-u[0], v[1]-u[1]).
func (Vec2) ToImagePoint ¶
ToImagePoint converts the vector to an image.Point. Each component is rounded to the nearest integer and then cast to int.