Defined Rectangle in package.
- Instead of deriving from image.Rectangle. Added methods on Rectangle.
This commit is contained in:
parent
f2705fcdf8
commit
ac8c2029e4
49
rectangle.go
49
rectangle.go
@ -1,13 +1,48 @@
|
||||
package geom
|
||||
|
||||
import (
|
||||
_image "image"
|
||||
)
|
||||
|
||||
// Rectangle is exposing the standard library image.Rectangle in the geom package.
|
||||
type Rectangle _image.Rectangle
|
||||
// Rectangle represents a 2-dimensional surface with as boundaries Min (inclusive) and Max (exclusive).
|
||||
type Rectangle struct {
|
||||
Min, Max Point
|
||||
}
|
||||
|
||||
// Rect is a shorthand function to create a rectangle.
|
||||
func Rect(x0, y0, x1, y1 int) Rectangle {
|
||||
return Rectangle(_image.Rect(x0, y0, x1, y1))
|
||||
if x0 > x1 {
|
||||
x0, x1 = x1, x0
|
||||
}
|
||||
if y0 > y1 {
|
||||
y0, y1 = y1, y0
|
||||
}
|
||||
return Rectangle{Point{x0, y0}, Point{x1, y1}}
|
||||
}
|
||||
|
||||
// Add translates rectangle r by point p.
|
||||
func (r Rectangle) Add(p Point) Rectangle {
|
||||
return Rectangle{
|
||||
Point{r.Min.X + p.X, r.Min.Y + p.Y},
|
||||
Point{r.Max.X + p.X, r.Max.Y + p.Y},
|
||||
}
|
||||
}
|
||||
|
||||
// Dx returns the width of r.
|
||||
func (r Rectangle) Dx() int {
|
||||
return r.Max.X - r.Min.X
|
||||
}
|
||||
|
||||
// Dy returns the height of r.
|
||||
func (r Rectangle) Dy() int {
|
||||
return r.Max.Y - r.Min.Y
|
||||
}
|
||||
|
||||
// Size returns the size of the rectangle.
|
||||
func (r Rectangle) Size() Point {
|
||||
return Point{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 Rectangle) Sub(p Point) Rectangle {
|
||||
return Rectangle{
|
||||
Point{r.Min.X - p.X, r.Min.Y - p.Y},
|
||||
Point{r.Max.X - p.X, r.Max.Y - p.Y},
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user