From d6663ba0a0d7f6d0f8cbe2b9917ebe2fad4c23d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nathana=C3=ABl?= Date: Tue, 11 Aug 2020 08:35:03 +0200 Subject: [PATCH] Add Matrix.Aligned() --- geometry.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/geometry.go b/geometry.go index dd44120..e1ba2e5 100644 --- a/geometry.go +++ b/geometry.go @@ -589,15 +589,22 @@ func (anchorPos AnchorPos) String() string { } // AnchorPos returns the position of the given anchor of the Rect. -func (r *Rect) AnchorPos(anchor AnchorPos) Vec { - return r.Min.Add(r.Size().ScaledXY(anchor.Val)) +func (r *Rect) AnchorPos(anchorPos AnchorPos) Vec { + return r.Min.Add(r.Size().ScaledXY(anchorPos.Val)) } // AnchorTo updates the Rect position to align it on the given anchor. -func (r *Rect) AnchorTo(anchor AnchorPos) { - size := r.Size() - r.Min = r.Min.Sub(size.ScaledXY(anchor.Val)) - r.Max = r.Max.Sub(size.ScaledXY(anchor.Val)) +func (rect *Rect) AnchorTo(anchorPos AnchorPos) { + size := rect.Size() + rect.Min = rect.Min.Sub(size.ScaledXY(anchorPos.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.