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:
Elias Naur
2022-03-30 12:00:45 +02:00
parent 920e6dd004
commit 1f11a5a17b
4 changed files with 34 additions and 51 deletions
+28 -1
View File
@@ -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)))
}