### [Silverlight] 抓滑鼠座標並兩點計算角度

[Silverlight] 抓滑鼠座標並兩點計算角度

XAML Code :

<UserControl x:Class="CalcAngle.MainPage"    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"    mc:Ignorable="d"    d:DesignHeight="500" d:DesignWidth="500" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" >    <Grid x:Name="LayoutRoot" Background="Black" Width="500" Height="500" MouseMove="LayoutRoot_MouseMove">        <Ellipse Height="10" HorizontalAlignment="Left" Margin="245,245,0,0" Name="ellipse1" Stroke="Red" StrokeThickness="5" VerticalAlignment="Top" Width="10" />        <sdk:Label Height="28" HorizontalAlignment="Left" Margin="12,460,0,0" Name="lblLogAngle" VerticalAlignment="Top" Width="292" Foreground="White" Content="-" />        <sdk:Label Height="28" HorizontalAlignment="Left" Margin="196,12,0,0" Name="lblMousePosition" VerticalAlignment="Top" Width="292" Content="-" Foreground="White" />    </Grid></UserControl>

/// <summary>/// 計算 targetPoint 之於 soucePoint 的相對角度/// </summary>/// <param name="soucePoint">基準點</param>/// <param name="targetPoint">觀測點</param>/// <returns></returns>private double ConvertPositionAngel(Point soucePoint,Point targetPoint){  var res=(Math.Atan2(targetPoint.Y-soucePoint.Y,targetPoint.X-soucePoint.X)) / Math.PI * 180.0;  return (res>=0 && res <=180)?res+=90:((res<0 && res>=-90)? res+=90: res+=450);}

Javacript 可以參考這篇 [Javscript] 計算兩點角度

Code 如下:

void LayoutRoot_MouseMove(object sender, MouseEventArgs e){    this.lblMousePosition.Content = "你滑鼠於LayoutRoot座標是 (" + e.GetPosition(this.LayoutRoot).X + "," + e.GetPosition(this.LayoutRoot).Y + ")";    this.lblLogAngle.Content = "滑鼠對於中心點角度是" + ConvertPositionAngel(new Point(250, 250), e.GetPosition(this.LayoutRoot));}

---