Skip to content

Commit 62196d8

Browse files
committed
Update docs
1 parent 98c8a40 commit 62196d8

File tree

4 files changed

+198
-10
lines changed

4 files changed

+198
-10
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1010
### Added
1111
- `Point`
1212
- `Vector`
13+
- `Size`
14+
- `Line`
15+
- `Circle`
16+
- `Rectangle`
17+
- `Polygon`
18+
- `RegularPolygon`

README.md

Lines changed: 187 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
[![Latest Stable Version][ico-release]][link-release]
44
[![Build Status][ico-workflow]][link-workflow]
55
[![Coverage Status][ico-coverage]][link-coverage]
6-
[![Quality Score][ico-code-quality]][link-code-quality]
76
[![Go Report Card][ico-go-report-card]][link-go-report-card]
87
[![Go Dev Reference][ico-go-dev-reference]][link-go-dev-reference]
98
[![Software License][ico-license]][link-licence]
@@ -28,6 +27,191 @@ import (
2827
)
2928
```
3029

30+
## API
31+
32+
All types and methods are generic and can be used with any numeric type.
33+
34+
```go
35+
type Number interface {
36+
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~float32 | ~float64
37+
}
38+
```
39+
40+
### Point
41+
42+
```go
43+
type Point[T Number] struct{
44+
X, Y T
45+
}
46+
47+
// Properties
48+
func (p Point[T]) XY() (T, T)
49+
50+
// Mathematical operations
51+
func (p Point[T]) Add(vector Vector[T]) Point[T
52+
func (p Point[T]) AddXY(deltaX, deltaY T) Point[T
53+
func (p Point[T]) Subtract(other Point[T]) Vector[T]
54+
func (p Point[T]) Multiply(scale float64) Point[T]
55+
func (p Point[T]) MultiplyXY(scaleX, scaleY float64) Point[T]
56+
func (p Point[T]) Divide(scale float64) Point[T]
57+
func (p Point[T]) DivideXY(scaleX, scaleY float64) Point[T]
58+
59+
// Geometric operations
60+
func (p Point[T]) DistanceTo(point Point[T]) float64
61+
func (p Point[T]) DistanceSquaredTo(point Point[T]) T
62+
func (p Point[T]) Midpoint(point Point[T]) Point[T]
63+
func (p Point[T]) Lerp(point Point[T], t float64) Point[T]
64+
func (p Point[T]) AngleTo(point Point[T]) float64
65+
66+
// Utilities
67+
func (p Point[T]) Equal(point Point[T]) bool
68+
func (p Point[T]) Zero() bool
69+
```
70+
71+
### Vector
72+
73+
```go
74+
type Vector[T Number] struct {
75+
X, Y T
76+
}
77+
78+
// Properties
79+
func (v Vector[T]) XY() (T, T)
80+
81+
// Mathematical operations
82+
func (v Vector[T]) Add(vector Vector[T]) Vector[T]
83+
func (v Vector[T]) AddXY(deltaX, deltaY T) Vector[T]
84+
func (v Vector[T]) Sub(vector Vector[T]) Vector[T]
85+
func (v Vector[T]) SubXY(deltaX, deltaY T) Vector[T]
86+
func (v Vector[T]) Multiply(scale float64) Vector[T]
87+
func (v Vector[T]) MultiplyXY(scaleX, scaleY float64) Vector[T]
88+
func (v Vector[T]) Divide(scale float64) Vector[T]
89+
func (v Vector[T]) DivideXY(scaleX, scaleY float64) Vector[T]
90+
func (v Vector[T]) Negate() Vector[T]
91+
92+
// Vector operations
93+
func (v Vector) Dot(vector Vector[T]) float64
94+
func (v Vector) Cross(vector Vector[T]) float64
95+
func (v Vector) Length() float64
96+
func (v Vector) LengthSquared() T
97+
func (v Vector) Normalize() Vector[T]
98+
func (v Vector) Angle() float64
99+
100+
// Transformations
101+
func (v Vector) Rotate(angle float64) Vector[T]
102+
func (v Vector) Normal() Vector[T]
103+
104+
// Utilities
105+
func (v Vector) Equal(vector Vector) bool
106+
func (v Vector) Zero() bool
107+
```
108+
109+
### Size
110+
111+
```go
112+
type Size[T Number] struct {
113+
Width, Height T
114+
}
115+
116+
// Properties
117+
func (s Size[T]) XY() (T, T)
118+
func (s Size[T]) Area() float64
119+
func (s Size[T]) Perimeter() T
120+
func (s Size[T]) AspectRatio() float64
121+
122+
123+
// Dimension operations
124+
func (s Size[T]) Scaled(factor float64) Size[T]
125+
func (s Size[T]) ScaledXY(scaleX, scaleY float64) Size[T]
126+
func (s Size[T]) Expand(deltaWidth, deltaHeight T) Size[T]
127+
func (s Size[T]) Shrunk(deltaWidth, deltaHeight T) Size [T]
128+
129+
// Utilities
130+
func (s Size) Equal(other Size[T]) bool
131+
func (s Size) Zero() bool
132+
```
133+
134+
### Circle
135+
136+
```go
137+
type Circle[T Number] struct {
138+
Center Point[T]
139+
Radius T
140+
}
141+
142+
// Properties
143+
func (c Circle[T]) Area() float64
144+
func (c Circle[T]) Circumference() float64
145+
func (c Circle[T]) Diameter() T
146+
func (c Circle[T]) Bounds() Rectangle[T]
147+
148+
// Transformations
149+
func (c Circle[T]) Translate(vector Vector) Circle[T]
150+
func (c Circle[T]) MoveTo(center Point) Circle[T]
151+
func (c Circle[T]) Scale(factor float64) Circle[T]
152+
153+
// Size operations
154+
func (c Circle[T]) Resize(radius T) Circle[T]
155+
func (c Circle[T]) Expand(amount T) Circle[T]
156+
func (c Circle[T]) Shrunk(amount T) Circle[T]
157+
158+
// Geometric queries
159+
func (c Circle[T]) Contains(point Point[T]) bool
160+
161+
// Utilities
162+
func (c Circle) Equal(circle Circle) bool
163+
```
164+
165+
### Rectangle
166+
167+
```go
168+
type Rectangle[T Number] struct{
169+
Center Point[T]
170+
Size Size[T]
171+
}
172+
```
173+
174+
### Line
175+
176+
```go
177+
type Line[T Number] struct{
178+
Start Point[T]
179+
End Point[T]
180+
}
181+
```
182+
183+
### Polygon
184+
185+
```go
186+
type Polygon[T Number] struct{
187+
Vertices []Point[T]
188+
}
189+
```
190+
191+
### Regular Polygon
192+
193+
```go
194+
type RegularPolygon[T Number] struct {
195+
Center Point[T]
196+
Size Size[T]
197+
N int
198+
}
199+
```
200+
201+
### Constructor Functions
202+
203+
Short constructors for convenience
204+
205+
```go
206+
func P(x, y T) Point[T]
207+
func V(x, y T) Vector[T]
208+
func S(w, h T) Size[T]
209+
func C(center Point[T], radius T) Circle[T]
210+
func R(center Point[T], size Size[T]) Rectangle[T]
211+
func L(start, end Point[T]) Line[T]
212+
func RP(center Point[T], size Size[T], n int) RegularPolygon[T]
213+
```
214+
31215

32216
## Credits
33217

@@ -45,8 +229,7 @@ The MIT License (MIT). Please see [License File][link-licence] for more informat
45229
[ico-release]: https://img.shields.io/github/v/release/gravitton/geometry?style=flat-square&colorB=blue
46230
[ico-go-dev-reference]: https://img.shields.io/badge/go.dev-reference-blue?style=flat-square
47231
[ico-go-report-card]: https://goreportcard.com/badge/github.com/gravitton/geometry?style=flat-square
48-
[ico-coverage]: https://img.shields.io/scrutinizer/coverage/g/gravitton/geometry/main.svg?style=flat-square
49-
[ico-code-quality]: https://img.shields.io/scrutinizer/g/gravitton/geometry.svg?style=flat-square
232+
[ico-coverage]: https://img.shields.io/coverallsCoverage/github/gravitton/assert?style=flat-square
50233

51234
[link-author]: https://github.com/gravitton
52235
[link-release]: https://github.com/gravitton/geometry/releases
@@ -56,5 +239,4 @@ The MIT License (MIT). Please see [License File][link-licence] for more informat
56239
[link-workflow]: https://github.com/gravitton/geometry/actions
57240
[link-go-dev-reference]: https://pkg.go.dev/github.com/gravitton/geometry
58241
[link-go-report-card]: https://goreportcard.com/report/github.com/gravitton/geometry
59-
[link-coverage]: https://scrutinizer-ci.com/g/gravitton/geometry/code-structure
60-
[link-code-quality]: https://scrutinizer-ci.com/g/gravitton/geometry
242+
[link-coverage]: https://coveralls.io/github/gravitton/geometry

point.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ func (p Point[T]) DistanceTo(point Point[T]) float64 {
5555
return point.Subtract(p).Length()
5656
}
5757

58-
// DistanceToSquared return euclidean distance squared (for faster comparison) from the current point to the given point.
59-
func (p Point[T]) DistanceToSquared(point Point[T]) T {
58+
// DistanceSquaredTo return euclidean distance squared (for faster comparison) from the current point to the given point.
59+
func (p Point[T]) DistanceSquaredTo(point Point[T]) T {
6060
return point.Subtract(p).LengthSquared()
6161
}
6262

point_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ func TestPoint_DistanceTo(t *testing.T) {
5656
assert.EqualDelta(t, P(0.4, -0.25).DistanceTo(P(0.5, -0.35)), math.Sqrt(0.02), Delta)
5757
}
5858

59-
func TestPoint_DistanceToSquared(t *testing.T) {
60-
assert.Equal(t, P(1, 2).DistanceToSquared(P(2, 3)), 2)
61-
assert.EqualDelta(t, P(0.4, -0.25).DistanceToSquared(P(0.5, -0.35)), 0.02, Delta)
59+
func TestPoint_DistanceSquaredTo(t *testing.T) {
60+
assert.Equal(t, P(1, 2).DistanceSquaredTo(P(2, 3)), 2)
61+
assert.EqualDelta(t, P(0.4, -0.25).DistanceSquaredTo(P(0.5, -0.35)), 0.02, Delta)
6262
}
6363

6464
func TestPoint_AngleTo(t *testing.T) {

0 commit comments

Comments
 (0)