Skip to content

Commit

Permalink
updated for weekly
Browse files Browse the repository at this point in the history
  • Loading branch information
skelterjohn committed Dec 16, 2011
1 parent 2e48e61 commit fe1c93e
Show file tree
Hide file tree
Showing 14 changed files with 299 additions and 276 deletions.
159 changes: 159 additions & 0 deletions coord.go
Original file line number Diff line number Diff line change
@@ -0,0 1,159 @@
// 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 (
"math"
)

type Coord struct {
X, Y float64
}

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

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

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

func (p *Coord) 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 *Coord) RotateLeft() {
p.X, p.Y = -p.Y, p.X
}

func (p *Coord) RotateRight() {
p.X, p.Y = p.Y, -p.X
}

func (p Coord) Unit() (u Coord) {
m := p.Magnitude()
u.X = p.X / m
u.Y = p.Y / m
return
}

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

func (p Coord) EqualsCoord(q Coord) bool {
return p.X == q.X && p.Y == q.Y
}

func (p Coord) DistanceFrom(q Coord) (d float64) {
return p.Minus(q).Magnitude()
}

func (p Coord) DistanceFromSquared(q Coord) (ds float64) {
return p.Minus(q).MagnitudeSquared()
}

func (p Coord) Magnitude() (m float64) {
m = math.Sqrt(p.MagnitudeSquared())
return
}

func (p Coord) MagnitudeSquared() (ms float64) {
ms = p.X*p.X p.Y*p.Y
return
}

func (p Coord) Minus(q Coord) (r Coord) {
r.X = p.X - q.X
r.Y = p.Y - q.Y
return
}

func (p Coord) Plus(q Coord) (r Coord) {
r.X = p.X q.X
r.Y = p.Y q.Y
return
}

func (p Coord) Times(s float64) (r Coord) {
r.X = p.X * s
r.Y = p.Y * s
return
}

func (p Coord) QuadPP(q Coord) bool {
return q.X >= p.X && q.Y >= p.Y
}

func (p Coord) QuadPM(q Coord) bool {
return q.X >= p.X && q.Y <= p.Y
}

func (p Coord) QuadMP(q Coord) bool {
return q.X <= p.X && q.Y >= p.Y
}

func (p Coord) QuadMM(q Coord) bool {
return q.X <= p.X && q.Y <= p.Y
}

func DotProduct(p, q Coord) (r float64) {
r = p.X*q.X p.Y*q.Y
return
}

func CrossProduct(p, q Coord) (z float64) {
z = p.X*q.Y - p.Y*q.X
return
}

func VectorAngle(X, Y Coord) (r float64) {
XdotY := DotProduct(X, Y)
mXmY := X.Magnitude() * Y.Magnitude()
r = math.Acos(XdotY / mXmY)
z := CrossProduct(X, Y)
if z < 0 {
r *= -1
}
return
}

func VertexAngle(A, B, C Coord) (r float64) {
X := A.Minus(B)
Y := C.Minus(B)
r = VectorAngle(X, Y)
if r < 0 {
r *= -1
}
return
}

func CoordChan(points []Coord) (ch <-chan Coord) {
tch := make(chan Coord, len(points))
go func(points []Coord, ch chan<- Coord) {
for _, p := range points {
ch <- p
}
close(ch)
}(points, tch)
ch = tch
return
}
2 changes: 1 addition & 1 deletion geom.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 10,7 @@ type Bounded interface {
}

type Transformable interface {
Translate(offset Point)
Translate(offset Coord)
Rotate(rad float64)
Scale(xfactor, yfactor float64)
}
24 changes: 12 additions & 12 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 9,11 @@ import (
)

type Path struct {
vertices []Point
vertices []Coord
bounds Rect
}

func (p *Path) Translate(offset Point) {
func (p *Path) Translate(offset Coord) {
p.bounds.Translate(offset)
for i := range p.vertices {
p.vertices[i].Translate(offset)
Expand All @@ -25,7 25,7 @@ func (p *Path) Rotate(rad float64) {
p.vertices[i].Rotate(rad)
}
p.bounds = Rect{p.vertices[0], p.vertices[0]}
p.bounds.ExpandToContain(PointChan(p.vertices[1:]))
p.bounds.ExpandToContain(CoordChan(p.vertices[1:]))
}

func (p *Path) Scale(xf, yf float64) {
Expand All @@ -39,7 39,7 @@ func (p *Path) Scale(xf, yf float64) {
func (p *Path) Clone() (op *Path) {
op = &Path{}
op.bounds = *p.bounds.Clone()
op.vertices = append([]Point{}, p.vertices...)
op.vertices = append([]Coord{}, p.vertices...)
return
}

Expand All @@ -57,22 57,22 @@ func (p *Path) Equals(oi interface{}) bool {
}

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

return true
}

func (p *Path) Register(op *Path) (offset Point, match bool) {
func (p *Path) Register(op *Path) (offset Coord, 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)) {
if !p.vertices[i].EqualsCoord(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
}
Expand All @@ -85,19 85,19 @@ func (p *Path) Length() int {
return len(p.vertices)
}

func (p *Path) AddVertex(v Point) {
func (p *Path) AddVertex(v Coord) {
if len(p.vertices) == 0 {
p.bounds = Rect{
Min: v,
Max: v,
}
} else {
p.bounds.ExpandToContainPoint(v)
p.bounds.ExpandToContainCoord(v)
}
p.vertices = append(p.vertices, v)
}

func (p *Path) InsertVertexAfter(v Point, index int) {
func (p *Path) InsertVertexAfter(v Coord, index int) {
p.vertices = append(p.vertices, v)
copy(p.vertices[index 1:], p.vertices[index:len(p.vertices)-1])
p.vertices[index] = v
Expand All @@ -107,12 107,12 @@ func (p *Path) Bounds() (bounds *Rect) {
return &p.bounds
}

func (p *Path) Vertices() (v []Point) {
func (p *Path) Vertices() (v []Coord) {
v = p.vertices
return
}

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

meCenter := me.bounds.Center()
oCenter := other.bounds.Center()
Expand Down
Loading

0 comments on commit fe1c93e

Please sign in to comment.