convenience functions for converting things to polygons

This commit is contained in:
Fraser Graham 2013-11-26 21:40:32 -08:00
parent 9f5fcf5543
commit 4a1b471742
2 changed files with 33 additions and 3 deletions

View File

@ -354,3 +354,8 @@ func TestPolyPolyIntersectFail(t *testing.T) {
i, m, p := PolyPolyIntersection(p1, Vector2d{0, 0}, p2)
fmt.Printf("%v %v %v\n", i, m, p)
}
func TestOrientedSquare(t *testing.T) {
p := OrientedSquare(Point2d{10, 10}, Vector2d{0.5, 0.5}, 5)
fmt.Printf("Square: %v\n", p)
}

View File

@ -79,6 +79,10 @@ func (p Point2d) ToVector() Vector2d {
return Vector2d{p.X, p.Y}
}
func (v Vector2d) ToPoint() Point2d {
return Point2d{v.X, v.Y}
}
func (v1 Vector2d) Rotate(a float32) Vector2d {
x := float64(v1.X)*math.Cos(float64(a)) - float64(v1.Y)*math.Sin(float64(a))
y := float64(v1.X)*math.Sin(float64(a)) + float64(v1.Y)*math.Cos(float64(a))
@ -95,9 +99,30 @@ func RectFromPoint(p Point2d, size float32) AABB2d {
B: Point2d{X: p.X + size, Y: p.Y + size}}
}
func PolygonFromAABB(r AABB2d) Polygon2d {
// TBD
return Polygon2d{}
func (r AABB2d) ToPolygon() Polygon2d {
p := Polygon2d{}
p.Origin = r.A
p.Points = append(p.Points, r.A.Sub(p.Origin))
p.Points = append(p.Points, Point2d{r.A.X, r.B.Y}.Sub(p.Origin))
p.Points = append(p.Points, r.B.Sub(p.Origin))
p.Points = append(p.Points, Point2d{r.B.X, r.A.Y}.Sub(p.Origin))
return p
}
func OrientedSquare(center Point2d, heading Vector2d, size float32) Polygon2d {
out := Polygon2d{}
out.Origin.X = center.X
out.Origin.Y = center.Y
x := heading.Normalize().Scale(size)
y := Vector2d{-x.Y, x.X}
z := Point2d{}
out.Points = append(out.Points, z.Add(x).Add(y).ToVector())
out.Points = append(out.Points, z.Add(y).Sub(x.ToPoint()))
out.Points = append(out.Points, z.Sub(x.ToPoint()).ToPoint().Sub(y.ToPoint()))
out.Points = append(out.Points, z.Add(x).Sub(y.ToPoint()))
return out
}
func (v Vector2d) Normalize() Vector2d {