title: Snap to Edge Effect
body: <p><img src="https://i.stack.imgur.com/eVdud.gif" alt="enter image description here"></p>
  
  <p>My final goal is to have a method, lets say:</p>
  
  <pre><code>Rectangle snapRects(Rectangle rec1, Rectangle rec2);
  </code></pre>
  
  <p>Imagine a <code>Rectangle</code> having info on <code>position</code>, <code>size</code> and <code>angle</code>.</p>
  
  <p>Dragging and dropping the ABDE rectangle close to the BCGF rectangle would call the method with ABDE as first argument and BCGF as second argument, and the resulting rectangle is a rectangle lined up with BCGF's edge.</p>
  
  <p>The vertices do not have to match (and preferrably won't so the snapping isn't so restrictive).</p>
  
  <p>I can only understand easily how to give the same angle, but the position change is quite confusing to me. Also, i believe even if i reached a solution it would be quite badly optimized (excessive resource cost), so I would appreciate guidance on this.</p>
  
  <p>(This has already been asked but no satisfatory answer was given and the question forgotten.)</p>
  
  <pre><code>------------------------------------------------------------------
  </code></pre>
  
  <p>Edit: It seems my explanation was insufficient so I will try to clarify my wishes:</p>
  
  <p>The following image shows the goal of the method in a nutshell:</p>
  
  <p><img src="https://i.stack.imgur.com/qFXhz.png" alt="enter image description here"></p>
  
  <p>Forget about "closest rectangle", imagine there are just two rectangles. The lines inside the rectangles represent the direction they are facing (visual aid for the angle).</p>
  
  <p>There is a static rectangle, which is not to be moved and has an angle (0->360), and a rectangle (also with an angle) which I want to Snap to the <strong>closest edge</strong> of the static rectangle. By this, i mean, i want the <strong>least transformations possible</strong> for the "snap to edge" to happen.</p>
  
  <p>This brings many possible cases, depending on the rotation of the rectangles and their position relative to each other.</p>
  
  <p>The next image shows the static rectangle and how the position of the "To Snap" rectangle changes the snapping result:</p>
  
  <p><img src="https://i.stack.imgur.com/IpNdJ.png" alt="enter image description here"></p>
  
  <p>The final rotations might not be perfect since it was done by eye, but you get the point, it matters the relative position and also both angles.</p>
  
  <p>Now, in my point of view, which may be completely naive, I see this problem solved on two important and distinct steps on transforming the "To Snap" rectangle: <strong>Positioning</strong> and <strong>Rotation</strong></p>
  
  <p><strong>Position</strong>: The objective of the new position is to stick to the closest edge, but since we want it to stick paralell to the static rectangle, the angle of the static rectangle matters. The next image shows examples of positioning:</p>
  
  <p><img src="https://i.stack.imgur.com/5FU7X.png" alt="enter image description here"></p>
  
  <p>In this case, the static rectangle has no angle, so its easy to determine up, down, left and right. But with angle, there are alot more possibilities:</p>
  
  <p><img src="https://i.stack.imgur.com/dusYI.png" alt="enter image description here"></p>
  
  <p>As for the rotation, the goal is for the "to snap" rectangle to rotate <strong>the minimum</strong> needed to become paralell with the static rectangle:</p>
  
  <p><img src="https://i.stack.imgur.com/0Ya8S.png" alt="enter image description here"></p>
  
  <p>As a final note, in regard of implementation input, the goal is to actually drag the "to snap" rectangle to wherever position i wish around the static rectangle and by pressing a keyboard key, the snap happens.</p>
  
  <p>Also, it appears i have exagerated a little when i asked for optimization, to be honest i do not need or require optimization, I do prefer an easy to read, step by step clear code (if its the case), rather than any optimization at all.</p>
  
  <p>I hope i was clear this time, sorry for the lack of clarity in the first place, if you have any more doubts please do ask.</p>