Skip to content

Instantly share code, notes, and snippets.

@jasondelponte
Created August 11, 2012 23:12
Show Gist options
  • Save jasondelponte/3327726 to your computer and use it in GitHub Desktop.
Save jasondelponte/3327726 to your computer and use it in GitHub Desktop.
r/dailyprogramer 87 easy
package dp87easy
import (
"math"
)
type Rect struct {
UX, UY int
BX, BY int
}
func (r Rect) Is(r2 Rect) bool {
return (r.UX == r2.UX && r.UY == r2.UY && r.BX == r2.BX && r.BY == r2.BY)
}
func (r Rect) Intersects(r2 Rect) *Rect {
if r.UX > r2.BX || r.BX < r2.UX || r.UY > r2.BY || r.BY < r2.UY {
return nil
}
return &Rect{
UX: int(math.Max(float64(r.UX), float64(r2.UX))),
UY: int(math.Max(float64(r.UY), float64(r2.UY))),
BX: int(math.Min(float64(r.BX), float64(r2.BX))),
BY: int(math.Min(float64(r.BY), float64(r2.BY))),
}
}
package dp87easy
import (
"testing"
)
func TestRectIs(t *testing.T) {
r := Rect{3, 3, 10, 10}
rSame := Rect{3, 3, 10, 10}
rDiff := Rect{6, 6, 12, 12}
if r.Is(rSame) != true {
t.Error("Failed to detect same rectangles", r, rSame)
}
if r.Is(rDiff) != false {
t.Error("Failed to detect different rectangles", r, rDiff)
}
}
func Test87Easy(t *testing.T) {
r := Rect{3, 3, 10, 10}
if i := r.Intersects(Rect{6, 6, 12, 12}); i.Is(Rect{6, 6, 10, 10}) != true {
t.Error("Failed to match: {3, 3, 10, 10}, to {6, 6, 12, 12}, expected intersect: {6, 6, 10, 10}")
}
r = Rect{4, 4, 5, 5}
if i := r.Intersects(Rect{6, 6, 10, 10}); i != nil {
t.Error("Failed to match: {4, 4, 5, 5}, to {6, 6, 10, 10}, expected no intersect")
}
r = Rect{6, 6, 10, 10}
if i := r.Intersects(Rect{6, 6, 10, 10}); i.Is(Rect{6, 6, 10, 10}) != true {
t.Error("Failed to match: {6, 6, 10, 10}, to {6, 6, 10, 10}, expected intersect: {6, 6, 10, 10}")
}
r = Rect{4, 4, 12, 12}
if i := r.Intersects(Rect{6, 6, 10, 10}); i.Is(Rect{6, 6, 10, 10}) != true {
t.Error("Failed to match: {4, 4, 12, 12}, to {6, 6, 10, 10}, expected intersect: {6, 6, 10, 10}")
}
r = Rect{6, 6, 10, 10}
if i := r.Intersects(Rect{4, 4, 12, 12}); i.Is(Rect{6, 6, 10, 10}) != true {
t.Error("Failed to match: {6, 6, 10, 10}, to {4, 4, 12, 12}, expected intersect: {6, 6, 10, 10}")
}
r = Rect{1, 1, 4, 4}
if i := r.Intersects(Rect{5, 5, 8, 8}); i != nil {
t.Error("Failed to match: {1, 1, 4, 4}, to {5, 5, 8, 8}, expected no intersect")
}
if i := r.Intersects(Rect{2, 2, 5, 5}); i.Is(Rect{2, 2, 4, 4}) != true {
t.Error("Failed to match: {1, 1, 4, 4}, to {2, 2, 5, 5}, expected intersect: {2, 2, 4, 4}")
}
if i := r.Intersects(Rect{2, 2, 3, 5}); i.Is(Rect{2, 2, 3, 4}) != true {
t.Error("Failed to match: {1, 1, 4, 4}, to {2, 2, 3, 5}, expected intersect: {2, 2, 3, 4}")
}
if i := r.Intersects(Rect{2, 2, 5, 3}); i.Is(Rect{2, 2, 4, 3}) != true {
t.Error("Failed to match: {1, 1, 4, 4}, to {2, 2, 5, 3}, expected intersect: {2, 2, 4, 3}")
}
if i := r.Intersects(Rect{2, 2, 3, 3}); i.Is(Rect{2, 2, 3, 3}) != true {
t.Error("Failed to match: {1, 1, 4, 4}, to {2, 2, 3, 3}, expected intersect: {2, 2, 3, 3}")
}
if i := r.Intersects(Rect{0, 0, 5, 5}); i.Is(Rect{1, 1, 4, 4}) != true {
t.Error("Failed to match: {1, 1, 4, 4}, to {0, 0, 5, 5}, expected intersect: {1, 1, 4, 4}")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment