From b166fe2ef6994d361206938db913b871b3607fe7 Mon Sep 17 00:00:00 2001 From: Jaex Date: Sat, 25 Jun 2016 10:44:39 +0300 Subject: [PATCH] Proper way to check snap distance --- .../Shapes/ShapeManager.cs | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/ShareX.ScreenCaptureLib/Shapes/ShapeManager.cs b/ShareX.ScreenCaptureLib/Shapes/ShapeManager.cs index a1d1462e8..6876a8093 100644 --- a/ShareX.ScreenCaptureLib/Shapes/ShapeManager.cs +++ b/ShareX.ScreenCaptureLib/Shapes/ShapeManager.cs @@ -1275,26 +1275,27 @@ private void OpenOptionsMenu() private Point SnapPosition(Point posOnClick, Point posCurrent) { - Rectangle currentRect = CaptureHelpers.CreateRectangle(posOnClick, posCurrent); - Point newPosition = posCurrent; + Size currentSize = CaptureHelpers.CreateRectangle(posOnClick, posCurrent).Size; + Vector2 vector = new Vector2(currentSize.Width, currentSize.Height); - foreach (SnapSize size in Config.SnapSizes) + SnapSize snapSize = (from size in Config.SnapSizes + let distance = MathHelpers.Distance(vector, new Vector2(size.Width, size.Height)) + where distance > 0 && distance < Config.SnapDistance + orderby distance + select size).FirstOrDefault(); + + if (snapSize != null) { - if (currentRect.Width.IsBetween(size.Width - Config.SnapDistance, size.Width + Config.SnapDistance) || - currentRect.Height.IsBetween(size.Height - Config.SnapDistance, size.Height + Config.SnapDistance)) + Point posNew = CaptureHelpers.CalculateNewPosition(posOnClick, posCurrent, snapSize); + + Rectangle newRect = CaptureHelpers.CreateRectangle(posOnClick, posNew); + + if (form.ScreenRectangle0Based.Contains(newRect)) { - newPosition = CaptureHelpers.CalculateNewPosition(posOnClick, posCurrent, size); - break; + return posNew; } } - Rectangle newRect = CaptureHelpers.CreateRectangle(posOnClick, newPosition); - - if (form.ScreenRectangle0Based.Contains(newRect)) - { - return newPosition; - } - return posCurrent; }