Added Rect{,2D,Rel,Rel2D} methods on Point{,F,F32} to create a rectangle with the point as minimum.

This commit is contained in:
Sander Schobers 2020-05-17 11:21:41 +02:00
parent c81df8279f
commit 82f27a2b4a
3 changed files with 60 additions and 0 deletions

View File

@ -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)

View File

@ -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}

View File

@ -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}