1
0
Fork 0

Initial commit

This commit is contained in:
Henrik Hautakoski 2025-06-07 20:26:00 +02:00
commit f26c478727
18 changed files with 621 additions and 0 deletions

23
math/color.go Normal file
View file

@ -0,0 +1,23 @@
package math
type Color struct {
R, G, B, A uint8
}
func (c Color) Shade(value uint8) Color {
return Color{
R: c.R / value,
G: c.G / value,
B: c.B / value,
A: c.A,
}
}
func (c Color) Sub(value uint8) Color {
return Color{
R: c.R - value,
G: c.G - value,
B: c.B - value,
A: c.A,
}
}

7
math/degree.go Normal file
View file

@ -0,0 +1,7 @@
package math
import "math"
func DecToRad(deg float64) float64 {
return deg * (math.Pi / 180)
}

26
math/direction.go Normal file
View file

@ -0,0 +1,26 @@
package math
import (
stdmath "math"
)
type Direction float64
func (d *Direction) Set(v float64) {
*d = Direction(v)
}
func (d Direction) Get() float64 {
return float64(d)
}
func (d Direction) ForwardVector() Vec2f {
return Vec2f{
X: stdmath.Cos(float64(d)),
Y: stdmath.Sin(float64(d)),
}
}
func (d *Direction) Rotate(delta float64) {
*d += Direction(delta)
}

11
math/position.go Normal file
View file

@ -0,0 +1,11 @@
package math
type Position Vec2f
func (p *Position) Set(v Vec2f) {
*p = Position(v)
}
func (p Position) Get() Vec2f {
return Vec2f(p)
}

6
math/transform.go Normal file
View file

@ -0,0 +1,6 @@
package math
type Transform struct {
Position
Direction
}

82
math/vec2.go Normal file
View file

@ -0,0 +1,82 @@
package math
import stdmath "math"
type UnsignedInteger interface {
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}
type SignedInteger interface {
~int | ~int8 | ~int16 | ~int32 | ~int64
}
type Integer interface {
UnsignedInteger | SignedInteger
}
type Float interface {
~float32 | ~float64
}
type Number interface {
Integer | Float
}
type Vec2[T Number] struct {
X, Y T
}
type (
Vec2i = Vec2[int]
Vec2i32 = Vec2[int32]
Vec2f = Vec2[float64]
Vec2u8 = Vec2[uint8]
)
func (v Vec2[T]) Add(x, y T) Vec2[T] {
return Vec2[T]{
X: v.X + x,
Y: v.Y + y,
}
}
func (v Vec2[T]) AddS(s T) Vec2[T] {
return v.Add(s, s)
}
func (v Vec2[T]) AddVec(other Vec2[T]) Vec2[T] {
return Vec2[T]{
X: v.X + other.X,
Y: v.Y + other.Y,
}
}
func (v Vec2[T]) Mul(x, y T) Vec2[T] {
return Vec2[T]{
X: v.X * x,
Y: v.Y * y,
}
}
func (v Vec2[T]) MulVec(other Vec2[T]) Vec2[T] {
return v.Mul(other.X, other.Y)
}
func (v Vec2[T]) Scale(f T) Vec2[T] {
return v.Mul(f, f)
}
func (v Vec2[T]) Length() float64 {
return stdmath.Sqrt(float64(v.X*v.X) + float64(v.Y*v.Y))
}
func (v Vec2[T]) Normalize() Vec2[T] {
l := v.Length()
if l <= 0.0 {
return Vec2[T]{}
}
return Vec2[T]{
X: T(float64(v.X) / l),
Y: T(float64(v.Y) / l),
}
}