Skip to content

Commit

Permalink
gofmt
Browse files Browse the repository at this point in the history
  • Loading branch information
John Asmuth committed Aug 5, 2011
1 parent 2888450 commit 2e48e61
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 123 deletions.
2 changes: 1 addition & 1 deletion debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ func dbg(format string, args ...interface{}) {
if Debug {
fmt.Printf(format+"\n", args...)
}
}
}
2 changes: 1 addition & 1 deletion geom.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ type Transformable interface {
Translate(offset Point)
Rotate(rad float64)
Scale(xfactor, yfactor float64)
}
}
28 changes: 16 additions & 12 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (p *Path) Rotate(rad float64) {
}

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

for i := range p.vertices {
p.vertices[i].Scale(xf, yf)
}
Expand All @@ -48,20 +48,24 @@ func (p *Path) Clone() (op *Path) {

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

if len(p.vertices) != len(o.vertices) { return false }

if !ok {
return false
}

if len(p.vertices) != len(o.vertices) {
return false
}

for i := range p.vertices {
if !p.vertices[i].EqualsPoint(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 ) {
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")
Expand Down Expand Up @@ -109,22 +113,22 @@ func (p *Path) Vertices() (v []Point) {
}

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

meCenter := me.bounds.Center()
oCenter := other.bounds.Center()

offset = meCenter.Minus(oCenter)
if len(me.vertices) != len(other.vertices) {
error = math.Inf(1)
return
}

for i, mv := range me.vertices {
ov := other.vertices[i]
offsetMe := mv.Minus(meCenter)
offsetOther := ov.Minus(oCenter)
error += offsetMe.DistanceFrom(offsetOther)
}

return
}
8 changes: 4 additions & 4 deletions point.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Point struct {

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

Expand All @@ -35,8 +35,8 @@ func (p *Point) Translate(offset Point) {
}

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)
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) {
Expand Down Expand Up @@ -133,4 +133,4 @@ func PointChan(points []Point) (ch <-chan Point) {
}(points, tch)
ch = tch
return
}
}
53 changes: 28 additions & 25 deletions poly.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Polygon struct {
func wrapIndex(index, length int) (i int) {
i = index % length
if i < 0 {
i = length+i
i = length + i
}
return
}
Expand All @@ -27,7 +27,9 @@ func (p *Polygon) Clone() (op *Polygon) {

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

Expand All @@ -42,14 +44,14 @@ func (me *Polygon) Vertex(index int) (v Point) {
}

func (me *Polygon) Segment(index int) (s *Segment) {
s = &Segment{me.Vertex(index), me.Vertex(index+1)}
s = &Segment{me.Vertex(index), me.Vertex(index + 1)}
return
}

func (me *Polygon) VertexAngle(index int) (r float64) {
a := me.Vertex(index-1)
a := me.Vertex(index - 1)
b := me.Vertex(index)
c := me.Vertex(index+1)
c := me.Vertex(index + 1)
r = VertexAngle(a, b, c)
return
}
Expand All @@ -58,18 +60,18 @@ func (me *Polygon) WindingOrder() (winding float64) {
for i := 0; i < len(me.vertices); i++ {
winding += me.VertexAngle(i)
}
return
return
}

func (me *Polygon) ContainsPoint(p Point) bool {
fakeSegment := &Segment{p, Point{p.X, p.Y+1}}
fakeSegment := &Segment{p, Point{p.X, p.Y + 1}}

above := 0
for i := 0; i < me.Length(); i++ {
s := me.Segment(i)
uh, uv := s.IntersectParameters(fakeSegment)
if uh < 0 || uh >= 1 {
continue
continue
}
if uv > 0 {
above++
Expand All @@ -82,19 +84,19 @@ func (me *Polygon) ContainsPoint(p Point) bool {
func (me *Polygon) Bisect(i, j int) (p1, p2 *Polygon) {
i = wrapIndex(i, len(me.vertices))
j = wrapIndex(j, len(me.vertices))

//build the first one, starting at i and ending at j
p1 = &Polygon{}
for c := i; c != wrapIndex(j+1, len(me.vertices)); c = wrapIndex(c+1, len(me.vertices)) {
p1.AddVertex(me.Vertex(c))
}

//build the second one, starting at j and ending at i
p2 = &Polygon{}
for c := j; c != wrapIndex(i+1, len(me.vertices)); c = wrapIndex(c+1, len(me.vertices)) {
p2.AddVertex(me.Vertex(c))
}

return
}

Expand All @@ -104,21 +106,22 @@ func (me *Polygon) Error(other *Polygon) (offset Point, error float64) {

func (me *Polygon) Triangles() (tris []Triangle, ok bool) {
dbg("%v.Triangles()", me)

if me.Length() == 3 {
dbg("already a triangle")
tris = []Triangle{Triangle{me.Vertex(0), me.Vertex(1), me.Vertex(2)}}
ok = true
return
}
for i:=0; i<me.Length(); i++ {

for i := 0; i < me.Length(); i++ {
iv := me.Vertex(i)
v2: for j:=i+2; j!=wrapIndex(i-1, me.Length()); j=wrapIndex(j+1, me.Length()) {
v2:
for j := i + 2; j != wrapIndex(i-1, me.Length()); j = wrapIndex(j+1, me.Length()) {
jv := me.Vertex(j)
bisectingSegment := &Segment{iv, jv}
dbg("bisectingSegment(%d, %d) = %v", i, j, bisectingSegment)

//first check to see that it doesn't intersect any other segments
for si := 0; si < me.Length(); si++ {
s := me.Segment(si)
Expand All @@ -127,19 +130,19 @@ v2: for j:=i+2; j!=wrapIndex(i-1, me.Length()); j=wrapIndex(j+1, me.Length()) {
dbg(" Segment(%d, %d) %v\n%f %f", si, si+1, s, u1, u2)
continue v2
} else {
dbg(" doesn"t intersect %v: %f %f", s, u1, u2)
dbg(" doesn"t intersect %v: %f %f", s, u1, u2)
}
}

//second check to see that it is in the interior of the polygon
midPoint := bisectingSegment.Extrapolate(0.5)
if !me.ContainsPoint(midPoint) {
dbg(" poly contains %v", midPoint)
continue v2
}

dbg(" Segment %v is good", bisectingSegment)

p1, p2 := me.Bisect(i, j)
t1, ok1 := p1.Triangles()
t2, ok2 := p2.Triangles()
Expand All @@ -148,9 +151,9 @@ v2: for j:=i+2; j!=wrapIndex(i-1, me.Length()); j=wrapIndex(j+1, me.Length()) {
return
}
}

dbg("failed with %v", me)
//panic("couldn't find any valid bisecting segment")
return
}

return
}
32 changes: 16 additions & 16 deletions poly_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ func TestInsert(t *testing.T) {
p.AddVertex(Point{1, 1})
p.AddVertex(Point{1, 0})
p.InsertVertexAfter(Point{5, 0}, 2)

p2 := &Polygon{}
p2.AddVertex(Point{0, 0})
p2.AddVertex(Point{0, 1})
p2.AddVertex(Point{5, 0})
p2.AddVertex(Point{1, 1})
p2.AddVertex(Point{1, 0})

if !p.Equals(p2) {
t.Fail()
}
Expand All @@ -39,12 +39,12 @@ func TestPolyTriangularize(t *testing.T) {
if ok {
fmt.Println()
for _, tri := range tris {
fmt.Printf("triangle: %v\n", tri)
fmt.Printf("triangle: %v\n", tri)
}
} else {
fmt.Printf("No triangles for %v\n", poly)
fmt.Printf("No triangles for %v\n", poly)
}

poly = new(Polygon)
poly.AddVertex(Point{0, 0})
poly.AddVertex(Point{1, 1})
Expand All @@ -56,12 +56,12 @@ func TestPolyTriangularize(t *testing.T) {
if ok {
fmt.Println()
for _, tri := range tris {
fmt.Printf("triangle: %v\n", tri)
fmt.Printf("triangle: %v\n", tri)
}
} else {
fmt.Printf("No triangles for %v\n", poly)
fmt.Printf("No triangles for %v\n", poly)
}

poly = new(Polygon)
poly.AddVertex(Point{2, 1})
poly.AddVertex(Point{2, 2})
Expand All @@ -71,10 +71,10 @@ func TestPolyTriangularize(t *testing.T) {
if ok {
fmt.Println()
for _, tri := range tris {
fmt.Printf("triangle: %v\n", tri)
fmt.Printf("triangle: %v\n", tri)
}
} else {
fmt.Printf("No triangles for %v\n", poly)
fmt.Printf("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 @@ -100,12 +100,12 @@ func TestPiece(t *testing.T) {
if ok {
fmt.Println()
for _, tri := range tris {
fmt.Printf("triangle: %v\n", tri)
fmt.Printf("triangle: %v\n", tri)
}
} else {
fmt.Printf("No triangles for %v\n", poly)
fmt.Printf("No triangles for %v\n", poly)
}

vertices = []Point{
Point{44, 736},
Point{44, 848},
Expand All @@ -129,9 +129,9 @@ func TestPiece(t *testing.T) {
if ok {
fmt.Println()
for _, tri := range tris {
fmt.Printf("triangle: %v\n", tri)
fmt.Printf("triangle: %v\n", tri)
}
} else {
fmt.Printf("No triangles for %v\n", poly)
fmt.Printf("No triangles for %v\n", poly)
}
}
}
1 change: 0 additions & 1 deletion qtree/overlap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ func col3() (items []Item) {
items = append(items, Item(&geom.Rect{geom.Point{2, 10}, geom.Point{3, 11}}))
return
}

Loading

0 comments on commit 2e48e61

Please sign in to comment.