From 18bac571b88eee0c6b674150012950a93565c433 Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Sat, 16 May 2020 19:52:05 +0200 Subject: [PATCH] Added PointF{,32}.ToInt that converts the point to a Point. Added Point.In(Rectangle). --- point.go | 8 ++++++++ pointf.go | 5 +++++ pointf32.go | 5 +++++ 3 files changed, 18 insertions(+) diff --git a/point.go b/point.go index a88a43f..965f86a 100644 --- a/point.go +++ b/point.go @@ -64,6 +64,14 @@ func (p Point) DistInt(q Point) int { return ints.SubAbs(p.X, q.X) + ints.SubAbs(p.Y, q.Y) } +// In tests if the point p is inside the rectangle r. +func (p Point) In(r Rectangle) bool { + if p.X < r.Min.X || p.X >= r.Max.X || p.Y < r.Min.Y || p.Y >= r.Max.Y { + return false + } + return true +} + // Less returns true if q is above (Y is smaller) or left (X is smaller) than p. Otherwise returns false. func (p Point) Less(q Point) bool { if p.Y == q.Y { diff --git a/pointf.go b/pointf.go index 3808e65..0a8d206 100644 --- a/pointf.go +++ b/pointf.go @@ -147,6 +147,11 @@ func (p PointF) To32() PointF32 { return PointF32{float32(p.X), float32(p.Y)} } +// ToInt transforms the point p into a Point. +func (p PointF) ToInt() Point { + return Point{int(p.X), int(p.Y)} +} + // MaxPtF returns the point that is at the largest X & Y position of a and b. func MaxPtF(a, b PointF) PointF { return PtF(Max(a.X, b.X), Max(a.Y, b.Y)) diff --git a/pointf32.go b/pointf32.go index 2f40260..7777961 100644 --- a/pointf32.go +++ b/pointf32.go @@ -147,6 +147,11 @@ func (p PointF32) To64() PointF { return PointF{float64(p.X), float64(p.Y)} } +// ToInt transforms the point p into a Point. +func (p PointF32) ToInt() Point { + return Point{int(p.X), int(p.Y)} +} + // MaxPtF32 returns the point that is at the largest X & Y position of a and b. func MaxPtF32(a, b PointF32) PointF32 { return PtF32(Max32(a.X, b.X), Max32(a.Y, b.Y))