Skip to content

Commit 73e3918

Browse files
committed
Init image conversions and add mathematical utility functions
1 parent 24f80ee commit 73e3918

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

constraints.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"math"
66
)
77

8+
// Number is a generic number type supported by all types and functions in this package.
89
type Number interface {
910
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~float32 | ~float64
1011
}
@@ -21,9 +22,9 @@ func Cast[T Number](a float64) T {
2122
// ToString format Number as numeric string
2223
func ToString[T Number](value T) string {
2324
if isIntValue(value) {
24-
return fmt.Sprintf("%+d", int64(value))
25+
return fmt.Sprintf("%d", int64(value))
2526
} else {
26-
return fmt.Sprintf("%+.2f", float64(value))
27+
return fmt.Sprintf("%.2f", float64(value))
2728
}
2829
}
2930

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module github.com/gravitton/geometry
22

3-
go 1.24
3+
go 1.25
44

55
require github.com/gravitton/assert v0.4.0

image.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package geom
2+
3+
import (
4+
"image"
5+
)
6+
7+
func PointFromImage[T Number](p image.Point) Point[T] {
8+
return Point[T]{T(p.X), T(p.Y)}
9+
}
10+
11+
func SizeFromImage[T Number](r image.Rectangle) Size[T] {
12+
return Size[T]{T(r.Dx()), T(r.Dy())}
13+
}
14+
15+
func RectangleFromImage[T Number](r image.Rectangle) Rectangle[T] {
16+
return RectangleFromMin(PointFromImage[T](r.Min), SizeFromImage[T](r))
17+
}
18+
19+
func (p Point[T]) ToImagePoint() image.Point {
20+
return image.Point{int(p.X), int(p.Y)}
21+
}
22+
23+
func (s Size[T]) ToImageRectangle() image.Rectangle {
24+
return image.Rectangle{image.Point{0, 0}, image.Point{int(s.Width), int(s.Height)}}
25+
}
26+
27+
func (r Rectangle[T]) ToImageRectangle() image.Rectangle {
28+
return image.Rectangle{r.Min().ToImagePoint(), r.Max().ToImagePoint()}
29+
}

math.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ const (
99
DegToRad float64 = math.Pi / 180.0
1010

1111
Delta float64 = 1e-6
12+
13+
Sqrt3 = 1.732050807568877293527446341505872367
14+
OneOverSqrtTwo = 1 / math.Sqrt2
1215
)
1316

1417
// ToRadians converts degrees to radians
@@ -21,19 +24,27 @@ func ToDegrees(radians float64) float64 {
2124
return radians * RadToDeg
2225
}
2326

24-
// Scale multiple number by scale factor
25-
func Scale[T Number](a T, scale float64) T {
27+
// Multiple multiple number by scale factor
28+
func Multiple[T Number](a T, scale float64) T {
2629
return Cast[T](float64(a) * scale)
2730
}
2831

32+
// Divide divide number by scale factor
33+
func Divide[T Number](a T, scale float64) T {
34+
if scale == 0 {
35+
return a
36+
}
37+
38+
return Cast[T](float64(a) / scale)
39+
}
40+
2941
// Abs returns absolute value
3042
func Abs[T Number](a T) T {
3143
return Cast[T](math.Abs(float64(a)))
3244
}
3345

3446
// Midpoint calculate midpoint (point exactly halfway between two points)
3547
// Shorthand for `lerp(a, b, 0.5)`
36-
// TODO: fix rounding for int numbers
3748
func Midpoint[T Number](a, b T) T {
3849
// return Cast(lerp(float64(a), float64(b), 0.5))
3950
return Cast[T](midpoint(float64(a), float64(b)))
@@ -46,7 +57,6 @@ func midpoint(a, b float64) float64 {
4657
}
4758

4859
// Lerp calculate linear interpolation (point along a line between two points based on a given ratio)
49-
// TODO: fix rounding for int numbers
5060
func Lerp[T Number](a, b T, t float64) T {
5161
return Cast[T](lerp(float64(a), float64(b), t))
5262
}

0 commit comments

Comments
 (0)