已知两个坐标点(旋转前后的点),和它们的旋转角度,求它的旋转中心。
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;
}
}
}
总结:可以输入旋转前后的点和旋转角度,直接计算出旋转中心。