From 0f44030e535c23c9f87de5f856e73ee49f9a7c75 Mon Sep 17 00:00:00 2001 From: Derek McQuay Date: Sat, 27 Aug 2016 22:28:15 -0700 Subject: [PATCH] updated the readme and cmd/recty/main.go added examples to the readme and updated main.go to give an example of who one could use the rect package --- README.md | 86 ++++++++++++++++++++++++++++++++++++++++++++++- cmd/recty/main.go | 55 ++++++++++++++++++++++++------ 2 files changed, 129 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 8aa08f9..ae8732b 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,87 @@ # rect -apprenda take home assessment \ No newline at end of file +[![GoDoc](https://godoc.org/s.mcquay.me/dm/rect?status.svg)](https://godoc.org/s.mcquay.me/dm/rect) +[![Go Report Card](https://goreportcard.com/badge/s.mcquay.me/dm/rect)](https://goreportcard.com/report/s.mcquay.me/dm/rect) + +Package rect implements rectangles and can determine when rectangles +intersect, are contained, and when rectangles lie adjacent + +## usage + +in cmd/recty/main.go is an example of how one could use this package. + +``` go +r := rect.Rectangle{ + P1: rect.Point{1, 1}, + P2: rect.Point{1, 2}, + P3: rect.Point{2, 1}, + P4: rect.Point{2, 2}, +} +``` + +defines a rectangle. + +``` go +fmt.Println(r.IsRect()) +``` + +would varify that the rectangle provided is a valid rectangle. + +``` go +r1 := rect.Rectangle{ + P1: rect.Point{1, 1}, + P2: rect.Point{1, 2}, + P3: rect.Point{2, 1}, + P4: rect.Point{2, 2}, +} +r2 := rect.Rectangle{ + P1: rect.Point{1, 1}, + P2: rect.Point{1, 2}, + P3: rect.Point{2, 1}, + P4: rect.Point{2, 2}, +} + +fmt.Println(rect.Intersection(r1, r2)) +``` + +Running the above code would print out all intersection points of the two +rectangles. + +``` go +r3 := rect.Rectangle{ + P1: rect.Point{0, 0}, + P2: rect.Point{0, 3}, + P3: rect.Point{3, 0}, + P4: rect.Point{3, 3}, +} +r4 := rect.Rectangle{ + P1: rect.Point{2, 2}, + P2: rect.Point{2, 3}, + P3: rect.Point{3, 2}, + P4: rect.Point{3, 3}, +} + +fmt.Println(rect.Adjacency(r3, r4)) +``` + +This would return a boolean for whether r3 and r4 have an adjacent side. + +``` go +r5 := rect.Rectangle{ + P1: rect.Point{0, 0}, + P2: rect.Point{0, 3}, + P3: rect.Point{3, 0}, + P4: rect.Point{3, 3}, +} +r6 := rect.Rectangle{ + P1: rect.Point{1, 1}, + P2: rect.Point{1, 2}, + P3: rect.Point{2, 1}, + P4: rect.Point{2, 2}, +} + +fmt.Println(rect.Containment(r5, r6)) +``` + +And finally, running `rect.Containment(r5, r6)` would return true because r6 +is contained inside of r5 diff --git a/cmd/recty/main.go b/cmd/recty/main.go index 66696ee..9db47a9 100644 --- a/cmd/recty/main.go +++ b/cmd/recty/main.go @@ -13,19 +13,52 @@ func main() { P3: rect.Point{2, 1}, P4: rect.Point{2, 2}, } + fmt.Println(r.IsRect()) + r1 := rect.Rectangle{ - P1: rect.Point{0, 0}, - P2: rect.Point{0, 1}, - P3: rect.Point{1, 0}, - P4: rect.Point{1, 1}, + P1: rect.Point{1, 1}, + P2: rect.Point{1, 2}, + P3: rect.Point{2, 1}, + P4: rect.Point{2, 2}, } - fmt.Println(r1.IsRect()) - r1 := rect.Rectangle{ - P1: rect.Point{0, 0}, - P2: rect.Point{0, 1}, - P3: rect.Point{1, 0}, - P4: rect.Point{1, 1}, + r2 := rect.Rectangle{ + P1: rect.Point{2, 2}, + P2: rect.Point{2, 3}, + P3: rect.Point{3, 2}, + P4: rect.Point{3, 3}, } - fmt.Println(r1.IsRect()) + + fmt.Println(rect.Intersection(r1, r2)) + + r3 := rect.Rectangle{ + P1: rect.Point{0, 0}, + P2: rect.Point{0, 3}, + P3: rect.Point{3, 0}, + P4: rect.Point{3, 3}, + } + r4 := rect.Rectangle{ + P1: rect.Point{2, 2}, + P2: rect.Point{2, 3}, + P3: rect.Point{3, 2}, + P4: rect.Point{3, 3}, + } + + fmt.Println(rect.Adjacency(r3, r4)) + + r5 := rect.Rectangle{ + P1: rect.Point{0, 0}, + P2: rect.Point{0, 3}, + P3: rect.Point{3, 0}, + P4: rect.Point{3, 3}, + } + r6 := rect.Rectangle{ + P1: rect.Point{1, 1}, + P2: rect.Point{1, 2}, + P3: rect.Point{2, 1}, + P4: rect.Point{2, 2}, + } + + fmt.Println(rect.Containment(r5, r6)) + }