geom/polygonf32.go
Sander Schobers e01aa3242d Added TriangleF.
Renamed Add to Extend on PolygonF{,32}.
Add now translates a polygon.
2021-08-09 01:37:16 +02:00

45 lines
1.1 KiB
Go

package geom
// PointsF32 is a set of points.
type PointsF32 []PointF32
// PolF32 creates a polygon of points q.
func PolF32(q ...PointF32) PolygonF32 { return PolygonF32{Points: q} }
// PolygonF32 is defined by a set of points (floating point).
type PolygonF32 struct {
Points PointsF32
}
// Add adds q as a vector to all points in p.
func (p PolygonF32) Add(q PointF32) PolygonF32 {
var r = p.copy()
for i, pt := range r.Points {
r.Points[i] = pt.Add(q)
}
return r
}
func (p PolygonF32) copy() PolygonF32 {
var q = PolygonF32{make(PointsF32, len(p.Points))}
copy(q.Points, p.Points)
return q
}
// Extend creates a new polygon based on p with one or more extra points q.
func (p PolygonF32) Extend(q ...PointF32) PolygonF32 {
var t = PolygonF32{make(PointsF32, len(p.Points)+len(q))}
copy(t.Points, p.Points)
copy(t.Points[len(p.Points):], q)
return t
}
// Mul multiplies the points of polygon p with t and returns the result.
func (p PolygonF32) Mul(t float32) PolygonF32 {
var q = p.copy()
for i, pt := range q.Points {
q.Points[i] = pt.Mul(t)
}
return q
}