implemented adjacency and tests
using the new line type am able to check if lines overlap or not.
This commit is contained in:
parent
d6e76ef26b
commit
aaf2dbb2eb
45
rectangle.go
45
rectangle.go
@ -54,8 +54,49 @@ func (r Rectangle) Size() float64 {
|
|||||||
return distance(r.P1, n1) * distance(r.P1, n2)
|
return distance(r.P1, n1) * distance(r.P1, n2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r Rectangle) inOrder() []Point {
|
||||||
|
n1, n2 := r.Neighbors(r.P1)
|
||||||
|
accross := &Point{}
|
||||||
|
if r.P2 != n1 || r.P2 != n2 {
|
||||||
|
accross = &r.P2
|
||||||
|
}
|
||||||
|
if r.P3 != n1 || r.P3 != n2 {
|
||||||
|
accross = &r.P3
|
||||||
|
}
|
||||||
|
if r.P4 != n1 || r.P4 != n2 {
|
||||||
|
accross = &r.P4
|
||||||
|
}
|
||||||
|
return []Point{r.P1, n1, n2, *accross}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Adjacency(r1, r2 Rectangle) bool {
|
||||||
|
order1 := r1.inOrder()
|
||||||
|
order2 := r2.inOrder()
|
||||||
|
|
||||||
|
sides1 := []line{
|
||||||
|
line{order1[0], order1[1]},
|
||||||
|
line{order1[0], order1[2]},
|
||||||
|
line{order1[3], order1[1]},
|
||||||
|
line{order1[3], order1[2]},
|
||||||
|
}
|
||||||
|
sides2 := []line{
|
||||||
|
line{order2[0], order2[1]},
|
||||||
|
line{order2[0], order2[2]},
|
||||||
|
line{order2[3], order2[1]},
|
||||||
|
line{order2[3], order2[2]},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, i := range sides1 {
|
||||||
|
for _, j := range sides2 {
|
||||||
|
if lineOnLine(i, j) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func sumOfTri(r Rectangle, p Point) bool {
|
func sumOfTri(r Rectangle, p Point) bool {
|
||||||
rsize := r.Size()
|
|
||||||
n1, n2 := r.Neighbors(r.P1)
|
n1, n2 := r.Neighbors(r.P1)
|
||||||
x1, x2 := Point{}, Point{}
|
x1, x2 := Point{}, Point{}
|
||||||
accross := &Point{}
|
accross := &Point{}
|
||||||
@ -75,7 +116,7 @@ func sumOfTri(r Rectangle, p Point) bool {
|
|||||||
SizeTriangle(r.P1, n2, p) +
|
SizeTriangle(r.P1, n2, p) +
|
||||||
SizeTriangle(*accross, x1, p) +
|
SizeTriangle(*accross, x1, p) +
|
||||||
SizeTriangle(*accross, x2, p)
|
SizeTriangle(*accross, x2, p)
|
||||||
if rsize == sumTri {
|
if r.Size() == sumTri {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
@ -95,3 +95,52 @@ func TestContainment(t *testing.T) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAdjacency(t *testing.T) {
|
||||||
|
var adjacencyTest = []struct {
|
||||||
|
r []Rectangle
|
||||||
|
expected bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
[]Rectangle{
|
||||||
|
Rectangle{Point{0, 0}, Point{0, 3}, Point{3, 0}, Point{3, 3}},
|
||||||
|
Rectangle{Point{2, 2}, Point{2, 3}, Point{3, 2}, Point{3, 3}}},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
[]Rectangle{
|
||||||
|
Rectangle{Point{0, 0}, Point{0, 3}, Point{3, 0}, Point{3, 3}},
|
||||||
|
Rectangle{Point{1, 3}, Point{2, 3}, Point{1, 4}, Point{2, 4}}},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
[]Rectangle{
|
||||||
|
Rectangle{Point{2, 2}, Point{2, 3}, Point{3, 2}, Point{3, 3}},
|
||||||
|
Rectangle{Point{1, 3}, Point{2, 3}, Point{1, 4}, Point{2, 4}}},
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
[]Rectangle{
|
||||||
|
Rectangle{Point{-2, -2}, Point{-2, -3}, Point{-3, -2}, Point{-3, -3}},
|
||||||
|
Rectangle{Point{-1, -3}, Point{-2, -3}, Point{-1, -4}, Point{-2, -4}}},
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
[]Rectangle{
|
||||||
|
Rectangle{Point{0, 0}, Point{0, -3}, Point{-3, 0}, Point{-3, -3}},
|
||||||
|
Rectangle{Point{-2, -2}, Point{-2, -3}, Point{-3, -2}, Point{-3, -3}}},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, rt := range adjacencyTest {
|
||||||
|
actual := Adjacency(rt.r[0], rt.r[1])
|
||||||
|
if actual != rt.expected {
|
||||||
|
t.Errorf(
|
||||||
|
"failed spiral:\n\texpected: %d\n\t actual: %d",
|
||||||
|
rt.expected,
|
||||||
|
actual,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user