WPF 極座標簡單應用

在圓形佈局中說過極座標。工具

極座標是長度和邊與極軸之間的角度的座標表示。佈局

換句話說,只要知道角度和長度(與中心點的距離),咱們就能求出這一點的座標,相對的咱們知道這個一點的XY座標也能求出角度和長度。動畫

極座標的工具性真的很強,在繪圖,動畫上 有很大的幫助,計算過程要簡單很多。spa

下面我給出一個簡單的小栗子code

截圖:blog

 

using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace 極座標綜合應用
{
    /// <summary>
    /// MainWindow.xaml 的交互邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private Line DrawLine;
        private bool IsDraw;
 
        private Line SetBaseLine() => new Line() {Stroke=new SolidColorBrush(Colors.Red),StrokeThickness=1.1 };
        private void C1_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if(e.LeftButton==MouseButtonState.Pressed)
            {
                c1.Children.Clear();
                IsDraw = true;
               
                DrawLine = SetBaseLine();
                DrawLine.X1 = e.GetPosition(c1).X;
                DrawLine.Y1 = e.GetPosition(c1).Y;
                DrawLine.X2 = e.GetPosition(c1).X;
                DrawLine.Y2 = e.GetPosition(c1).Y;
                c1.Children.Add(DrawLine);
            }
        }

        private void C1_MouseMove(object sender, MouseEventArgs e)
        {
            if(e.LeftButton==MouseButtonState.Pressed)
            if(IsDraw==true)
            {
                DrawLine.X2 = e.GetPosition(c1).X;
                DrawLine.Y2 = e.GetPosition(c1).Y;
            }
        }

        private void C1_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Released)
            {
                IsDraw = false;
                CoodSystem();
            }
        }

        private void CoodSystem()
        {
            var startpoint = new Point(DrawLine.X1, DrawLine.Y1);
            var enpoint = new Point(DrawLine.X2, DrawLine.Y2);
            //轉換爲笛卡爾座標
            var sub = Point.Subtract(enpoint, startpoint);
            var x = sub.X;
            var y = sub.Y;
            //轉換公式
            sub = new Vector(x, 0 - y);
            //求出tan
            var k = y / x;
            //轉換角度
            var angle = 180 / Math.PI * Math.Atan2(y, x);

            t1.Text = $"{(angle<=0?Math.Abs(angle):360-angle)}度";
        }

        double ToRandion(double angle) => angle * (Math.PI / 180);

        void Create(Line l1, double angle,double len)
        {
            //轉極座標公式求X值
            var x = len * Math.Cos(ToRandion(angle));
            //轉極座標公式求Y值
            var y = len * Math.Sin(ToRandion(angle));

            //笛卡爾轉屏幕座標
            x =l1.X1 + x;
            y = l1.Y1 - y;

            l1.X2 = x;
            l1.Y2 = y;
        }

        Point GetMid() => new Point(c1.ActualWidth / 2, c1.ActualHeight / 2);

        private void T2_KeyDown(object sender, KeyEventArgs e)
        {
            var x =Convert.ToDouble( xt.Text);
            var y = Convert.ToDouble(yt.Text);
            var len = Convert.ToDouble(lt.Text);
            c1.Children.Clear();
            DrawLine = SetBaseLine();
            DrawLine.X1 = x;
            DrawLine.Y1 = y;
            Create(DrawLine, Convert.ToDouble(t2.Text),len);
            c1.Children.Add(DrawLine);
        }
    }
}
相關文章
相關標籤/搜索