WPF 极坐标简单应用

导读:本篇文章讲解 WPF 极坐标简单应用,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

在圆形布局中说过极坐标。

极坐标是长度和边与极轴之间的角度的坐标表示。

换句话说,只要知道角度和长度(与中心点的距离),我们就能求出这一点的坐标,相对的我们知道这个一点的XY坐标也能求出角度和长度。

极坐标的工具性真的很强,在绘图,动画上 有很大的帮助,计算过程要简单不少。

下面我给出一个简单的小栗子

截图:

WPF 极坐标简单应用

 

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

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/12598.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!