Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF: Getting new coordinates after a Rotation
    primarykey
    data
    text
    <p><em>With reference to <a href="https://stackoverflow.com/questions/505040/developing-a-robocode-type-game-with-net-for-a-school-assignment">this programming game</a> I am currently building.</em></p> <p><a href="http://img12.imageshack.us/img12/2089/shapetransformationf.jpg" rel="nofollow noreferrer">alt text http://img12.imageshack.us/img12/2089/shapetransformationf.jpg</a></p> <p>To translate a Canvas in WPF, I am using two Forms: <code>TranslateTransform</code> (to move it), and <code>RotateTransform</code> (to rotate it) [<em>children of the same</em> <code>TransformationGroup</code>]</p> <p>I can easily get the top left x,y coordinates of a canvas when its not rotated (or rotated at 90deg, since it will be the same), but the problem I am facing is getting the top left (and the other 3 points) coordinates. </p> <p>This is because when a <code>RotateTransform</code> is applied, the <code>TranslateTransform</code>'s <code>X</code> and <code>Y</code> properties are not changed (and thus still indicate that the top-left of the square is like the dotted-square (from the image)</p> <p>The Canvas is being rotated from its center, so that is its origin.</p> <p>So <strong>how can I get the "new" x and y coordinates of the 4 points after a rotation</strong>? </p> <p><strong>[UPDATE]</strong></p> <p><a href="http://img25.imageshack.us/img25/8676/shaperotationaltransfor.jpg" rel="nofollow noreferrer">alt text http://img25.imageshack.us/img25/8676/shaperotationaltransfor.jpg</a></p> <p>I have found a way to find the <strong>top-left</strong> coordinates after a rotation (as you can see from the new image) by adding the OffsetX and OffsetY from the rotation to the starting X and Y coordinates.</p> <p>But I'm now having trouble figuring out the rest of the coordinates (the other 3).</p> <p>With this rotated shape, <strong>how can I figure out the x and y coordinates of the remaining 3 corners?</strong></p> <p><strong>[EDIT]</strong></p> <p>The points in the 2nd image <strong>ARE NOT ACCURATE AND EXACT POINTS</strong>. I made the points up with estimates in my head.</p> <p><strong>[UPDATE] Solution:</strong></p> <p>First of all, I would like to thank <a href="https://stackoverflow.com/questions/581837/wpf-getting-new-coordinates-after-a-rotation/591058#591058">Jason S</a> for that lengthy and Very informative post in which he describes the mathematics behind the whole process; I certainly learned a lot by reading your post and trying out the values.</p> <p>But I have now found a code snippet (thanks to <a href="https://stackoverflow.com/questions/581837/wpf-getting-new-coordinates-after-a-rotation/593552#593552">EugeneZ</a>'s mention of <code>TransformBounds</code>) that does exactly what I want:</p> <pre><code>public Rect GetBounds(FrameworkElement of, FrameworkElement from) { // Might throw an exception if of and from are not in the same visual tree GeneralTransform transform = of.TransformToVisual(from); return transform.TransformBounds(new Rect(0, 0, of.ActualWidth, of.ActualHeight)); } </code></pre> <p><em>Reference: <a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/86350f19-6457-470e-bde9-66e8970f7059/" rel="nofollow noreferrer">http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/86350f19-6457-470e-bde9-66e8970f7059/</a></em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload