From 82f27a2b4a21b7e321e3f09c4251fc648b8a95af Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Sun, 17 May 2020 11:21:41 +0200 Subject: [PATCH] Added Rect{,2D,Rel,Rel2D} methods on Point{,F,F32} to create a rectangle with the point as minimum. --- point.go | 20 ++++++++++++++++++++ pointf.go | 20 ++++++++++++++++++++ pointf32.go | 20 ++++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/point.go b/point.go index 965f86a..92ccc72 100644 --- a/point.go +++ b/point.go @@ -90,6 +90,26 @@ func (p Point) Norm() Point { return Pt(ints.Norm(p.X), ints.Norm(p.Y)) } +// Rect returns a rectangle starting from point p to given point q +func (p Point) Rect(q Point) Rectangle { + return Rectangle{Min: p, Max: q} +} + +// Rect2D returns a rectangle starting from point p to given coordinate (x, y) +func (p Point) Rect2D(x, y int) Rectangle { + return Rectangle{Min: p, Max: Pt(x, y)} +} + +// RectRel returns a rectangle starting from point p with the given size. +func (p Point) RectRel(size Point) Rectangle { + return RectRel(p.X, p.Y, size.X, size.Y) +} + +// RectRel2D returns a rectangle starting from point p with the given width and height +func (p Point) RectRel2D(width, height int) Rectangle { + return RectRel(p.X, p.Y, width, height) +} + // String returns a string representation of point p "X, Y". func (p Point) String() string { return fmt.Sprintf("%d, %d", p.X, p.Y) diff --git a/pointf.go b/pointf.go index 22b6090..5ee65d8 100644 --- a/pointf.go +++ b/pointf.go @@ -137,6 +137,26 @@ func (p PointF) Mul(t float64) PointF { return PtF(p.X*t, p.Y*t) } +// Rect returns a rectangle starting from point p to given point q +func (p PointF) Rect(q PointF) RectangleF { + return RectangleF{Min: p, Max: q} +} + +// Rect2D returns a rectangle starting from point p to given coordinate (x, y) +func (p PointF) Rect2D(x, y float64) RectangleF { + return RectangleF{Min: p, Max: PtF(x, y)} +} + +// RectRel returns a rectangle starting from point p with the given size. +func (p PointF) RectRel(size PointF) RectangleF { + return RectRelF(p.X, p.Y, size.X, size.Y) +} + +// RectRel2D returns a rectangle starting from point p with the given width and height +func (p PointF) RectRel2D(width, height float64) RectangleF { + return RectRelF(p.X, p.Y, width, height) +} + // 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 353e9d8..1e9c506 100644 --- a/pointf32.go +++ b/pointf32.go @@ -137,6 +137,26 @@ func (p PointF32) Mul(t float32) PointF32 { return PtF32(p.X*t, p.Y*t) } +// Rect returns a rectangle starting from point p to given point q +func (p PointF32) Rect(q PointF32) RectangleF32 { + return RectangleF32{Min: p, Max: q} +} + +// Rect2D returns a rectangle starting from point p to given coordinate (x, y) +func (p PointF32) Rect2D(x, y float32) RectangleF32 { + return RectangleF32{Min: p, Max: PtF32(x, y)} +} + +// RectRel returns a rectangle starting from point p with the given size. +func (p PointF32) RectRel(size PointF32) RectangleF32 { + return RectRelF32(p.X, p.Y, size.X, size.Y) +} + +// RectRel2D returns a rectangle starting from point p with the given width and height +func (p PointF32) RectRel2D(width, height float32) RectangleF32 { + return RectRelF32(p.X, p.Y, width, height) +} + // 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}