Skip to content

Commit 382785f

Browse files
committed
Add missing String method
1 parent 665b88f commit 382785f

File tree

8 files changed

+60
-36
lines changed

8 files changed

+60
-36
lines changed

image.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ import (
44
"image"
55
)
66

7-
// PtFromImage converts a Point from an image.Point.
8-
func PtFromImage[T Number](p image.Point) Point[T] {
7+
// PointFromImage converts a Point from an image.Point.
8+
func PointFromImage[T Number](p image.Point) Point[T] {
99
return Point[T]{T(p.X), T(p.Y)}
1010
}
1111

12-
// SzFromImage converts a Size from an image.Rectangle.
13-
func SzFromImage[T Number](r image.Rectangle) Size[T] {
12+
// SizeFromImage converts a Size from an image.Rectangle.
13+
func SizeFromImage[T Number](r image.Rectangle) Size[T] {
1414
return Size[T]{T(r.Dx()), T(r.Dy())}
1515
}
1616

1717
// RectFromImage converts a Rectangle from an image.Rectangle.
1818
func RectFromImage[T Number](r image.Rectangle) Rectangle[T] {
19-
return RectFromMin(PtFromImage[T](r.Min), SzFromImage[T](r))
19+
return RectFromMin(PointFromImage[T](r.Min), SizeFromImage[T](r))
2020
}
2121

2222
// Point converts a Point to an image.Point.

padding.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
package geom
22

3+
import "fmt"
4+
35
// Padding represents a 2D padding.
46
type Padding[T Number] struct {
5-
Top, Right, Bottom, Left T
7+
Top T `json:"t"`
8+
Right T `json:"r"`
9+
Bottom T `json:"b"`
10+
Left T `json:"l"`
611
}
712

813
// Pad is shorthand for Padding{top, right, bottom, left}.
914
func Pad[T Number](top, right, bottom, left T) Padding[T] {
1015
return Padding[T]{top, right, bottom, left}
1116
}
1217

13-
// PadU is shorthand for Padding{padding, padding, padding, padding}.
14-
func PadU[T Number](padding T) Padding[T] {
15-
return Padding[T]{padding, padding, padding, padding}
16-
}
17-
18-
// PadXY is shorthand for Padding{topBottom, leftRight, topBottom, leftRight}.
19-
func PadXY[T Number](topBottom, leftRight T) Padding[T] {
20-
return Padding[T]{topBottom, leftRight, topBottom, leftRight}
21-
}
22-
2318
// Width returns the width of the padding.
2419
func (p Padding[T]) Width() T {
2520
return p.Left + p.Right
@@ -49,3 +44,18 @@ func (p Padding[T]) Int() Padding[int] {
4944
func (p Padding[T]) Float() Padding[float64] {
5045
return Padding[float64]{float64(p.Top), float64(p.Right), float64(p.Bottom), float64(p.Left)}
5146
}
47+
48+
// String returns a string representation of the Padding.
49+
func (p Padding[T]) String() string {
50+
return fmt.Sprintf("Pad(%s;%s;%s;%s)", String(p.Top), String(p.Right), String(p.Bottom), String(p.Left))
51+
}
52+
53+
// PadU is shorthand for Padding{padding, padding, padding, padding}.
54+
func PadU[T Number](padding T) Padding[T] {
55+
return Padding[T]{padding, padding, padding, padding}
56+
}
57+
58+
// PadXY is shorthand for Padding{topBottom, leftRight, topBottom, leftRight}.
59+
func PadXY[T Number](topBottom, leftRight T) Padding[T] {
60+
return Padding[T]{topBottom, leftRight, topBottom, leftRight}
61+
}

polygon.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package geom
22

33
import (
4+
"fmt"
5+
"strings"
6+
47
"github.com/gravitton/x/slices"
58
)
69

@@ -60,10 +63,15 @@ func (p Polygon[T]) Empty() bool {
6063

6164
// Int converts the polygon to a [int] polygon.
6265
func (p Polygon[T]) Int() Polygon[int] {
63-
return Polygon[int]{slices.Map(p.Vertices, (Point[T]).Int)}
66+
return Polygon[int]{slices.Map(p.Vertices, Point[T].Int)}
6467
}
6568

6669
// Float converts the polygon to a [float64] polygon.
6770
func (p Polygon[T]) Float() Polygon[float64] {
68-
return Polygon[float64]{slices.Map(p.Vertices, (Point[T]).Float)}
71+
return Polygon[float64]{slices.Map(p.Vertices, Point[T].Float)}
72+
}
73+
74+
// String returns a string representation of the Polygon.
75+
func (p Polygon[T]) String() string {
76+
return fmt.Sprintf("Pol(%s)", strings.Join(slices.Map(p.Vertices, Point[T].String), ", "))
6977
}

rectangle.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66

77
// Rectangle is a 2D axis-aligned rectangle represented by its center and size.
88
type Rectangle[T Number] struct {
9-
Center Point[T]
10-
Size Size[T]
9+
Center Point[T] `json:",inline"`
10+
Size Size[T] `json:",inline"`
1111
}
1212

1313
// Rect is shorthand for Rectangle{center, size}.

regular_polygon.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package geom
22

33
import (
4+
"fmt"
45
"math"
56
)
67

@@ -51,7 +52,7 @@ func (rp RegularPolygon[T]) Vertices() []Point[T] {
5152
vertices := make([]Point[T], rp.N)
5253
for i := 0; i < rp.N; i++ {
5354
// TODO: Vec(1,0).Rotate(angle)
54-
vertices[i] = rp.Center.Add(VecFromAngle[T](initAngle+float64(i)*angleStep, 1).MultiplyXY(float64(rp.Size.Width), float64(rp.Size.Height)))
55+
vertices[i] = rp.Center.Add(VectorFromAngle[T](initAngle+float64(i)*angleStep, 1).MultiplyXY(float64(rp.Size.Width), float64(rp.Size.Height)))
5556
}
5657

5758
return vertices
@@ -94,6 +95,11 @@ func (rp RegularPolygon[T]) Float() RegularPolygon[float64] {
9495
return RegularPolygon[float64]{rp.Center.Float(), rp.Size.Float(), rp.N, rp.Angle}
9596
}
9697

98+
// String returns a string representation of the RegularPolygon.
99+
func (rp RegularPolygon[T]) String() string {
100+
return fmt.Sprintf("RegPol(%s;%s;%s;%s)", rp.Center.String(), rp.Size.String(), String(rp.N), String(rp.Angle))
101+
}
102+
97103
type Orientation int
98104

99105
const (

size.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ func Sz[T Number](width, height T) Size[T] {
1515
return Size[T]{width, height}
1616
}
1717

18-
// SzU is shorthand for Size{size, size}.
19-
func SzU[T Number](size T) Size[T] {
20-
return Size[T]{size, size}
21-
}
22-
2318
// Scale creates a new Size scaled by the given factor in both dimensions.
2419
func (s Size[T]) Scale(factor float64) Size[T] {
2520
return Size[T]{Multiple(s.Width, factor), Multiple(s.Height, factor)}
@@ -99,3 +94,8 @@ func (s Size[T]) Float() Size[float64] {
9994
func (s Size[T]) String() string {
10095
return fmt.Sprintf("%sx%s", String(s.Width), String(s.Height))
10196
}
97+
98+
// SzU is shorthand for Size{size, size}.
99+
func SzU[T Number](size T) Size[T] {
100+
return Size[T]{size, size}
101+
}

vector.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,20 @@ func (v Vector[T]) String() string {
182182
return fmt.Sprintf("⟨%s,%s⟩", String(v.X), String(v.Y))
183183
}
184184

185+
// VectorFromAngle is shorthand for Vec(1,0).Rotate(angle)
186+
func VectorFromAngle[T Number](angle float64, length T) Vector[T] {
187+
sin, cos := math.Sincos(angle)
188+
189+
return Vector[T]{Cast[T](float64(length) * cos), Cast[T](float64(length) * sin)}
190+
}
191+
185192
// ZeroVector creates a new Vector with zero values (0,0).
186193
func ZeroVector[T Number]() Vector[T] {
187194
return Vector[T]{}
188195
}
189196

190-
// IdentityVector creates a new Vector with identity values (+1,+1).
191-
func IdentityVector[T Number]() Vector[T] {
197+
// OneVector creates a new Vector with identity values (+1,+1).
198+
func OneVector[T Number]() Vector[T] {
192199
return Vector[T]{1, 1}
193200
}
194201

@@ -231,10 +238,3 @@ func DownLeftVector() Vector[float64] {
231238
func DownRightVector() Vector[float64] {
232239
return Vector[float64]{OneOverSqrt2, OneOverSqrt2}
233240
}
234-
235-
// VecFromAngle is shorthand for Vec(1,0).Rotate(angle)
236-
func VecFromAngle[T Number](angle float64, length T) Vector[T] {
237-
sin, cos := math.Sincos(angle)
238-
239-
return Vector[T]{Cast[T](float64(length) * cos), Cast[T](float64(length) * sin)}
240-
}

vector_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestVector_New(t *testing.T) {
1313
testVector(t, Vec[float64](0.16, 204), 0.16, 204.0)
1414

1515
testVector(t, ZeroVector[int](), 0, 0)
16-
testVector(t, IdentityVector[float64](), 1, 1)
16+
testVector(t, OneVector[float64](), 1, 1)
1717
testVector(t, UpVector[float64](), 0, -1)
1818
testVector(t, DownVector[float64](), 0, 1)
1919
testVector(t, RightVector[float64](), 1, 0)

0 commit comments

Comments
 (0)