From 885f27689fb49f610e0e5d2e0cc33b6c76ed0147 Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Mon, 15 Feb 2021 19:39:39 +0100 Subject: [PATCH] Added Union for Rectangle{,F,F32}. --- rectangle.go | 7 +++++++ rectanglef.go | 5 +++++ rectanglef32.go | 5 +++++ 3 files changed, 17 insertions(+) diff --git a/rectangle.go b/rectangle.go index 5af858a..6f5136b 100644 --- a/rectangle.go +++ b/rectangle.go @@ -1,5 +1,7 @@ package geom +import "opslag.de/schobers/geom/ints" + // Rectangle represents a 2-dimensional surface with as boundaries Min (inclusive) and Max (exclusive). type Rectangle struct { Min, Max Point @@ -57,3 +59,8 @@ func (r Rectangle) ToF() RectangleF { return RectangleF{Min: r.Min.ToF(), Max: r // ToF32 returns the floating point representation of r. func (r Rectangle) ToF32() RectangleF32 { return RectangleF32{Min: r.Min.ToF32(), Max: r.Max.ToF32()} } + +// Union gives back the union of the two rectangles. +func (r Rectangle) Union(s Rectangle) Rectangle { + return Rect(ints.Min(r.Min.X, s.Min.X), ints.Min(r.Min.Y, s.Min.Y), ints.Max(r.Max.X, s.Max.X), ints.Max(r.Max.Y, s.Max.Y)) +} diff --git a/rectanglef.go b/rectanglef.go index 58d754f..807fb83 100644 --- a/rectanglef.go +++ b/rectanglef.go @@ -83,3 +83,8 @@ func (r RectangleF) ToF32() RectangleF32 { return RectangleF32{Min: r.Min.ToF32( // ToInt returns the integer point representation of r. func (r RectangleF) ToInt() Rectangle { return Rectangle{Min: r.Min.ToInt(), Max: r.Max.ToInt()} } + +// Union gives back the union of the two rectangles. +func (r RectangleF) Union(s RectangleF) RectangleF { + return RectF(Min(r.Min.X, s.Min.X), Min(r.Min.Y, s.Min.Y), Max(r.Max.X, s.Max.X), Max(r.Max.Y, s.Max.Y)) +} diff --git a/rectanglef32.go b/rectanglef32.go index a948d43..51c81a8 100644 --- a/rectanglef32.go +++ b/rectanglef32.go @@ -83,3 +83,8 @@ func (r RectangleF32) ToInt() Rectangle { return Rectangle{Min: r.Min.ToInt(), M // ToF returns the floating point representation of r. func (r RectangleF32) ToF() RectangleF { return RectangleF{Min: r.Min.ToF(), Max: r.Max.ToF()} } + +// Union gives back the union of the two rectangles. +func (r RectangleF32) Union(s RectangleF32) RectangleF32 { + return RectF32(Min32(r.Min.X, s.Min.X), Min32(r.Min.Y, s.Min.Y), Max32(r.Max.X, s.Max.X), Max32(r.Max.Y, s.Max.Y)) +}