From 50e1d8c96bf77d07addc6c91d158b0a461959ddd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nathana=C3=ABl=20Jourdane?= Date: Mon, 10 Aug 2020 17:25:09 +0200 Subject: [PATCH] Add Rect.AnchorTo() --- geometry.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/geometry.go b/geometry.go index 75b414e..dd44120 100644 --- a/geometry.go +++ b/geometry.go @@ -548,7 +548,7 @@ func (r Rect) Edges() [4]Line { } // AnchorPos enumerates available anchor positions, such as `Center`, `Top`, `TopRight`, etc. -type AnchorPos struct{ v Vec } +type AnchorPos struct{ Val Vec } var ( Center = AnchorPos{V(0.5, 0.5)} @@ -590,8 +590,14 @@ func (anchorPos AnchorPos) String() string { // AnchorPos returns the position of the given anchor of the Rect. func (r *Rect) AnchorPos(anchor AnchorPos) Vec { - // func (u Vec) ScaledXY(v Vec) Vec { - return r.Min.Add(r.Size().ScaledXY(anchor.v)) + return r.Min.Add(r.Size().ScaledXY(anchor.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)) } // Center returns the position of the center of the Rect.