mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +00:00
f32,gpu,op/clip: add f32.Rectangle method for converting to image.Rectangle
Creating an image.Rectangle from a f32.Rectangle is used by two packages in Gio and about to be used for a third. Add a Round method to f32.Rectangle to avoid duplicating the implementation. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+28
-1
@@ -9,7 +9,11 @@ corner with the axes extending right and down.
|
||||
*/
|
||||
package f32
|
||||
|
||||
import "strconv"
|
||||
import (
|
||||
"image"
|
||||
"math"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// A Point is a two dimensional point.
|
||||
type Point struct {
|
||||
@@ -167,3 +171,26 @@ func (r Rectangle) Sub(p Point) Rectangle {
|
||||
Point{r.Max.X - p.X, r.Max.Y - p.Y},
|
||||
}
|
||||
}
|
||||
|
||||
// Round returns the smallest integer rectangle that
|
||||
// contains r.
|
||||
func (r Rectangle) Round() image.Rectangle {
|
||||
return image.Rectangle{
|
||||
Min: image.Point{
|
||||
X: int(floor(r.Min.X)),
|
||||
Y: int(floor(r.Min.Y)),
|
||||
},
|
||||
Max: image.Point{
|
||||
X: int(ceil(r.Max.X)),
|
||||
Y: int(ceil(r.Max.Y)),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func ceil(v float32) int {
|
||||
return int(math.Ceil(float64(v)))
|
||||
}
|
||||
|
||||
func floor(v float32) int {
|
||||
return int(math.Floor(float64(v)))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user