Added Sub on RectangleF{,32}.

Added Invert on PointF{,32}.
Changed formula of Center (PointF{,32}).
This commit is contained in:
Sander Schobers 2019-03-06 19:24:03 +01:00
parent 2202b4d1a8
commit afb30dc1e6
4 changed files with 28 additions and 2 deletions

View File

@ -72,6 +72,11 @@ func (p PointF) InPolygon(q PolygonF) bool {
return c
}
// Invert changes the sign of the components.
func (p PointF) Invert() PointF {
return PointF{-p.X, -p.Y}
}
// Sub subtracts q as a vector from p.
func (p PointF) Sub(q PointF) PointF {
return PointF{p.X - q.X, p.Y - q.Y}

View File

@ -54,6 +54,11 @@ func (p PointF32) In(r RectangleF32) bool {
return true
}
// Invert changes the sign of the components.
func (p PointF32) Invert() PointF32 {
return PointF32{-p.X, -p.Y}
}
// Sub subtracts q as a vector from p.
func (p PointF32) Sub(q PointF32) PointF32 {
return PointF32{p.X - q.X, p.Y - q.Y}

View File

@ -27,7 +27,7 @@ func (r RectangleF) Add(p PointF) RectangleF {
// Center returns the center of the rectangle.
func (r RectangleF) Center() PointF {
return PointF{r.Min.X + .5*(r.Max.X-r.Min.X), r.Min.Y + .5*(r.Max.Y-r.Min.Y)}
return PointF{.5 * (r.Min.X + r.Max.X), .5 * (r.Min.Y + r.Max.Y)}
}
// Dx returns the width of r.
@ -64,3 +64,11 @@ func (r RectangleF) Inset(n float64) RectangleF {
func (r RectangleF) Size() PointF {
return PointF{r.Max.X - r.Min.X, r.Max.Y - r.Min.Y}
}
// Sub translates rectangle r in the opposite direction of point p.
func (r RectangleF) Sub(p PointF) RectangleF {
return RectangleF{
PointF{r.Min.X - p.X, r.Min.Y - p.Y},
PointF{r.Max.X - p.X, r.Max.Y - p.Y},
}
}

View File

@ -27,7 +27,7 @@ func (r RectangleF32) Add(p PointF32) RectangleF32 {
// Center returns the center of the rectangle.
func (r RectangleF32) Center() PointF32 {
return PointF32{r.Min.X + .5*(r.Max.X-r.Min.X), r.Min.Y + .5*(r.Max.Y-r.Min.Y)}
return PointF32{.5 * (r.Min.X + r.Max.X), .5 * (r.Min.Y + r.Max.Y)}
}
// Dx returns the width of r.
@ -64,3 +64,11 @@ func (r RectangleF32) Inset(n float32) RectangleF32 {
func (r RectangleF32) Size() PointF32 {
return PointF32{r.Max.X - r.Min.X, r.Max.Y - r.Min.Y}
}
// Sub translates rectangle r in the opposite direction of point p.
func (r RectangleF32) Sub(p PointF32) RectangleF32 {
return RectangleF32{
PointF32{r.Min.X - p.X, r.Min.Y - p.Y},
PointF32{r.Max.X - p.X, r.Max.Y - p.Y},
}
}