Add Matrix.Aligned()

This commit is contained in:
Nathanaël 2020-08-11 08:35:03 +02:00
parent 50e1d8c96b
commit d6663ba0a0
1 changed files with 13 additions and 6 deletions

View File

@ -589,15 +589,22 @@ func (anchorPos AnchorPos) String() string {
} }
// AnchorPos returns the position of the given anchor of the Rect. // AnchorPos returns the position of the given anchor of the Rect.
func (r *Rect) AnchorPos(anchor AnchorPos) Vec { func (r *Rect) AnchorPos(anchorPos AnchorPos) Vec {
return r.Min.Add(r.Size().ScaledXY(anchor.Val)) return r.Min.Add(r.Size().ScaledXY(anchorPos.Val))
} }
// AnchorTo updates the Rect position to align it on the given anchor. // AnchorTo updates the Rect position to align it on the given anchor.
func (r *Rect) AnchorTo(anchor AnchorPos) { func (rect *Rect) AnchorTo(anchorPos AnchorPos) {
size := r.Size() size := rect.Size()
r.Min = r.Min.Sub(size.ScaledXY(anchor.Val)) rect.Min = rect.Min.Sub(size.ScaledXY(anchorPos.Val))
r.Max = r.Max.Sub(size.ScaledXY(anchor.Val)) rect.Max = rect.Max.Sub(size.ScaledXY(anchorPos.Val))
}
// Aligned moves everything to align the given rectangle on the given anchor position.
func (m Matrix) Aligned(rect Rect, anchorPos AnchorPos) Matrix {
vect := rect.Size().ScaledXY(anchorPos.Val)
m[4], m[5] = m[4]-vect.X, m[5]-vect.Y
return m
} }
// Center returns the position of the center of the Rect. // Center returns the position of the center of the Rect.