title: Rotate shapes to follow cursor in wpf
body: <p>I've got the majority of this working. However the problem is occurring somewhere in my math calculations. I want the Arrows(user controls) to rotate to point towards the cursor as it's dragging around the canvas 'on click' in wpf. 
  I've figured out how to calculate the angle in radians. However when i apply the value it doesn't appear to work as expected.</p>
  
  <p><strong>Current</strong></p>
  
  <p><a href="https://i.stack.imgur.com/WMJKe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WMJKe.png" alt="enter image description here"></a></p>
  
  <p><strong>Goal</strong></p>
  
  <p><a href="https://i.stack.imgur.com/pcsgr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pcsgr.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/xObUy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xObUy.png" alt="enter image description here"></a></p>
  
  <p>The main snippet of code in focus is this...</p>
  
  <pre><code>private double Angle(Point origin, Point target)
          {
              //Calculate the distance from the square to the mouse's X and Y position
              var radians = Math.Atan2(origin.Y - target.Y, origin.X - target.X);
              var degrees = radians * (180 / Math.PI) - 90;
  
              Console.WriteLine(target + "--" + origin + "--" + degrees);
              return degrees;
          }
  
          private void Canvas_MouseMove(object sender, MouseEventArgs e)
          {                                     
              var canvas = (Canvas)sender;
  
              if (e.LeftButton == MouseButtonState.Pressed)
              {
                  if (_followMouse)
                  {
                      // Get Cursor Position
                      Point _targetPoint = e.GetPosition(this);
  
                      // Follow mouse
                      foreach (UIElement element in canvas.Children)
                      {                        
                          Arrow arrow = (Arrow)element;
  
                          // example 1
                          double x = Canvas.GetTop(arrow) + arrow.ActualWidth / 2.0;
                          double y = Canvas.GetLeft(arrow) + arrow.ActualHeight / 2.0;
                          Point _originPoint = new Point(x,y);
  
                          arrow.rotateTransform.Angle = Angle(_originPoint, _targetPoint);         
                      }
                  }
              }
          }
  </code></pre>
  
  <hr>
  
  <p>Entire project code below...</p>
  
  <p><strong>MainWindow.xaml.cs</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  
  namespace WpfApplication1
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
          }
  
          bool _followMouse;
  
          private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
          {
              _followMouse = true;
          }
          private void Canvas_MouseUp(object sender, MouseButtonEventArgs e)
          {
              _followMouse = false;
          }
  
          private double Angle(Point origin, Point target)
          {
              //Calculate the distance from the square to the mouse's X and Y position
              var radians = Math.Atan2(origin.Y - target.Y, origin.X - target.X);
              var degrees = radians * (180 / Math.PI) - 90;
  
              Console.WriteLine(target + "--" + origin + "--" + degrees);
              return degrees;
          }
  
          private void Canvas_MouseMove(object sender, MouseEventArgs e)
          {                                     
              var canvas = (Canvas)sender;
  
              if (e.LeftButton == MouseButtonState.Pressed)
              {
                  if (_followMouse)
                  {
                      // Get Cursor Position
                      Point _targetPoint = e.GetPosition(this);
  
                      // Follow mouse
                      foreach (UIElement element in canvas.Children)
                      {                        
                          Arrow arrow = (Arrow)element;
  
                          // example 1
                          double x = Canvas.GetTop(arrow) + arrow.ActualWidth / 2.0;
                          double y = Canvas.GetLeft(arrow) + arrow.ActualHeight / 2.0;
                          Point _originPoint = new Point(x,y);
  
                          arrow.rotateTransform.Angle = Angle(_originPoint, _targetPoint);         
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>MainWindow.xaml</strong></p>
  
  <pre><code>&lt;Window x:Class="WpfApplication1.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
          xmlns:local="clr-namespace:WpfApplication1"
          mc:Ignorable="d"
          Title="MainWindow" Height="300" Width="300"
          WindowStartupLocation="CenterScreen"&gt;
  
      &lt;Canvas
          MouseDown="Canvas_MouseDown" 
          MouseUp="Canvas_MouseUp"
          MouseMove="Canvas_MouseMove"
          Background="LightBlue"&gt;
          &lt;local:Arrow Canvas.Left="158" Canvas.Top="43"/&gt;
          &lt;local:Arrow Canvas.Left="38" Canvas.Top="108"/&gt;
          &lt;local:Arrow Canvas.Left="158" Canvas.Top="170"/&gt;
          &lt;local:Arrow Canvas.Left="78" Canvas.Top="158"/&gt;
          &lt;local:Arrow Canvas.Left="196" Canvas.Top="108"/&gt;
          &lt;local:Arrow Canvas.Left="78" Canvas.Top="53"/&gt;
      &lt;/Canvas&gt;
  
  &lt;/Window&gt;
  </code></pre>
  
  <p><strong>Arrow.xaml</strong></p>
  
  <pre><code>&lt;UserControl x:Class="WpfApplication1.Arrow"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
               xmlns:local="clr-namespace:WpfApplication1"
               mc:Ignorable="d" 
               d:DesignHeight="50" d:DesignWidth="50"&gt;
      &lt;Grid&gt;
          &lt;Path Data="M 0 4 L 4 0 L 8 4 Z" RenderTransformOrigin="0.5,0.5"
                Width="50"
                Height="50"
                Stretch="Uniform"
                Fill="Red"&gt;
              &lt;Path.RenderTransform&gt;
                  &lt;RotateTransform x:Name="rotateTransform"/&gt;
              &lt;/Path.RenderTransform&gt;
  
          &lt;/Path&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>