Skip to content

Commit

Permalink
Merge pull request #5 from mrsombre/tech-rect
Browse files Browse the repository at this point in the history
Tech rect
  • Loading branch information
mrsombre authored Jan 11, 2024
2 parents 289a360 + f687a64 commit 4a38b6b
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 55 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ Currently, the framework is written in Golang 1.19.x as it is closer to 1.18.x a
```shell
go test -cover ./...
```

### Benchmarks

```shell
go test -bench=. -benchmem -run=^$ > bench.out
```
11 changes: 11 additions & 0 deletions all_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

// Benchmarks

var (
GlobalB bool
GlobalI int
GlobalF float64

GlobalPoint Point
)
9 changes: 9 additions & 0 deletions bench.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
goos: linux
goarch: amd64
pkg: github.com/mrsombre/codingame-framework
cpu: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
BenchmarkLine_IsCollision-8 271175239 4.403 ns/op 0 B/op 0 allocs/op
BenchmarkLine_LinesIntersection-8 351207843 3.375 ns/op 0 B/op 0 allocs/op
BenchmarkLine_IsPointOnLine-8 515632399 2.412 ns/op 0 B/op 0 allocs/op
PASS
ok github.com/mrsombre/codingame-framework 4.666s
20 changes: 12 additions & 8 deletions geometry_line_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,6 @@ func TestLine_IsPointOnLine(t *testing.T) {
}
}

func BenchmarkLine_IsPointOnLine(b *testing.B) {
line := Line{Point{0, 0}, Point{300, 300}}
point := Point{150, 150}
for i := 0; i < b.N; i++ {
line.IsPointOnLine(point)
}
}

func TestLine_IsPointOnSegment(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -601,3 +593,15 @@ func TestNewLine(t *testing.T) {
ln := NewLine(Point{0, 0}, Point{300, 400})
assert.Equal(t, Line{Point{0, 0}, Point{300, 400}}, ln)
}

// Benchmarks

func BenchmarkLine_IsCollision(b *testing.B) {
r := false
al := Line{Point{0, 0}, Point{600, 800}}
bl := Line{Point{300, 400}, Point{150, 200}}
for i := 0; i < b.N; i++ {
r = al.IsCollision(bl, 50)
}
GlobalB = r
}
6 changes: 6 additions & 0 deletions geometry_point.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ const (
topRight = 5
bottomRight = 6
bottomLeft = 7

// 0-corners
topLeft0 = 0
topRight0 = 1
bottomRight0 = 2
bottomLeft0 = 3
)

// Point represents a point in a 2D plane.
Expand Down
6 changes: 6 additions & 0 deletions geometry_point_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ func TestPoint_Index(t *testing.T) {
width: 10,
want: 0,
},
{
name: `last in row`,
p: Point{9, 0},
width: 10,
want: 9,
},
{
name: `next row`,
p: Point{0, 1},
Expand Down
52 changes: 52 additions & 0 deletions geometry_rect.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ func (r Rect) Center() Point {
)
}

func (r Rect) Symmetric(width, height float64) Rect {
return Rect{
Xf: width - r.Xf,
Xt: width - r.Xt,
Yf: height - r.Yf,
Yt: height - r.Yt,
}
}

// IsContainsPoint tests if the Rect contains the Point.
func (r Rect) IsContainsPoint(c Point) bool {
return c.X >= r.Xf && c.X <= r.Xt && c.Y >= r.Yf && c.Y <= r.Yt
Expand All @@ -48,6 +57,49 @@ func (r Rect) IsContainsRectangle(t Rect) bool {
return r.Xf <= t.Xf && r.Xt >= t.Xt && r.Yf <= t.Yf && r.Yt >= t.Yt
}

// IsIntersectsRect tests if the Rect intersects the other Rect.
func (r Rect) IsIntersectsRect(t Rect) bool {
return !(r.Xt < t.Xf || r.Xf > t.Xt || r.Yt < t.Yf || r.Yf > t.Yt)
}

// RectsIntersection returns the intersection Rect of two Rects.
func (r Rect) RectsIntersection(t Rect) (Rect, bool) {
if !r.IsIntersectsRect(t) {
return Rect{}, false
}

ir := NewRectangle(
math.Max(r.Xf, t.Xf),
math.Min(r.Xt, t.Xt),
math.Max(r.Yf, t.Yf),
math.Min(r.Yt, t.Yt),
)
if ir.Width() == 0 || ir.Height() == 0 {
return Rect{}, false
}

return ir, true
}

func (r Rect) Vertices() Points {
return Points{
topLeft0: {r.Xf, r.Yt},
topRight0: {r.Xt, r.Yt},
bottomRight0: {r.Xt, r.Yf},
bottomLeft0: {r.Xf, r.Yf},
}
}

// Edges returns the Lines edges of the Rect.
func (r Rect) Edges() Lines {
return Lines{
top: {Point{r.Xf, r.Yt}, Point{r.Xt, r.Yt}},
right: {Point{r.Xt, r.Yf}, Point{r.Xt, r.Yt}},
bottom: {Point{r.Xf, r.Yf}, Point{r.Xt, r.Yf}},
left: {Point{r.Xf, r.Yf}, Point{r.Xf, r.Yt}},
}
}

func (r Rect) String() string {
return fmt.Sprintf("[X:%.f>%.f,Y:%.f>%.f]", r.Xf, r.Xt, r.Yf, r.Yt)
}
Expand Down
Loading

0 comments on commit 4a38b6b

Please sign in to comment.