Skip to content

Commit 665b88f

Browse files
committed
Add type conversion methods for geometry primitives
1 parent debc22a commit 665b88f

File tree

12 files changed

+134
-103
lines changed

12 files changed

+134
-103
lines changed

circle.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ func (c Circle[T]) Contains(point Point[T]) bool {
8181
return c.Center.Subtract(point).Less(c.Radius)
8282
}
8383

84+
// Int converts the circle to a [int] circle.
85+
func (c Circle[T]) Int() Circle[int] {
86+
return Circle[int]{c.Center.Int(), Cast[int](float64(c.Radius))}
87+
}
88+
89+
// Float converts the circle to a [float64] circle.
90+
func (c Circle[T]) Float() Circle[float64] {
91+
return Circle[float64]{c.Center.Float(), float64(c.Radius)}
92+
}
93+
8494
// String returns a string representation of the Circle.
8595
func (c Circle[T]) String() string {
8696
return fmt.Sprintf("C(%s;%s)", c.Center.String(), String(c.Radius))

line.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ func (l Line[T]) IsZero() bool {
6060
return l.Start.IsZero() && l.End.IsZero()
6161
}
6262

63+
// Int converts the line to a [int] line.
64+
func (l Line[T]) Int() Line[int] {
65+
return Line[int]{l.Start.Int(), l.End.Int()}
66+
}
67+
68+
// Float converts the line to a [float64] line.
69+
func (l Line[T]) Float() Line[float64] {
70+
return Line[float64]{l.Start.Float(), l.End.Float()}
71+
}
72+
6373
// String returns a string representation of the Line.
6474
func (l Line[T]) String() string {
6575
return fmt.Sprintf("L(%s;%s)", l.Start.String(), l.End.String())

padding.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,13 @@ func (p Padding[T]) XY() (T, T) {
3939
func (p Padding[T]) Size() Size[T] {
4040
return Size[T]{p.Width(), p.Height()}
4141
}
42+
43+
// Int converts the padding to a [int] padding.
44+
func (p Padding[T]) Int() Padding[int] {
45+
return Padding[int]{Cast[int](float64(p.Top)), Cast[int](float64(p.Right)), Cast[int](float64(p.Bottom)), Cast[int](float64(p.Left))}
46+
}
47+
48+
// Float converts the padding to a [float64] padding.
49+
func (p Padding[T]) Float() Padding[float64] {
50+
return Padding[float64]{float64(p.Top), float64(p.Right), float64(p.Bottom), float64(p.Left)}
51+
}

point.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,21 @@ func (p Point[T]) XY() (T, T) {
9595
return p.X, p.Y
9696
}
9797

98-
// Vector converts the sipointze to a Vector.
98+
// Vector converts the point to a Vector.
9999
func (p Point[T]) Vector() Vector[T] {
100100
return Vector[T]{p.X, p.Y}
101101
}
102102

103+
// Int converts the point to a [int] point.
104+
func (p Point[T]) Int() Point[int] {
105+
return Point[int]{Cast[int](float64(p.X)), Cast[int](float64(p.Y))}
106+
}
107+
108+
// Float converts the point to a [float64] point.
109+
func (p Point[T]) Float() Point[float64] {
110+
return Point[float64]{float64(p.X), float64(p.Y)}
111+
}
112+
103113
// String returns a string representing the point.
104114
func (p Point[T]) String() string {
105115
return fmt.Sprintf("(%s,%s)", String(p.X), String(p.Y))

polygon.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,13 @@ func (p Polygon[T]) ScaleXY(factorX, factorY float64) Polygon[T] {
5757
func (p Polygon[T]) Empty() bool {
5858
return len(p.Vertices) == 0
5959
}
60+
61+
// Int converts the polygon to a [int] polygon.
62+
func (p Polygon[T]) Int() Polygon[int] {
63+
return Polygon[int]{slices.Map(p.Vertices, (Point[T]).Int)}
64+
}
65+
66+
// Float converts the polygon to a [float64] polygon.
67+
func (p Polygon[T]) Float() Polygon[float64] {
68+
return Polygon[float64]{slices.Map(p.Vertices, (Point[T]).Float)}
69+
}

rectangle.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,21 @@ func (r Rectangle[T]) Contains(point Point[T]) bool {
187187
return minPoint.X <= point.X && point.X <= maxPoint.X && minPoint.Y <= point.Y && point.Y <= maxPoint.Y
188188
}
189189

190-
// ToPolygon converts the rectangle into a generic Polygon with computed vertices.
191-
func (r Rectangle[T]) ToPolygon() Polygon[T] {
190+
// Polygon converts the rectangle into a generic Polygon with computed vertices.
191+
func (r Rectangle[T]) Polygon() Polygon[T] {
192192
return Polygon[T]{r.Vertices()}
193193
}
194194

195+
// Int converts the rectangle to a [int] rectangle.
196+
func (r Rectangle[T]) Int() Rectangle[int] {
197+
return Rectangle[int]{r.Center.Int(), r.Size.Int()}
198+
}
199+
200+
// Float converts the rectangle to a [float64] rectangle.
201+
func (r Rectangle[T]) Float() Rectangle[float64] {
202+
return Rectangle[float64]{r.Center.Float(), r.Size.Float()}
203+
}
204+
195205
// String returns a string representation of the Rectangle using min and max.
196206
func (r Rectangle[T]) String() string {
197207
return fmt.Sprintf("%s-%s", r.Min().String(), r.Max().String())

rectangle_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func TestRectangle_Contains(t *testing.T) {
137137

138138
func TestRectangle_ToPolygon(t *testing.T) {
139139
r := Rect(Pt(0, 0), Sz(2, 2))
140-
p := r.ToPolygon()
140+
p := r.Polygon()
141141

142142
assert.Equal(t, p.Vertices, r.Vertices())
143143
assert.NotSame(t, p.Vertices, r.Vertices())

regular_polygon.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ func (rp RegularPolygon[T]) Empty() bool {
8484
return rp.N == 0
8585
}
8686

87+
// Int converts the regular polygon to a [int] regular polygon.
88+
func (rp RegularPolygon[T]) Int() RegularPolygon[int] {
89+
return RegularPolygon[int]{rp.Center.Int(), rp.Size.Int(), rp.N, rp.Angle}
90+
}
91+
92+
// Float converts the regular polygon to a [float64] regular polygon.
93+
func (rp RegularPolygon[T]) Float() RegularPolygon[float64] {
94+
return RegularPolygon[float64]{rp.Center.Float(), rp.Size.Float(), rp.N, rp.Angle}
95+
}
96+
8797
type Orientation int
8898

8999
const (

size.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ func (s Size[T]) Vector() Vector[T] {
8585
return Vector[T]{s.Width, s.Height}
8686
}
8787

88+
// Int converts the size to a [int] size.
89+
func (s Size[T]) Int() Size[int] {
90+
return Size[int]{Cast[int](float64(s.Width)), Cast[int](float64(s.Height))}
91+
}
92+
93+
// Float converts the size to a [float64] size.
94+
func (s Size[T]) Float() Size[float64] {
95+
return Size[float64]{float64(s.Width), float64(s.Height)}
96+
}
97+
8898
// String returns a string in the form "WxH" using the underlying number formatting.
8999
func (s Size[T]) String() string {
90100
return fmt.Sprintf("%sx%s", String(s.Width), String(s.Height))

types/floats/types.go

Lines changed: 20 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package floats
33

44
import (
55
geom "github.com/gravitton/geometry"
6-
"github.com/gravitton/x/slices"
76
)
87

98
type Point = geom.Point[float64]
@@ -16,75 +15,47 @@ type Polygon = geom.Polygon[float64]
1615
type RegularPolygon = geom.RegularPolygon[float64]
1716
type Padding = geom.Padding[float64]
1817

18+
// Pt is shorthand for geom.Pt(x, y).Float()
1919
func Pt[T geom.Number](x, y T) Point {
20-
return Point{X: float64(x), Y: float64(y)}
21-
}
22-
23-
func ToPoint[T geom.Number](point geom.Point[T]) Point {
24-
return Pt(point.X, point.Y)
20+
return geom.Pt(x, y).Float()
2521
}
2622

23+
// Vec is shorthand for geom.Vec(x, y).Float()
2724
func Vec[T geom.Number](x, y T) Vector {
28-
return Vector{X: float64(x), Y: float64(y)}
29-
}
30-
31-
func ToVector[T geom.Number](vector geom.Vector[T]) Vector {
32-
return Vec(vector.X, vector.Y)
25+
return geom.Vec(x, y).Float()
3326
}
3427

28+
// Sz is shorthand for geom.Sz(width, height).Float()
3529
func Sz[T geom.Number](width, height T) Size {
36-
return Size{Width: float64(width), Height: float64(height)}
37-
}
38-
39-
func ToSize[T geom.Number](size geom.Size[T]) Size {
40-
return Sz(size.Width, size.Height)
30+
return geom.Sz(width, height).Float()
4131
}
4232

33+
// Circ is shorthand for geom.Circ(center, radius).Float()
4334
func Circ[T geom.Number](center geom.Point[T], radius T) Circle {
44-
return Circle{Center: ToPoint(center), Radius: float64(radius)}
45-
}
46-
47-
func ToCircle[T geom.Number](circle geom.Circle[T]) Circle {
48-
return Circ(circle.Center, circle.Radius)
35+
return geom.Circ(center, radius).Float()
4936
}
5037

38+
// Ln is shorthand for geom.Ln(start, end).Float()
5139
func Ln[T geom.Number](start, end geom.Point[T]) Line {
52-
return Line{Start: ToPoint(start), End: ToPoint(end)}
40+
return geom.Ln(start, end).Float()
5341
}
5442

55-
func ToLine[T geom.Number](line geom.Line[T]) Line {
56-
return Ln(line.Start, line.End)
57-
}
43+
// Rect is shorthand for geom.Rect(center, size).Float()
5844
func Rect[T geom.Number](center geom.Point[T], size geom.Size[T]) Rectangle {
59-
return Rectangle{Center: ToPoint(center), Size: ToSize(size)}
45+
return geom.Rect(center, size).Float()
6046
}
6147

62-
func ToRectangle[T geom.Number](rectangle geom.Rectangle[T]) Rectangle {
63-
return Rect(rectangle.Center, rectangle.Size)
64-
}
65-
66-
func Pol[T geom.Number](Vertices []geom.Point[T]) Polygon {
67-
return Polygon{Vertices: slices.Map(Vertices, func(point geom.Point[T]) Point {
68-
return ToPoint(point)
69-
})}
70-
}
71-
72-
func ToPolygon[T geom.Number](polygon geom.Polygon[T]) Polygon {
73-
return Pol(polygon.Vertices)
48+
// Pol is shorthand for geom.Pol(vertices).Float()
49+
func Pol[T geom.Number](vertices []geom.Point[T]) Polygon {
50+
return geom.Pol(vertices).Float()
7451
}
7552

53+
// RegPol is shorthand for geom.RegPol(center, size, n, angle).Float()
7654
func RegPol[T geom.Number](center geom.Point[T], size geom.Size[T], n int, angle float64) RegularPolygon {
77-
return RegularPolygon{Center: ToPoint(center), Size: ToSize(size), N: n, Angle: angle}
78-
}
79-
80-
func ToRegularPolygon[T geom.Number](polygon geom.RegularPolygon[T]) RegularPolygon {
81-
return RegPol(polygon.Center, polygon.Size, polygon.N, polygon.Angle)
82-
}
83-
84-
func Pd[T geom.Number](top, right, bottom, left T) Padding {
85-
return Padding{Top: float64(top), Right: float64(right), Bottom: float64(bottom), Left: float64(left)}
55+
return geom.RegPol(center, size, n, angle).Float()
8656
}
8757

88-
func ToPadding[T geom.Number](padding geom.Padding[T]) Padding {
89-
return Pd(padding.Top, padding.Right, padding.Bottom, padding.Left)
58+
// Pad is shorthand for geom.Pad(top, right, bottom, left).Float()
59+
func Pad[T geom.Number](top, right, bottom, left T) Padding {
60+
return geom.Pad(top, right, bottom, left).Float()
9061
}

0 commit comments

Comments
 (0)