2024-08-07 13:57:29 +02:00
|
|
|
package imageext
|
|
|
|
|
2024-08-07 14:00:02 +02:00
|
|
|
import "image"
|
|
|
|
|
2024-08-07 13:57:29 +02:00
|
|
|
type Rectangle struct {
|
|
|
|
X float64
|
|
|
|
Y float64
|
|
|
|
W float64
|
|
|
|
H float64
|
|
|
|
}
|
|
|
|
|
|
|
|
type PercentageRectangle struct {
|
|
|
|
X float64 // [0..1]
|
|
|
|
Y float64 // [0..1]
|
|
|
|
W float64 // [0..1]
|
|
|
|
H float64 // [0..1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r PercentageRectangle) Of(ref Rectangle) Rectangle {
|
|
|
|
return Rectangle{
|
|
|
|
X: ref.X + r.X*ref.W,
|
|
|
|
Y: ref.Y + r.Y*ref.H,
|
|
|
|
W: r.W * ref.W,
|
|
|
|
H: r.H * ref.H,
|
|
|
|
}
|
|
|
|
}
|
2024-08-07 14:00:02 +02:00
|
|
|
|
|
|
|
func calcRelativeRect(inner image.Rectangle, outer image.Rectangle) PercentageRectangle {
|
|
|
|
return PercentageRectangle{
|
|
|
|
X: float64(inner.Min.X-outer.Min.X) / float64(outer.Dx()),
|
|
|
|
Y: float64(inner.Min.Y-outer.Min.Y) / float64(outer.Dy()),
|
|
|
|
W: float64(inner.Dx()) / float64(outer.Dx()),
|
|
|
|
H: float64(inner.Dy()) / float64(outer.Dy()),
|
|
|
|
}
|
|
|
|
}
|