WPF 極坐標簡單應用


在圓形布局中說過極坐標。

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

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

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

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

截圖:

 

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);
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM