Skip to content

Commit 83587a8

Browse files
committed
Standardize Zero method naming to IsZero across all types, add Empty, Bounds, and Equal
1 parent 95c193c commit 83587a8

File tree

12 files changed

+100
-39
lines changed

12 files changed

+100
-39
lines changed

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (p Point[T]) AngleTo(point Point[T]) float64
6565

6666
// Utilities
6767
func (p Point[T]) Equal(point Point[T]) bool
68-
func (p Point[T]) Zero() bool
68+
func (p Point[T]) IsZero() bool
6969
```
7070

7171
### Vector
@@ -105,7 +105,7 @@ func (v Vector[T]) Abs() Vector[T]
105105

106106
// Utilities
107107
func (v Vector[T]) Equal(vector Vector[T]) bool
108-
func (v Vector[T]) Zero() bool
108+
func (v Vector[T]) IsZero() bool
109109
func (v Vector[T]) Unit() bool
110110
func (v Vector[T]) Less(value T) bool
111111
func (v Vector[T]) String() string
@@ -134,7 +134,7 @@ func (s Size[T]) ShrinkXY(amountX, amountY T) Size[T]
134134

135135
// Utilities
136136
func (s Size[T]) Equal(other Size[T]) bool
137-
func (s Size[T]) Zero() bool
137+
func (s Size[T]) IsZero() bool
138138
func (s Size[T]) String() string
139139
```
140140

@@ -150,7 +150,6 @@ type Circle[T Number] struct {
150150
func (c Circle[T]) Area() float64
151151
func (c Circle[T]) Circumference() float64
152152
func (c Circle[T]) Diameter() T
153-
func (c Circle[T]) Bounds() Rectangle[T]
154153

155154
// Transformations
156155
func (c Circle[T]) Translate(vector Vector[T]) Circle[T]
@@ -167,6 +166,8 @@ func (c Circle[T]) Contains(point Point[T]) bool
167166

168167
// Utilities
169168
func (c Circle[T]) Equal(circle Circle[T]) bool
169+
func (c Circle[T]) IsZero() bool
170+
func (c Circle[T]) Bounds() Rectangle[T]
170171
func (c Circle[T]) String() string
171172
```
172173

@@ -211,6 +212,8 @@ func (r Rectangle[T]) Contains(point Point[T]) bool
211212

212213
// Utilities
213214
func (r Rectangle[T]) Equal(other Rectangle[T]) bool
215+
func (r Rectangle[T]) IsZero() bool
216+
func (r Rectangle[T]) Bounds() Rectangle[T]
214217
func (r Rectangle[T]) ToPolygon() Polygon[T]
215218
func (r Rectangle[T]) String() string
216219
```
@@ -235,6 +238,8 @@ func (l Line[T]) Length() float64
235238

236239
// Utilities
237240
func (l Line[T]) Equal(other Line[T]) bool
241+
func (l Line[T]) IsZero() bool
242+
func (l Line[T]) Bounds() Rectangle[T]
238243
func (l Line[T]) String() string
239244
```
240245

@@ -253,6 +258,9 @@ func (p Polygon[T]) Translate(vector Vector[T]) Polygon[T]
253258
func (p Polygon[T]) MoveTo(center Point[T]) Polygon[T]
254259
func (p Polygon[T]) Scale(factor float64) Polygon[T]
255260
func (p Polygon[T]) ScaleXY(factorX, factorY float64) Polygon[T]
261+
262+
// Utilities
263+
func (p Polygon[T]) Empty() bool
256264
```
257265

258266
### Regular Polygon
@@ -274,6 +282,10 @@ func (rp RegularPolygon[T]) Scale(factor float64) RegularPolygon[T]
274282
func (rp RegularPolygon[T]) ScaleXY(factorX, factorY float64) RegularPolygon[T]
275283

276284
// Utilities
285+
func (rp RegularPolygon[T]) Equal(polygon RegularPolygon[T]) bool
286+
func (rp RegularPolygon[T]) IsZero() bool
287+
func (rp RegularPolygon[T]) Empty() bool
288+
func (rp RegularPolygon[T]) Bounds() Rectangle[T]
277289
func (rp RegularPolygon[T]) ToPolygon() Polygon[T]
278290
```
279291

circle.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ func (c Circle[T]) Equal(circle Circle[T]) bool {
7171
return c.Center.Equal(circle.Center) && Equal(c.Radius, circle.Radius)
7272
}
7373

74+
// IsZero checks if center point and radius are zero.
75+
func (c Circle[T]) IsZero() bool {
76+
return c.Center.IsZero() && Equal(c.Radius, 0)
77+
}
78+
7479
// Contains checks if the given point lies inside the circle.
7580
func (c Circle[T]) Contains(point Point[T]) bool {
7681
return c.Center.Subtract(point).Less(c.Radius)

line.go

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

3-
import "fmt"
3+
import (
4+
"fmt"
5+
)
46

57
// Line is a 2D line.
68
type Line[T Number] struct {
@@ -43,11 +45,21 @@ func (l Line[T]) Length() float64 {
4345
return l.Direction().Length()
4446
}
4547

48+
// Bounds returns the axis-aligned bounding rectangle.
49+
func (l Line[T]) Bounds() Rectangle[T] {
50+
return Rectangle[T]{Center: l.Midpoint(), Size: S(l.Direction().Abs().XY())}
51+
}
52+
4653
// Equal checks if the start and end points of the lines are equal.
4754
func (l Line[T]) Equal(other Line[T]) bool {
4855
return l.Start.Equal(other.Start) && l.End.Equal(other.End)
4956
}
5057

58+
// IsZero checks if start and end points are zero.
59+
func (l Line[T]) IsZero() bool {
60+
return l.Start.IsZero() && l.End.IsZero()
61+
}
62+
5163
// String returns a string representation of the Line.
5264
func (l Line[T]) String() string {
5365
return fmt.Sprintf("L(%s;%s)", l.Start.String(), l.End.String())

point.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ func (p Point[T]) Equal(point Point[T]) bool {
8080
return Equal(p.X, point.X) && Equal(p.Y, point.Y)
8181
}
8282

83-
// Zero checks if X and Y values are 0.
84-
func (p Point[T]) Zero() bool {
83+
// IsZero checks if X and Y values are 0.
84+
func (p Point[T]) IsZero() bool {
8585
return p.Equal(Point[T]{})
8686
}
8787

point_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ func TestPoint_Equal(t *testing.T) {
7575
assert.True(t, P(0.4, -0.25).Equal(P(0.4, -0.250001)))
7676
}
7777

78-
func TestPoint_Zero(t *testing.T) {
79-
assert.False(t, P(1, 2).Zero())
80-
assert.True(t, P(0, -0).Zero())
81-
assert.True(t, ZeroPoint[int]().Zero())
82-
83-
assert.False(t, P(0.4, -0.25).Zero())
84-
assert.True(t, P(0.0, 0.0).Zero())
85-
assert.True(t, P(0.0, 0.000001).Zero())
86-
assert.True(t, ZeroPoint[float64]().Zero())
78+
func TestPoint_IsZero(t *testing.T) {
79+
assert.False(t, P(1, 2).IsZero())
80+
assert.True(t, P(0, -0).IsZero())
81+
assert.True(t, ZeroPoint[int]().IsZero())
82+
83+
assert.False(t, P(0.4, -0.25).IsZero())
84+
assert.True(t, P(0.0, 0.0).IsZero())
85+
assert.True(t, P(0.0, 0.000001).IsZero())
86+
assert.True(t, ZeroPoint[float64]().IsZero())
8787
}
8888

8989
func TestPoint_XY(t *testing.T) {

polygon.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,8 @@ func (p Polygon[T]) ScaleXY(factorX, factorY float64) Polygon[T] {
4343
return center.Add(point.Subtract(center).MultiplyXY(factorX, factorY))
4444
})}
4545
}
46+
47+
// Empty checks if number of vertices is zero.
48+
func (p Polygon[T]) Empty() bool {
49+
return len(p.Vertices) == 0
50+
}

rectangle.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ func RectangleFromMinMax[T Number](min, max Point[T]) Rectangle[T] {
2323
return RectangleFromMin(min, S(max.Subtract(min).XY()))
2424
}
2525

26-
// RectangleFromSize creates a Rectangle from zero point and size.
27-
func RectangleFromSize[T Number](size Size[T]) Rectangle[T] {
28-
return Rectangle[T]{P[T](0, 0), size}
29-
}
30-
3126
// Translate creates a new Rectangle translated by the given vector.
3227
func (r Rectangle[T]) Translate(vector Vector[T]) Rectangle[T] {
3328
return Rectangle[T]{r.Center.Add(vector), r.Size}
@@ -147,11 +142,21 @@ func (r Rectangle[T]) AspectRatio() float64 {
147142
return r.Size.AspectRatio()
148143
}
149144

145+
// Bounds returns the axis-aligned bounding rectangle.
146+
func (r Rectangle[T]) Bounds() Rectangle[T] {
147+
return r
148+
}
149+
150150
// Equal checks for equal center and size values using tolerant numeric comparison.
151151
func (r Rectangle[T]) Equal(other Rectangle[T]) bool {
152152
return r.Center.Equal(other.Center) && r.Size.Equal(other.Size)
153153
}
154154

155+
// IsZero checks if center point and size are zero.
156+
func (r Rectangle[T]) IsZero() bool {
157+
return r.Center.IsZero() && r.Size.IsZero()
158+
}
159+
155160
// Contains reports whether the given point lies within or on the rectangle bounds.
156161
func (r Rectangle[T]) Contains(point Point[T]) bool {
157162
minPoint, maxPoint := r.Min(), r.Max()

regular_polygon.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,27 @@ func (rp RegularPolygon[T]) Vertices() []Point[T] {
6464
return vertices
6565
}
6666

67+
// Bounds returns the axis-aligned bounding rectangle.
68+
func (rp RegularPolygon[T]) Bounds() Rectangle[T] {
69+
return Rectangle[T]{rp.Center, rp.Size}
70+
}
71+
6772
// ToPolygon converts the regular polygon into a generic Polygon with computed vertices.
6873
func (rp RegularPolygon[T]) ToPolygon() Polygon[T] {
6974
return Polygon[T]{rp.Vertices()}
7075
}
76+
77+
// Equal checks if center point, size and number of vertices are equal.
78+
func (rp RegularPolygon[T]) Equal(polygon RegularPolygon[T]) bool {
79+
return rp.Center.Equal(polygon.Center) && rp.Size.Equal(polygon.Size) && rp.N == polygon.N
80+
}
81+
82+
// IsZero checks if center point, size and number of vertices are zero.
83+
func (rp RegularPolygon[T]) IsZero() bool {
84+
return rp.Center.IsZero() && rp.Size.IsZero() && rp.N == 0
85+
}
86+
87+
// Empty checks if number of vertices is zero.
88+
func (rp RegularPolygon[T]) Empty() bool {
89+
return rp.N == 0
90+
}

size.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package geom
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
)
46

57
// Size is a 2D size.
68
type Size[T Number] struct {
@@ -63,8 +65,8 @@ func (s Size[T]) Equal(other Size[T]) bool {
6365
return Equal(s.Width, other.Width) && Equal(s.Height, other.Height)
6466
}
6567

66-
// Zero checks if width and height values are 0.
67-
func (s Size[T]) Zero() bool {
68+
// IsZero checks if width and height values are 0.
69+
func (s Size[T]) IsZero() bool {
6870
return s.Equal(Size[T]{})
6971
}
7072

size_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ func TestSize_Equal(t *testing.T) {
5656
assert.True(t, S(0.4, -0.25).Equal(S(0.4, -0.250001)))
5757
}
5858

59-
func TestSize_Zero(t *testing.T) {
60-
assert.False(t, S(1, 2).Zero())
61-
assert.True(t, S(0, 0).Zero())
62-
assert.True(t, S[float64](0.0, 0.000001).Zero())
59+
func TestSize_IsZero(t *testing.T) {
60+
assert.False(t, S(1, 2).IsZero())
61+
assert.True(t, S(0, 0).IsZero())
62+
assert.True(t, S[float64](0.0, 0.000001).IsZero())
6363
}
6464

6565
func TestSize_XY(t *testing.T) {

0 commit comments

Comments
 (0)