旋转中心计算

已知两个坐标点(旋转前后的点),和它们的旋转角度,求它的旋转中心。

winfrom窗体,如图:

 窗体代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test
{
    public partial class Form7 : Form
    {
        public Form7()
        {
            InitializeComponent();
        }

        private void Form7_Load(object sender, EventArgs e)
        {
            int[] dropNum = new[] {10,20,30,50,60 };
            for (int i=0;i<dropNum.Length;i++)
            {
                cboDeg.Items.Add(dropNum[i]);
            }
        }

        private void btnAttach1_Click(object sender, EventArgs e)
        {
            txtPiont1.Text = Clipboard.GetText();
        }

        private void btnAttach2_Click(object sender, EventArgs e)
        {
            txtPoint2.Text = Clipboard.GetText();
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(txtRotate.Text);
        }

        private void btnCal_Click(object sender, EventArgs e)
        {
            try
            {
                var p1 = new SvPoint(txtPiont1.Text.Split(',')[0].ToDouble(), txtPiont1.Text.Split(',')[1].ToDouble());
                var p2 = new SvPoint(txtPoint2.Text.Split(',')[0].ToDouble(), txtPoint2.Text.Split(',')[1].ToDouble());
                var angle = cboDeg.Text.ToDouble();
                if (p1.IsInitialized == false || p2.IsInitialized == false || angle == 0)
                {
                    throw new Exception();
                }
                var rotate = GetRotateCenter(p1, p2, angle);
                txtRotate.Text = $"{rotate.Row:0},{rotate.Column:0}";
            }
            catch (Exception ex)
            {
               ShowError("請輸入正確的點位", ex);
            }
        }
        /// <summary>
        /// 兩點自由旋轉角度求旋轉中心
        /// </summary>
        /// <param name="p1">旋轉前點</param>
        /// <param name="p2">旋轉後點</param>
        /// <param name="angle">逆時針旋轉角度</param>
        /// <returns></returns>
        public static SvPoint GetRotateCenter(SvPoint p1, SvPoint p2, double angle)
        {
            try
            {
                //轉換為數學坐標系
                double x1 = p1.Column;
                double y1 = -p1.Row;
                double x2 = p2.Column;
                double y2 = -p2.Row;
                double a = (angle / 180) * Math.PI;

                //d為p1與p2的距離,r為旋轉軸半徑,(xt,yt)為虛擬延伸點
                double d = Math.Pow(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2), 0.5);
                double r = (d / 2) / Math.Sin(a / 2);
                double xt = (1 - r / d) * x1 + (r / d) * x2;
                double yt = (1 - r / d) * y1 + (r / d) * y2;
                double b = ((90.0 / 180) * Math.PI) - (a / 2);
                double x0 = Math.Cos(b) * (xt - x1) - Math.Sin(b) * (yt - y1) + x1;
                double y0 = Math.Cos(b) * (yt - y1) + Math.Sin(b) * (xt - x1) + y1;

                SvPoint p0 = new SvPoint(Math.Round(-y0, 3), Math.Round(x0, 3));
                return p0;
            }
            catch (Exception)
            {
                return new SvPoint(-999, -999);
            }
        }
        private List<SvLogInfo> BufferSave = new List<SvLogInfo>();
        private List<SvLogInfo> BufferShow = new List<SvLogInfo>();
        public void ShowError(string msg, Exception ex)
        {
            var logInfo = new SvLogInfo();
            logInfo.CreateTime = DateTime.Now;
            logInfo.Category = SvLogInfo.Categorys.ERROR;
            logInfo.Message = msg + "," + ex.Message;
            logInfo.StackTrace = ex.StackTrace;
            lock (BufferSave)
            {
                BufferSave.Add(logInfo);
            }
            lock (BufferShow)
            {
                BufferShow.Add(logInfo);
            }
            MessageBox.Show(logInfo.Message, "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

所用类:

SvPoint类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
    public class SvPoint
    {
        public double Row = -999;
        public double Column = -999;

        public SvPoint()
        {
        }

        public SvPoint(double _Row, double _Column)
        {
            Row = _Row;
            Column = _Column;
        }

        public bool IsInitialized
        {
            get
            {
                if (Row == -999 & Column == -999)
                    return false;
                else
                    return true;
            }
        }

        public override string ToString()
        {
            return $"{Row:0.000},{Column:0.000}";
        }
    }
}

SvLogInfo类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
    public class SvLogInfo
    {
        public enum Categorys
        {
            INFO,
            ERROR,
        }

        public DateTime CreateTime { get; set; }
        public Categorys Category { get; set; }
        public string Message { get; set; }
        public string StackTrace { get; set; }
    }
}

SvExtension类(静态类)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
    public static class SvExtension
    {
        public static double ToDouble(this string source)
        {
            return double.TryParse(source, out double result) ? result : 0d;
        }
    }
}

总结:可以输入旋转前后的点和旋转角度,直接计算出旋转中心。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

susan花雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值