Skip to content

Commit

Permalink
sync
Browse files Browse the repository at this point in the history
  • Loading branch information
skelterjohn committed May 11, 2012
1 parent eb8bc70 commit 39dc68c
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 24 deletions.
20 changes: 20 additions & 0 deletions line.go
Original file line number Diff line number Diff line change
@@ -0,0 1,20 @@
package geom


/*
A line that goes through Intersection and has a normal Normal.
*/
type Line struct {
Intersection Coord
Normal Coord
}

func LineIntersection(l1, l2 Line) (p Coord) {
b1 := l1.Normal.Unit()
b1.RotateRight()
unum := (l2.Normal.X*l2.Intersection.X l2.Normal.Y*l2.Intersection.Y)
uden := (l2.Normal.X*b1.X l2.Normal.Y*b1.Y)
u := unum / uden
p = l1.Intersection.Plus(b1.Times(u))
return
}
34 changes: 34 additions & 0 deletions line_test.go
Original file line number Diff line number Diff line change
@@ -0,0 1,34 @@
package geom

import (
"fmt"
"testing"
)

type LineIntersectionTest struct {
l1, l2 Line
p Coord
}

var itests = []LineIntersectionTest {
{
Line{
Intersection: Coord{0, 0},
Normal: Coord{0, 1},
},
Line{
Intersection: Coord{1, 1},
Normal: Coord{1, 0},
},
Coord{0, 1},
},
}

func TestIntersect(t *testing.T) {
for _, test := range itests {
p := LineIntersection(test.l1, test.l2)
if p != test.p {
t.Error(fmt.Sprintf("should be %v, was %v", test.p, p))
}
}
}
41 changes: 26 additions & 15 deletions poly_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 9,17 @@ import (
"fmt"
)

func debugln(args ...interface{}) {
if false {
fmt.Println(args...)
}
}
func debugf(format string, args ...interface{}) {
if false {
fmt.Printf(format, args...)
}
}

func TestInsert(t *testing.T) {
p := &Polygon{}
p.AddVertex(Coord{0, 0})
Expand Down Expand Up @@ -37,12 48,12 @@ func TestPolyTriangularize(t *testing.T) {
poly.AddVertex(Coord{1, 0})
tris, ok := poly.Triangles()
if ok {
fmt.Println()
debugln()
for _, tri := range tris {
fmt.Printf("triangle: %v\n", tri)
debugf("triangle: %v\n", tri)
}
} else {
fmt.Printf("No triangles for %v\n", poly)
debugf("No triangles for %v\n", poly)
}

poly = new(Polygon)
Expand All @@ -54,12 65,12 @@ func TestPolyTriangularize(t *testing.T) {
poly.AddVertex(Coord{0, 3})
tris, ok = poly.Triangles()
if ok {
fmt.Println()
debugln()
for _, tri := range tris {
fmt.Printf("triangle: %v\n", tri)
debugf("triangle: %v\n", tri)
}
} else {
fmt.Printf("No triangles for %v\n", poly)
debugf("No triangles for %v\n", poly)
}

poly = new(Polygon)
Expand All @@ -69,12 80,12 @@ func TestPolyTriangularize(t *testing.T) {
poly.AddVertex(Coord{1, 3})
tris, ok = poly.Triangles()
if ok {
fmt.Println()
debugln()
for _, tri := range tris {
fmt.Printf("triangle: %v\n", tri)
debugf("triangle: %v\n", tri)
}
} else {
fmt.Printf("No triangles for %v\n", poly)
debugf("No triangles for %v\n", poly)
}
}
//{44 736} {44 848} {88 848} {88 1044} {44 1044} {44 1244} {68 1244} {68 1068} {112 1068} {112 824} {68 824} {68 736}
Expand All @@ -98,12 109,12 @@ func TestPiece(t *testing.T) {
}
tris, ok := poly.Triangles()
if ok {
fmt.Println()
debugln()
for _, tri := range tris {
fmt.Printf("triangle: %v\n", tri)
debugf("triangle: %v\n", tri)
}
} else {
fmt.Printf("No triangles for %v\n", poly)
debugf("No triangles for %v\n", poly)
}

vertices = []Coord{
Expand All @@ -127,11 138,11 @@ func TestPiece(t *testing.T) {
}
tris, ok = poly.Triangles()
if ok {
fmt.Println()
debugln()
for _, tri := range tris {
fmt.Printf("triangle: %v\n", tri)
debugf("triangle: %v\n", tri)
}
} else {
fmt.Printf("No triangles for %v\n", poly)
debugf("No triangles for %v\n", poly)
}
}
4 changes: 2 additions & 2 deletions qtree/qtree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 8,9 @@ import (

func TestInsertCollect(t *testing.T) {
cfg := ConfigDefault()
qt := New(cfg, &geom.Rect{geom.Coord{0, 0}, geom.Coord{100, 100}})
qt := New(cfg, geom.Rect{geom.Coord{0, 0}, geom.Coord{100, 100}})

r := &geom.Rect{geom.Coord{20, 20}, geom.Coord{40, 40}}
r := geom.Rect{geom.Coord{20, 20}, geom.Coord{40, 40}}
qt.Insert(r)

collection := make(map[Item]bool)
Expand Down
27 changes: 25 additions & 2 deletions rect.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 5,8 @@
package geom

import (
"math"
"fmt"
"math"
)

type Rect struct {
Expand Down Expand Up @@ -48,7 48,7 @@ func (r Rect) ContainsRect(o Rect) bool {
return r.ContainsCoord(o.Min) && r.ContainsCoord(o.Max)
}

func (r Rect) Translate(offset Coord) {
func (r *Rect) Translate(offset Coord) {
r.Min = r.Min.Plus(offset)
r.Max = r.Max.Plus(offset)
}
Expand Down Expand Up @@ -115,6 115,29 @@ func RectsIntersect(r1, r2 Rect) bool {
return xoverlap && yoverlap
}

func RectsIntersectStrict(r1, r2 Rect) bool {
ov := func(min1, max1, min2, max2 float64) (overlap bool) {
if min1 < min2 && max1 > min2 {
return true
}
if min1 < max2 && max1 > max2 {
return true
}
if min2 < min1 && max2 > min1 {
return true
}
if min2 < max1 && max2 > max1 {
return true
}
return false
}
dbg("RI(%v, %v)", r1, r2)
xoverlap := ov(r1.Min.X, r1.Max.X, r2.Min.X, r2.Max.X)
yoverlap := ov(r1.Min.Y, r1.Max.Y, r2.Min.Y, r2.Max.Y)
dbg("%v %v", xoverlap, yoverlap)
return xoverlap && yoverlap
}

func RectsEqual(r1, r2 Rect) bool {
if !r1.Min.EqualsCoord(r2.Min) {
return false
Expand Down
10 changes: 5 additions & 5 deletions rect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 9,8 @@ import (
)

func TestRectsIntersect(t *testing.T) {
r1 := &Rect{Coord{0, 0}, Coord{650, 650}}
r2 := &Rect{Coord{200, 500}, Coord{450, 750}}
r1 := Rect{Coord{0, 0}, Coord{650, 650}}
r2 := Rect{Coord{200, 500}, Coord{450, 750}}
if !r1.Min.QuadPP(r2.Min) {
t.Error("QuadPP")
}
Expand All @@ -24,10 24,10 @@ func TestRectsIntersect(t *testing.T) {
t.Error("intersect")
}

r1 = &Rect{Coord{325, 325}, Coord{650, 650}}
r2 = &Rect{Coord{200, 500}, Coord{450, 750}}
r1 = Rect{Coord{325, 325}, Coord{650, 650}}
r2 = Rect{Coord{200, 500}, Coord{450, 750}}

Debug = true
Debug = false

if !r1.Min.QuadPP(r2.Min) {
t.Error("QuadPP2")
Expand Down

0 comments on commit 39dc68c

Please sign in to comment.