From c9681cec8a20e3263f07770bdd9c51fda2d7e8de Mon Sep 17 00:00:00 2001 From: Nathanael Jourdane Date: Wed, 12 Aug 2020 17:25:43 +0200 Subject: [PATCH] Add Anchor.Opposite() func --- geometry.go | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/geometry.go b/geometry.go index 05c4937e..6e8b0c04 100644 --- a/geometry.go +++ b/geometry.go @@ -553,13 +553,13 @@ type Anchor Vec var ( Center = Anchor{0.5, 0.5} Top = Anchor{0.5, 0} - TopRight = Anchor{0, 0} - Right = Anchor{0, 0.5} - BottomRight = Anchor{0, 1} + TopRight = Anchor{0, 0} + Right = Anchor{0, 0.5} + BottomRight = Anchor{0, 1} Bottom = Anchor{0.5, 1} - BottomLeft = Anchor{1, 1} - Left = Anchor{1, 0.5} - TopLeft = Anchor{1, 0} + BottomLeft = Anchor{1, 1} + Left = Anchor{1, 0.5} + TopLeft = Anchor{1, 0} ) var anchorStrings map[Anchor]string = map[Anchor]string{ @@ -579,6 +579,23 @@ func (anchor Anchor) String() string { return anchorStrings[anchor] } +var oppositeAnchors map[Anchor]Anchor = map[Anchor]Anchor{ + Center: Center, + Top: Bottom, + Bottom: Top, + Right: Left, + Left: Right, + TopRight: BottomLeft, + BottomLeft: TopRight, + BottomRight: TopLeft, + TopLeft: BottomRight, +} + +// Opposite returns the opposite position of the anchor (ie. Top -> Bottom; BottomLeft -> TopRight, etc.). +func (anchor Anchor) Opposite() Anchor { + return oppositeAnchors[anchor] +} + // AnchorPos returns the relative position of the given anchor. func (r Rect) AnchorPos(anchor Anchor) Vec { return r.Size().ScaledXY(V(0, 0).Sub(Vec(anchor)))