scath

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

README

scath

scath stands for custom math. It's here because we may want ot reuse this in some other stuff. May also be published as a separate package at some point, depending on how much we end up putting into here.

What this does

This contains custom implementations for functions we may need in this game. It may not be completely limited to working with numbers, strings and stuff shall also be supported at some point.

Documentation

Overview

Scaff + math = scath. As you can tell, this is scaff's math package for things like padding, vectors and more.

Vec provides abstractions over vector math. A lot of the code for it is taken from https://github.com/setanarut/v.

Padding is for general padding in UI and more.

Timeframe provides functions to deal with, well, time frames. Various lerp functions are available as well to help with animations and transitions.

Constraints are normal rectangle constraints with lots of utility.

Index

Constants

View Source
const Infinite float64 = -1

Infinite represents an unbounded maximum size.

Variables

View Source
var (
	// Zero Vec{0, 0} vector is a vector with all components set to 0.
	Zero = Vec{0, 0}
	// One Vec{1, 1} vector is a vector with all components set to 1.
	One = Vec{1, 1}
	// Left unit vector. Vec{-1, 0} Represents the direction of left.
	Left = Vec{-1, 0}
	// Right unit vector. Vec{1, 0} Represents the direction of right.
	Right = Vec{1, 0}
	// Up unit vector. Vec{0, -1} Y is down in 2D, so this vector points -Y.
	Up = Vec{0, -1}
	// Down unit vector. Vec{0, 1} Y is down in 2D, so this vector points +Y.
	Down = Vec{0, 1}
)

Functions

This section is empty.

Types

type Constraints

type Constraints struct {
	MinX float64
	MaxX float64
	MinY float64
	MaxY float64
}

func Expand

func Expand(width, height float64) Constraints

Expand returns constraints that fill specified finite axes.

func Loose

func Loose(maxX, maxY float64) Constraints

Loose returns constraints that are only bounded by maxima.

func Minimum

func Minimum(minX, minY float64) Constraints

func NewConstraints

func NewConstraints(minX, maxX, minY, maxY float64) Constraints

NewConstraints creates normalized width and height constraints.

func Tight

func Tight(width, height float64) Constraints

Tight returns constraints with an exact width and height.

func TightFor

func TightFor(width, height float64) Constraints

TightFor returns tight constraints for provided axes.

func TightForFinite

func TightForFinite(width, height float64) Constraints

TightForFinite returns tight constraints only for finite axes.

func Unconstrained

func Unconstrained() Constraints

Unconstrained returns constraints with no upper bound.

func (Constraints) DoesXFit

func (c Constraints) DoesXFit(w float64) bool

func (Constraints) DoesYFit

func (c Constraints) DoesYFit(h float64) bool

func (Constraints) Fits

func (c Constraints) Fits(c2 Constraints) bool

func (Constraints) IsTight

func (c Constraints) IsTight() bool

func (Constraints) Max

func (c Constraints) Max(horizontal bool) float64

Find the max size of constraints.

func (Constraints) Min

func (c Constraints) Min(horizontal bool) float64

Find the min size of constraints.

func (Constraints) RealMax

func (c Constraints) RealMax(horizontal bool) float64

Find the max size of constraints.

func (Constraints) RealMaxX

func (c Constraints) RealMaxX() float64

func (Constraints) RealMaxY

func (c Constraints) RealMaxY() float64

func (Constraints) SubtractPadding

func (c Constraints) SubtractPadding(padding Padding) Constraints

SubtractPadding shrinks constraints by horizontal and vertical padding totals.

func (Constraints) TakeMaxWithin

func (c Constraints) TakeMaxWithin(c2 Constraints) (Vec, error)

type Padding

type Padding struct {
	Top    float64
	Bottom float64
	Right  float64
	Left   float64
}

func NewPadding

func NewPadding(top, right, bottom, left float64) Padding

NewPadding creates padding with explicit top, right, bottom, and left values.

func Pad

func Pad(value float64) Padding

Pad creates uniform padding on all sides.

func PadBottom

func PadBottom(value float64) Padding

PadBottom creates padding only on bottom side.

func PadHorizontal

func PadHorizontal(value float64) Padding

PadHorizontal creates padding on left and right sides.

func PadLeft

func PadLeft(value float64) Padding

PadLeft creates padding only on left side.

func PadRight

func PadRight(value float64) Padding

PadRight creates padding only on right side.

func PadTop

func PadTop(value float64) Padding

PadTop creates padding only on top side.

func PadVertical

func PadVertical(value float64) Padding

PadVertical creates padding on top and bottom sides.

func (Padding) ToVecTopLeft

func (p Padding) ToVecTopLeft() Vec

ToVecTopLeft converts top and left padding into position vector.

type Timeframe

type Timeframe struct {
	// contains filtered or unexported fields
}

Timeframe represents a duration with a start and end time.

func NewTimeframe

func NewTimeframe(start time.Time, duration time.Duration) Timeframe

NewTimeframe creates a new Timeframe with the given start time and duration.

func (Timeframe) AddDelay

func (f Timeframe) AddDelay(delay time.Duration) Timeframe

AddDelay adds a delay to the end of the timeframe.

func (Timeframe) IsBackwards

func (f Timeframe) IsBackwards() bool

IsBackwards returns true if the timeframe is backwards.

func (Timeframe) LerpFloat

func (f Timeframe) LerpFloat(now time.Time, a, b float64) float64

Linear interpolation between two floating-point numbers.

func (Timeframe) LerpInt

func (f Timeframe) LerpInt(now time.Time, a, b int) int

Linear interpolation between two integers.

func (Timeframe) LerpTypewriter

func (f Timeframe) LerpTypewriter(now time.Time, a string) string

LerpTypewriter writes a string with a typewriter effect, constructing it over time.

func (Timeframe) LerpTypewriterBetween

func (f Timeframe) LerpTypewriterBetween(now time.Time, a, b string) string

LerpTypewriterBetween writes a string with a typewriter effect between two strings, deleting the first and then writing the second.

func (Timeframe) LerpUint

func (f Timeframe) LerpUint(now time.Time, a, b uint) uint

Linear interpolation between two unsigned integers.

func (Timeframe) MakeBackwards

func (f Timeframe) MakeBackwards() Timeframe

MakeBackwards sets the timeframe to be backwards (end < start).

func (Timeframe) MakeForwards

func (f Timeframe) MakeForwards() Timeframe

MakeForwards sets the timeframe to be forwards (end > start).

func (Timeframe) Over

func (f Timeframe) Over(now time.Time) bool

Over returns true if the current time is after the end of the timeframe.

func (Timeframe) Remaining

func (f Timeframe) Remaining(now time.Time) time.Duration

Remaining returns the remaining duration until the end of the timeframe.

Also clamps to 0 if the current time is after the end of the timeframe. Or to the duration.

func (Timeframe) Started

func (f Timeframe) Started(now time.Time) bool

Started returns true if the current time is after the start of the timeframe.

type Vec

type Vec struct {
	X float64
	Y float64
}

func FromAngle

func FromAngle(angle float64) Vec

FromAngle makes a new 2D unit vector from an angle

func (Vec) Abs

func (v Vec) Abs() Vec

Abs returns the absolute value of vector.

func (Vec) AbsX

func (v Vec) AbsX() float64

AbsX returns the absolute X value of vector.

func (Vec) AbsY

func (v Vec) AbsY() float64

AbsY returns the absolute Y value of vector.

func (Vec) Add

func (v Vec) Add(a Vec) Vec

Add returns this + a

func (Vec) AddC

func (v Vec) AddC(c float64) Vec

Add returns this + c

func (Vec) AddPadding

func (v Vec) AddPadding(padding Padding) Vec

AddPadding grows v by horizontal and vertical padding totals.

func (Vec) Angle

func (v Vec) Angle() float64

Angle returns the angular direction v is pointing in (in radians).

func (Vec) AngleTo

func (v Vec) AngleTo(other Vec) float64

AngleTo returns the angle to the given vector, in radians.

func (Vec) Ceil

func (v Vec) Ceil() Vec

Ceil returns vector with all components rounded up (towards positive infinity).

func (Vec) Cross

func (v Vec) Cross(other Vec) float64

Cross calculates the 2D vector cross product analog. The cross product of 2D vectors results in a 3D vector with only a z component. This function returns the magnitude of the z value.

func (Vec) Dist

func (v Vec) Dist(other Vec) float64

Dist returns distance between v and other.

func (Vec) DistSq

func (v Vec) DistSq(other Vec) float64

DistSq returns the squared distance between this and other.

Faster than v.Dist() when you only need to compare distances.

func (Vec) Div

func (v Vec) Div(a Vec) Vec

Div divides this vector by a.

func (Vec) DivS

func (v Vec) DivS(s float64) Vec

DivS divides this vector by scalar value s.

func (Vec) Dot

func (v Vec) Dot(other Vec) float64

Dot returns dot product

func (Vec) Equals

func (v Vec) Equals(other Vec) bool

Equals checks if two vectors are equal. (Be careful when comparing floating point numbers!)

func (Vec) EqualsPr

func (v Vec) EqualsPr(other Vec, allowedDelta float64) bool

EqualsP returns they are practically equal with each other within a delta tolerance.

func (Vec) FitsWithin

func (v Vec) FitsWithin(c Constraints) bool

FitsWithin checks if x and y (treated as width and height) fit into constraints

func (Vec) Floor

func (v Vec) Floor() Vec

Floor returns vector with all components rounded down (towards negative infinity).

func (Vec) IsWithinRectangle

func (v Vec) IsWithinRectangle(start Vec, size Vec) bool

IsWithin checks if the current position is in the rectangle starting at start with size.

func (Vec) IsZero

func (v Vec) IsZero() bool

IsZero returns true if vector is zero vector

func (Vec) Lerp

func (v Vec) Lerp(other Vec, t float64) Vec

Lerp linearly interpolates between this and other vector.

func (Vec) Limit

func (v Vec) Limit(max float64) Vec

Limits a vector's magnitude to a maximum value.

func (Vec) Mag

func (v Vec) Mag() float64

Mag returns the magnitude (length) of the vector.

func (Vec) MagSq

func (v Vec) MagSq() float64

MagSq returns the magnitude (length) of the vector, squared.

This method is often used to improve performance since, unlike Mag(), it does not require a Sqrt() operation.

func (Vec) Mul

func (v Vec) Mul(a Vec) Vec

Mul returns this * a

func (Vec) Neg

func (v Vec) Neg() Vec

Neg negates a vector.

func (Vec) NegX

func (v Vec) NegX() Vec

NegY negates X.

func (Vec) NegY

func (v Vec) NegY() Vec

NegY negates Y.

func (Vec) Project

func (v Vec) Project(other Vec) Vec

Returns the vector projection onto other.

func (Vec) Reflect

func (v Vec) Reflect(normal Vec) Vec

Reflect returns the reflection of the vector v over the given normal. normal should be a normalized (unit) vector.

func (Vec) Rotate

func (v Vec) Rotate(angle float64) Vec

Rotate a vector by an angle in radians

func (Vec) Round

func (v Vec) Round() Vec

Round returns the nearest integer Vector, rounding half away from zero.

func (Vec) Scale

func (v Vec) Scale(s float64) Vec

Scale scales vector

func (Vec) Slerp

func (v Vec) Slerp(to Vec, weight float64) Vec

Slerp performs spherical linear interpolation between two vectors with given weight value in [0,1] range, returning interpolated vector

func (Vec) String

func (v Vec) String() string

String returns string representation of this vector.

func (Vec) Sub

func (v Vec) Sub(a Vec) Vec

Sub returns this - a

func (Vec) SubtractPadding

func (v Vec) SubtractPadding(padding Padding) Vec

SubtractPadding shrinks v by horizontal and vertical padding totals.

func (Vec) Unit

func (v Vec) Unit() Vec

Unit returns a normalized copy of this vector (unit vector).

Jump to

Keyboard shortcuts

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