Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
John Asmuth committed Aug 5, 2011
1 parent 5c22d95 commit 2888450
Show file tree
Hide file tree
Showing 15 changed files with 521 additions and 68 deletions.
4 changes: 4 additions & 0 deletions debug.go
Original file line number Diff line number Diff line change
@@ -1,3 1,7 @@
// Copyright 2009 The geom Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package geom

import "fmt"
Expand Down
10 changes: 10 additions & 0 deletions geom.go
Original file line number Diff line number Diff line change
@@ -1,6 1,16 @@
// Copyright 2009 The geom Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//target:github.com/skelterjohn/geom
package geom

type Bounded interface {
Bounds() (bounds *Rect)
}

type Transformable interface {
Translate(offset Point)
Rotate(rad float64)
Scale(xfactor, yfactor float64)
}
66 changes: 51 additions & 15 deletions path.go
Original file line number Diff line number Diff line change
@@ -1,3 1,7 @@
// Copyright 2009 The geom Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package geom

import (
Expand All @@ -9,6 13,36 @@ type Path struct {
bounds Rect
}

func (p *Path) Translate(offset Point) {
p.bounds.Translate(offset)
for i := range p.vertices {
p.vertices[i].Translate(offset)
}
}

func (p *Path) Rotate(rad float64) {
for i := range p.vertices {
p.vertices[i].Rotate(rad)
}
p.bounds = Rect{p.vertices[0], p.vertices[0]}
p.bounds.ExpandToContain(PointChan(p.vertices[1:]))
}

func (p *Path) Scale(xf, yf float64) {

for i := range p.vertices {
p.vertices[i].Scale(xf, yf)
}
p.bounds.Scale(xf, yf)
}

func (p *Path) Clone() (op *Path) {
op = &Path{}
op.bounds = *p.bounds.Clone()
op.vertices = append([]Point{}, p.vertices...)
return
}

//uncomment to check interface fulfillment
//var _ Bounded = &Path{}

Expand All @@ -19,14 53,30 @@ func (p *Path) Equals(oi interface{}) bool {
if len(p.vertices) != len(o.vertices) { return false }

for i := range p.vertices {
if !p.vertices[i].Equals(o.vertices[i]) {
if !p.vertices[i].EqualsPoint(o.vertices[i]) {
return false
}
}

return true
}

func (p *Path) Register(op *Path) (offset Point, match bool ) {
offset = p.bounds.Min.Minus(op.bounds.Min)
if len(p.vertices) != len(op.vertices) {
dbg("registure failure: wrong counts")
return // with match = false
}
for i := range p.vertices {
if !p.vertices[i].EqualsPoint(op.vertices[i].Plus(offset)) {
dbg("register failure: v1=%v v2=%v offset=%v", p.vertices[i], op.vertices[i], offset)
return // with match = false
}
}
match = true
return
}

func (p *Path) Length() int {
return len(p.vertices)
}
Expand Down Expand Up @@ -58,20 108,6 @@ func (p *Path) Vertices() (v []Point) {
return
}

func (p *Path) Translate(offset Point) {
p.bounds.Translate(offset)
for i, v := range p.vertices {
p.vertices[i] = v.Plus(offset)
}
}

func (p *Path) Scale(factor float64) {
p.bounds.Scale(factor)
for i, v := range p.vertices {
p.vertices[i] = v.Times(factor)
}
}

func (me *Path) Error(other *Path) (offset Point, error float64) {

meCenter := me.bounds.Center()
Expand Down
50 changes: 49 additions & 1 deletion point.go
Original file line number Diff line number Diff line change
@@ -1,3 1,7 @@
// Copyright 2009 The geom Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package geom

import (
Expand All @@ -8,7 12,39 @@ type Point struct {
X, Y float64
}

func (p Point) Equals(q Point) bool {
func (p *Point) Hashcode() (hash uint64) {
x, y := uint64(p.X), uint64(p.Y)
hash = x y
return
}

func (p *Point) Equals(oi interface{}) (equals bool) {
o, equals := oi.(*Point)
if !equals {
var op Point
op, equals = oi.(Point)
equals = equals && p.EqualsPoint(op)
return
}
equals = p.EqualsPoint(*o)
return
}

func (p *Point) Translate(offset Point) {
*p = p.Plus(offset)
}

func (p *Point) Rotate(rad float64) {
p.X = p.X * math.Cos(rad) - p.Y * math.Sin(rad)
p.Y = p.X * math.Sin(rad) p.Y * math.Cos(rad)
}

func (p *Point) Scale(xfactor, yfactor float64) {
p.X *= xfactor
p.Y *= yfactor
}

func (p Point) EqualsPoint(q Point) bool {
return p.X == q.X && p.Y == q.Y
}

Expand Down Expand Up @@ -86,3 122,15 @@ func VertexAngle(A, B, C Point) (r float64) {
}
return
}

func PointChan(points []Point) (ch <-chan Point) {
tch := make(chan Point, len(points))
go func(points []Point, ch chan<- Point) {
for _, p := range points {
ch <- p
}
close(ch)
}(points, tch)
ch = tch
return
}
4 changes: 4 additions & 0 deletions point_test.go
Original file line number Diff line number Diff line change
@@ -1,3 1,7 @@
// Copyright 2009 The geom Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package geom

import (
Expand Down
15 changes: 15 additions & 0 deletions poly.go
Original file line number Diff line number Diff line change
@@ -1,3 1,7 @@
// Copyright 2009 The geom Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package geom

import (
Expand All @@ -15,12 19,23 @@ func wrapIndex(index, length int) (i int) {
}
return
}

func (p *Polygon) Clone() (op *Polygon) {
op = &Polygon{*p.Path.Clone()}
return
}

func (p *Polygon) Equals(oi interface{}) bool {
o, ok := oi.(*Polygon)
if !ok { return false }
return (&p.Path).Equals(&o.Path)
}

func (p *Polygon) Register(op *Polygon) (offset Point, match bool) {
offset, match = p.Path.Register(&op.Path)
return
}

func (me *Polygon) Vertex(index int) (v Point) {
v = me.vertices[wrapIndex(index, len(me.vertices))]
return
Expand Down
4 changes: 4 additions & 0 deletions poly_test.go
Original file line number Diff line number Diff line change
@@ -1,3 1,7 @@
// Copyright 2009 The geom Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package geom

import (
Expand Down
35 changes: 35 additions & 0 deletions qtree/overlap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 1,35 @@
package qtree

/*
A number of tests to test qtree's ability to analyze the differences between
collections of shapes
*/

import (
"github.com/skelterjohn/geom"
)

func col1() (items []Item) {
items = append(items, Item(&geom.Rect{geom.Point{0, 0}, geom.Point{1, 1}}))
items = append(items, Item(&geom.Rect{geom.Point{0, 2}, geom.Point{1, 3}}))
items = append(items, Item(&geom.Rect{geom.Point{2, 2}, geom.Point{3, 3}}))
items = append(items, Item(&geom.Rect{geom.Point{2, 0}, geom.Point{3, 1}}))
return
}

func col2() (items []Item) {
items = append(items, Item(&geom.Rect{geom.Point{10, 0}, geom.Point{11, 1}}))
items = append(items, Item(&geom.Rect{geom.Point{10, 2}, geom.Point{11, 3}}))
items = append(items, Item(&geom.Rect{geom.Point{12, 2}, geom.Point{13, 3}}))
items = append(items, Item(&geom.Rect{geom.Point{12, 0}, geom.Point{13, 1}}))
return
}

func col3() (items []Item) {
items = append(items, Item(&geom.Rect{geom.Point{0, 10}, geom.Point{1, 11}}))
items = append(items, Item(&geom.Rect{geom.Point{0, 12}, geom.Point{1, 13}}))
items = append(items, Item(&geom.Rect{geom.Point{2, 12}, geom.Point{3, 13}}))
items = append(items, Item(&geom.Rect{geom.Point{2, 10}, geom.Point{3, 11}}))
return
}

Loading

0 comments on commit 2888450

Please sign in to comment.