updated IsRect to be public

this will allow users to make sure that the rectangles they define
really are rectangles
This commit is contained in:
Derek McQuay 2016-08-27 22:27:51 -07:00
parent a35e73ee10
commit 71f3aede25
2 changed files with 22 additions and 7 deletions

View File

@ -14,10 +14,10 @@ type Rectangle struct {
// maximum error used for floating point math // maximum error used for floating point math
var ɛ = 0.00001 var ɛ = 0.00001
// isRect determins if the rectangle provided really is a rectangle, which // IsRect determins if the rectangle provided really is a rectangle, which
// by definition means a plane figure with four straight sides and four // by definition means a plane figure with four straight sides and four
// right angles. // right angles.
func (r Rectangle) isRect() bool { func (r Rectangle) IsRect() bool {
// make sure they aren't all just the same point // make sure they aren't all just the same point
if (r.P1.X == r.P2.X && r.P1.X == r.P3.X && r.P1.X == r.P4.X) && if (r.P1.X == r.P2.X && r.P1.X == r.P3.X && r.P1.X == r.P4.X) &&
(r.P1.Y == r.P2.Y && r.P1.Y == r.P3.Y && r.P1.Y == r.P4.Y) { (r.P1.Y == r.P2.Y && r.P1.Y == r.P3.Y && r.P1.Y == r.P4.Y) {
@ -112,6 +112,19 @@ func Adjacency(r1, r2 Rectangle) bool {
return false return false
} }
func removeDuplicates(xs *[]Point) {
found := make(map[Point]bool)
j := 0
for i, x := range *xs {
if !found[x] {
found[x] = true
(*xs)[j] = (*xs)[i]
j++
}
}
*xs = (*xs)[:j]
}
// Intersection determine whether two rectangles, r1 and r2, have one or more // Intersection determine whether two rectangles, r1 and r2, have one or more
// intersecting lines and produce a result, []Point, identifying the points // intersecting lines and produce a result, []Point, identifying the points
// of intersection // of intersection
@ -141,6 +154,8 @@ func Intersection(r1, r2 Rectangle) []Point {
} }
} }
} }
removeDuplicates(&pts)
return pts return pts
} }

View File

@ -16,10 +16,10 @@ func TestIsRect(t *testing.T) {
{Rectangle{P1: Point{0, 0}, P2: Point{0, 0}, P3: Point{0, 0}, P4: Point{0, 0}}, false}, {Rectangle{P1: Point{0, 0}, P2: Point{0, 0}, P3: Point{0, 0}, P4: Point{0, 0}}, false},
} }
for _, rt := range isRectTests { for _, rt := range isRectTests {
actual := rt.r.isRect() actual := rt.r.IsRect()
if actual != rt.expected { if actual != rt.expected {
t.Errorf( t.Errorf(
"failed isRect:\n\texpected: %t\n\t actual: %t", "failed IsRect:\n\texpected: %t\n\t actual: %t",
rt.expected, rt.expected,
actual, actual,
) )
@ -29,7 +29,7 @@ func TestIsRect(t *testing.T) {
} }
func TestSize(t *testing.T) { func TestSize(t *testing.T) {
var isRectTests = []struct { var sizeTests = []struct {
r Rectangle r Rectangle
expected float64 expected float64
}{ }{
@ -40,7 +40,7 @@ func TestSize(t *testing.T) {
{Rectangle{P1: Point{0, 0}, P2: Point{0, 3}, P3: Point{2, 0}, P4: Point{2, 3}}, 6}, {Rectangle{P1: Point{0, 0}, P2: Point{0, 3}, P3: Point{2, 0}, P4: Point{2, 3}}, 6},
{Rectangle{P1: Point{0, 0}, P2: Point{0, 100}, P3: Point{100, 0}, P4: Point{100, 100}}, 10000}, {Rectangle{P1: Point{0, 0}, P2: Point{0, 100}, P3: Point{100, 0}, P4: Point{100, 100}}, 10000},
} }
for _, rt := range isRectTests { for _, rt := range sizeTests {
actual := rt.r.size() actual := rt.r.size()
if actual != rt.expected { if actual != rt.expected {
t.Errorf( t.Errorf(
@ -160,7 +160,7 @@ func TestIntersection(t *testing.T) {
[]Rectangle{ []Rectangle{
Rectangle{Point{0, 0}, Point{0, 3}, Point{3, 0}, Point{3, 3}}, Rectangle{Point{0, 0}, Point{0, 3}, Point{3, 0}, Point{3, 3}},
Rectangle{Point{0, 0}, Point{0, -1}, Point{-1, 0}, Point{-1, -1}}}, Rectangle{Point{0, 0}, Point{0, -1}, Point{-1, 0}, Point{-1, -1}}},
[]Point{Point{0, 0}, Point{0, 0}}, []Point{Point{0, 0}},
}, },
{ {
[]Rectangle{ []Rectangle{