From afb30dc1e6908d135a631a2dfc3f5d63023c8fe3 Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Wed, 6 Mar 2019 19:24:03 +0100 Subject: [PATCH] Added Sub on RectangleF{,32}. Added Invert on PointF{,32}. Changed formula of Center (PointF{,32}). --- pointf.go | 5 +++++ pointf32.go | 5 +++++ rectanglef.go | 10 +++++++++- rectanglef32.go | 10 +++++++++- 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/pointf.go b/pointf.go index fc42698..c7b91b1 100644 --- a/pointf.go +++ b/pointf.go @@ -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} diff --git a/pointf32.go b/pointf32.go index 24c39e9..d66e3a2 100644 --- a/pointf32.go +++ b/pointf32.go @@ -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} diff --git a/rectanglef.go b/rectanglef.go index 2bc003f..41b2248 100644 --- a/rectanglef.go +++ b/rectanglef.go @@ -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}, + } +} diff --git a/rectanglef32.go b/rectanglef32.go index 698737c..fab729a 100644 --- a/rectanglef32.go +++ b/rectanglef32.go @@ -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}, + } +}