一、标定旋转中心的操作步骤
如下图,在旋转轴工具上放置一个Mark点,旋转角度,获得三个点,三点拟合圆,获得圆心坐标和旋转半径(默认已经做完九点标定,这三个点转为世界坐标后再计算圆心坐标和旋转半径)
二、三点拟合圆心
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 求圆心
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
double[] xy = new double[3];
xy = RotateCenter(double.Parse(txtX1.Text),double.Parse(txtY1.Text),double.Parse(txtX2.Text),double.Parse(txtY2.Text),double.Parse(txtX3.Text),double.Parse(txtY3.Text));
txtxcen.Text = xy[0].ToString();//X
txtycen.Text = xy[1].ToString();//Y
txtrcen.Text = xy[2].ToString();//R
}
public double[] RotateCenter(double x1,double y1,double x2,double y2,double x3,double y3)
{
double a, b, c, d, e, f;
a = 2 * (x2-x1);
b = 2 * (y2-y1);
c = x2 * x2 + y2 * y2 - x1 * x1 - y1 * y1;
d = 2 * (x3 - x2);
e = 2 * (y3 - y2);
f = x3 * x3 + y3 * y3 - x2 * x2 - y2 * y2;
double x = (b * f - e * c) / (b*d-e*a);
double y = (d*c-a*f) / (b*d-e*a);
double r = Math.Sqrt((x1 - x) * (x1 - x) + (y1 - x) * (y1 - x));
double[] xyr = new double[3];
xyr[0] = x;
xyr[1] = y;
xyr[2] = r;
return xyr;
}
}
}
三、使用旋转中心计算偏移量原理
已知:旋转中心坐标和旋转半径R
设:
因为
三角形BAC 和 三角形 OAD是相似三角形
所以
求解上面方程:
四、 计算一点绕另一点旋转一定角度后新点的坐标
- 第4节主要是为了讲清楚这个推导过程,接下来我们直接给出代码:
- 博客链接:C# 计算一点绕另一点旋转一定角度后新点的坐标_c# 计算一条直线旋转30度后的起点和终点坐标_MechMaster的博客-CSDN博客
using System; namespace RotatePoint { class Program { static void Main(string[] args) { double X = 0; double Y = 0; //顺时针角度为负数 RotateAngle(1, 1, -45, 2, 2, ref X, ref Y); Console.WriteLine(X+","+Y); Console.ReadKey(); } public static string RotateAngle(double XRotation, double YRotation, double ARotate, double XBefore, double YBefore, ref double XAfter, ref double YAfter) { try { double Rad = 0; Rad = ARotate * Math.Acos(-1) / 180; XAfter = (XBefore - XRotation) * Math.Cos(Rad) - (YBefore - YRotation) * Math.Sin(Rad) + XRotation; YAfter = (YBefore - YRotation) * Math.Cos(Rad) + (XBefore - XRotation) * Math.Sin(Rad) + YRotation; return "OK"; } catch (Exception ex) { return ex.Message; } } } }
五、X,Y和角度纠偏的步骤:
- X,Y和角度的偏差,在机械动作上是同时进行纠偏的,但是在算法中却不是,我们往往是先转正角度,然后算出转正角度后新位置的坐标;
- 此时新位置和模板角度是相同的,只需要平移就能重合,这个平移量就是9点标定时算出并保存的仿射变换矩阵来计算的了。