C#实现坐标系转换

已知坐标系的向量线段AB,旋转指定角度后平移到达坐标A'B'

获取旋转角度以及新的其他坐标转换。

新建窗体应用程序CoordinateTransDemo,将默认的Form1重命名为FormCoordinateTrans,窗体设计如图:

窗体设计代码如下:

部分类文件FormCoordinateTrans.Designer.cs


namespace CoordinateTransDemo
{partial class FormCoordinateTrans{/// <summary>/// 必需的设计器变量。/// </summary>private System.ComponentModel.IContainer components = null;/// <summary>/// 清理所有正在使用的资源。/// </summary>/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region Windows 窗体设计器生成的代码/// <summary>/// 设计器支持所需的方法 - 不要修改/// 使用代码编辑器修改此方法的内容。/// </summary>private void InitializeComponent(){System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle5 = new System.Windows.Forms.DataGridViewCellStyle();System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle6 = new System.Windows.Forms.DataGridViewCellStyle();this.rtxtMessage = new System.Windows.Forms.RichTextBox();this.groupBox2 = new System.Windows.Forms.GroupBox();this.txtCrd2Mark2_Y = new System.Windows.Forms.TextBox();this.label9 = new System.Windows.Forms.Label();this.label10 = new System.Windows.Forms.Label();this.txtCrd2Mark2_X = new System.Windows.Forms.TextBox();this.txtCrd2Mark1_Y = new System.Windows.Forms.TextBox();this.label11 = new System.Windows.Forms.Label();this.label12 = new System.Windows.Forms.Label();this.txtCrd2Mark1_X = new System.Windows.Forms.TextBox();this.groupBox1 = new System.Windows.Forms.GroupBox();this.txtCrd1Mark2_Y = new System.Windows.Forms.TextBox();this.label7 = new System.Windows.Forms.Label();this.label8 = new System.Windows.Forms.Label();this.txtCrd1Mark2_X = new System.Windows.Forms.TextBox();this.txtCrd1Mark1_Y = new System.Windows.Forms.TextBox();this.label6 = new System.Windows.Forms.Label();this.label5 = new System.Windows.Forms.Label();this.txtCrd1Mark1_X = new System.Windows.Forms.TextBox();this.dgvData = new System.Windows.Forms.DataGridView();this.btnTransform = new System.Windows.Forms.Button();this.dgvcPoleSequence = new System.Windows.Forms.DataGridViewTextBoxColumn();this.dgvcPoleX = new System.Windows.Forms.DataGridViewTextBoxColumn();this.dgvcPoleY = new System.Windows.Forms.DataGridViewTextBoxColumn();this.dgvcPoleAngle = new System.Windows.Forms.DataGridViewTextBoxColumn();this.dgvcTransformX = new System.Windows.Forms.DataGridViewTextBoxColumn();this.dgvcTransformY = new System.Windows.Forms.DataGridViewTextBoxColumn();this.groupBox2.SuspendLayout();this.groupBox1.SuspendLayout();((System.ComponentModel.ISupportInitialize)(this.dgvData)).BeginInit();this.SuspendLayout();// // rtxtMessage// this.rtxtMessage.Location = new System.Drawing.Point(727, 12);this.rtxtMessage.Name = "rtxtMessage";this.rtxtMessage.Size = new System.Drawing.Size(491, 520);this.rtxtMessage.TabIndex = 22;this.rtxtMessage.Text = "";// // groupBox2// this.groupBox2.Controls.Add(this.txtCrd2Mark2_Y);this.groupBox2.Controls.Add(this.label9);this.groupBox2.Controls.Add(this.label10);this.groupBox2.Controls.Add(this.txtCrd2Mark2_X);this.groupBox2.Controls.Add(this.txtCrd2Mark1_Y);this.groupBox2.Controls.Add(this.label11);this.groupBox2.Controls.Add(this.label12);this.groupBox2.Controls.Add(this.txtCrd2Mark1_X);this.groupBox2.Location = new System.Drawing.Point(255, 4);this.groupBox2.Name = "groupBox2";this.groupBox2.Size = new System.Drawing.Size(217, 120);this.groupBox2.TabIndex = 21;this.groupBox2.TabStop = false;this.groupBox2.Text = "焊接Mark点坐标";// // txtCrd2Mark2_Y// this.txtCrd2Mark2_Y.Location = new System.Drawing.Point(108, 95);this.txtCrd2Mark2_Y.Name = "txtCrd2Mark2_Y";this.txtCrd2Mark2_Y.Size = new System.Drawing.Size(93, 21);this.txtCrd2Mark2_Y.TabIndex = 16;this.txtCrd2Mark2_Y.Text = "320.5";// // label9// this.label9.AutoSize = true;this.label9.Location = new System.Drawing.Point(10, 97);this.label9.Name = "label9";this.label9.Size = new System.Drawing.Size(89, 12);this.label9.TabIndex = 15;this.label9.Text = "焊接Mark2坐标Y";// // label10// this.label10.AutoSize = true;this.label10.Location = new System.Drawing.Point(10, 76);this.label10.Name = "label10";this.label10.Size = new System.Drawing.Size(89, 12);this.label10.TabIndex = 14;this.label10.Text = "焊接Mark2坐标X";// // txtCrd2Mark2_X// this.txtCrd2Mark2_X.Location = new System.Drawing.Point(108, 71);this.txtCrd2Mark2_X.Name = "txtCrd2Mark2_X";this.txtCrd2Mark2_X.Size = new System.Drawing.Size(93, 21);this.txtCrd2Mark2_X.TabIndex = 13;this.txtCrd2Mark2_X.Text = "120.5";// // txtCrd2Mark1_Y// this.txtCrd2Mark1_Y.Location = new System.Drawing.Point(108, 40);this.txtCrd2Mark1_Y.Name = "txtCrd2Mark1_Y";this.txtCrd2Mark1_Y.Size = new System.Drawing.Size(93, 21);this.txtCrd2Mark1_Y.TabIndex = 12;this.txtCrd2Mark1_Y.Text = "20.5";// // label11// this.label11.AutoSize = true;this.label11.Location = new System.Drawing.Point(10, 41);this.label11.Name = "label11";this.label11.Size = new System.Drawing.Size(89, 12);this.label11.TabIndex = 11;this.label11.Text = "焊接Mark1坐标Y";// // label12// this.label12.AutoSize = true;this.label12.Location = new System.Drawing.Point(10, 20);this.label12.Name = "label12";this.label12.Size = new System.Drawing.Size(89, 12);this.label12.TabIndex = 10;this.label12.Text = "焊接Mark1坐标X";// // txtCrd2Mark1_X// this.txtCrd2Mark1_X.Location = new System.Drawing.Point(108, 15);this.txtCrd2Mark1_X.Name = "txtCrd2Mark1_X";this.txtCrd2Mark1_X.Size = new System.Drawing.Size(93, 21);this.txtCrd2Mark1_X.TabIndex = 9;this.txtCrd2Mark1_X.Text = "20.5";// // groupBox1// this.groupBox1.Controls.Add(this.txtCrd1Mark2_Y);this.groupBox1.Controls.Add(this.label7);this.groupBox1.Controls.Add(this.label8);this.groupBox1.Controls.Add(this.txtCrd1Mark2_X);this.groupBox1.Controls.Add(this.txtCrd1Mark1_Y);this.groupBox1.Controls.Add(this.label6);this.groupBox1.Controls.Add(this.label5);this.groupBox1.Controls.Add(this.txtCrd1Mark1_X);this.groupBox1.Location = new System.Drawing.Point(9, 4);this.groupBox1.Name = "groupBox1";this.groupBox1.Size = new System.Drawing.Size(217, 120);this.groupBox1.TabIndex = 20;this.groupBox1.TabStop = false;this.groupBox1.Text = "拍照Mark点坐标";// // txtCrd1Mark2_Y// this.txtCrd1Mark2_Y.Location = new System.Drawing.Point(108, 95);this.txtCrd1Mark2_Y.Name = "txtCrd1Mark2_Y";this.txtCrd1Mark2_Y.Size = new System.Drawing.Size(93, 21);this.txtCrd1Mark2_Y.TabIndex = 16;this.txtCrd1Mark2_Y.Text = "110";// // label7// this.label7.AutoSize = true;this.label7.Location = new System.Drawing.Point(10, 97);this.label7.Name = "label7";this.label7.Size = new System.Drawing.Size(89, 12);this.label7.TabIndex = 15;this.label7.Text = "拍照Mark2坐标Y";// // label8// this.label8.AutoSize = true;this.label8.Location = new System.Drawing.Point(10, 76);this.label8.Name = "label8";this.label8.Size = new System.Drawing.Size(89, 12);this.label8.TabIndex = 14;this.label8.Text = "拍照Mark2坐标X";// // txtCrd1Mark2_X// this.txtCrd1Mark2_X.Location = new System.Drawing.Point(108, 71);this.txtCrd1Mark2_X.Name = "txtCrd1Mark2_X";this.txtCrd1Mark2_X.Size = new System.Drawing.Size(93, 21);this.txtCrd1Mark2_X.TabIndex = 13;this.txtCrd1Mark2_X.Text = "310";// // txtCrd1Mark1_Y// this.txtCrd1Mark1_Y.Location = new System.Drawing.Point(108, 40);this.txtCrd1Mark1_Y.Name = "txtCrd1Mark1_Y";this.txtCrd1Mark1_Y.Size = new System.Drawing.Size(93, 21);this.txtCrd1Mark1_Y.TabIndex = 12;this.txtCrd1Mark1_Y.Text = "10";// // label6// this.label6.AutoSize = true;this.label6.Location = new System.Drawing.Point(10, 41);this.label6.Name = "label6";this.label6.Size = new System.Drawing.Size(89, 12);this.label6.TabIndex = 11;this.label6.Text = "拍照Mark1坐标Y";// // label5// this.label5.AutoSize = true;this.label5.Location = new System.Drawing.Point(10, 20);this.label5.Name = "label5";this.label5.Size = new System.Drawing.Size(89, 12);this.label5.TabIndex = 10;this.label5.Text = "拍照Mark1坐标X";// // txtCrd1Mark1_X// this.txtCrd1Mark1_X.Location = new System.Drawing.Point(108, 15);this.txtCrd1Mark1_X.Name = "txtCrd1Mark1_X";this.txtCrd1Mark1_X.Size = new System.Drawing.Size(93, 21);this.txtCrd1Mark1_X.TabIndex = 9;this.txtCrd1Mark1_X.Text = "10";// // dgvData// this.dgvData.AllowUserToAddRows = false;this.dgvData.AllowUserToDeleteRows = false;this.dgvData.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;this.dgvData.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {this.dgvcPoleSequence,this.dgvcPoleX,this.dgvcPoleY,this.dgvcPoleAngle,this.dgvcTransformX,this.dgvcTransformY});this.dgvData.Location = new System.Drawing.Point(5, 129);this.dgvData.Name = "dgvData";this.dgvData.ReadOnly = true;this.dgvData.RowHeadersWidth = 25;this.dgvData.RowTemplate.Height = 23;this.dgvData.Size = new System.Drawing.Size(717, 403);this.dgvData.TabIndex = 19;// // btnTransform// this.btnTransform.Font = new System.Drawing.Font("宋体", 16F);this.btnTransform.ForeColor = System.Drawing.Color.Blue;this.btnTransform.Location = new System.Drawing.Point(505, 12);this.btnTransform.Name = "btnTransform";this.btnTransform.Size = new System.Drawing.Size(127, 45);this.btnTransform.TabIndex = 23;this.btnTransform.Text = "坐标转换";this.btnTransform.UseVisualStyleBackColor = true;this.btnTransform.Click += new System.EventHandler(this.btnTransform_Click);// // dgvcPoleSequence// this.dgvcPoleSequence.HeaderText = "极柱序号";this.dgvcPoleSequence.Name = "dgvcPoleSequence";this.dgvcPoleSequence.ReadOnly = true;this.dgvcPoleSequence.Width = 80;// // dgvcPoleX// this.dgvcPoleX.HeaderText = "极柱X坐标";this.dgvcPoleX.Name = "dgvcPoleX";this.dgvcPoleX.ReadOnly = true;// // dgvcPoleY// this.dgvcPoleY.HeaderText = "极柱Y坐标";this.dgvcPoleY.Name = "dgvcPoleY";this.dgvcPoleY.ReadOnly = true;// // dgvcPoleAngle// this.dgvcPoleAngle.HeaderText = "极柱角度Z";this.dgvcPoleAngle.Name = "dgvcPoleAngle";this.dgvcPoleAngle.ReadOnly = true;// // dgvcTransformX// dataGridViewCellStyle5.ForeColor = System.Drawing.Color.Red;this.dgvcTransformX.DefaultCellStyle = dataGridViewCellStyle5;this.dgvcTransformX.HeaderText = "转换后X坐标";this.dgvcTransformX.Name = "dgvcTransformX";this.dgvcTransformX.ReadOnly = true;this.dgvcTransformX.Width = 130;// // dgvcTransformY// dataGridViewCellStyle6.ForeColor = System.Drawing.Color.Red;this.dgvcTransformY.DefaultCellStyle = dataGridViewCellStyle6;this.dgvcTransformY.HeaderText = "转换后Y坐标";this.dgvcTransformY.Name = "dgvcTransformY";this.dgvcTransformY.ReadOnly = true;this.dgvcTransformY.Width = 130;// // FormCoordinateTrans// this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.ClientSize = new System.Drawing.Size(1238, 544);this.Controls.Add(this.btnTransform);this.Controls.Add(this.rtxtMessage);this.Controls.Add(this.groupBox2);this.Controls.Add(this.groupBox1);this.Controls.Add(this.dgvData);this.Name = "FormCoordinateTrans";this.Text = "坐标转换-斯内科";this.Load += new System.EventHandler(this.FormCoordinateTrans_Load);this.groupBox2.ResumeLayout(false);this.groupBox2.PerformLayout();this.groupBox1.ResumeLayout(false);this.groupBox1.PerformLayout();((System.ComponentModel.ISupportInitialize)(this.dgvData)).EndInit();this.ResumeLayout(false);}#endregionprivate System.Windows.Forms.RichTextBox rtxtMessage;private System.Windows.Forms.GroupBox groupBox2;private System.Windows.Forms.TextBox txtCrd2Mark2_Y;private System.Windows.Forms.Label label9;private System.Windows.Forms.Label label10;private System.Windows.Forms.TextBox txtCrd2Mark2_X;private System.Windows.Forms.TextBox txtCrd2Mark1_Y;private System.Windows.Forms.Label label11;private System.Windows.Forms.Label label12;private System.Windows.Forms.TextBox txtCrd2Mark1_X;private System.Windows.Forms.GroupBox groupBox1;private System.Windows.Forms.TextBox txtCrd1Mark2_Y;private System.Windows.Forms.Label label7;private System.Windows.Forms.Label label8;private System.Windows.Forms.TextBox txtCrd1Mark2_X;private System.Windows.Forms.TextBox txtCrd1Mark1_Y;private System.Windows.Forms.Label label6;private System.Windows.Forms.Label label5;private System.Windows.Forms.TextBox txtCrd1Mark1_X;private System.Windows.Forms.DataGridView dgvData;private System.Windows.Forms.Button btnTransform;private System.Windows.Forms.DataGridViewTextBoxColumn dgvcPoleSequence;private System.Windows.Forms.DataGridViewTextBoxColumn dgvcPoleX;private System.Windows.Forms.DataGridViewTextBoxColumn dgvcPoleY;private System.Windows.Forms.DataGridViewTextBoxColumn dgvcPoleAngle;private System.Windows.Forms.DataGridViewTextBoxColumn dgvcTransformX;private System.Windows.Forms.DataGridViewTextBoxColumn dgvcTransformY;}
}

新建坐标结构Coord

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace CoordinateTransDemo
{/// <summary>/// 表示三维坐标系的一个坐标(x,y,z)/// </summary>public struct Coord{/// <summary>/// X坐标,一般是左右方向的轴X/// </summary>public double X;/// <summary>/// Y坐标,一般是前后方向的轴Y/// </summary>public double Y;/// <summary>/// Z坐标,一般是垂直高度【上下】方向的轴Z,有时也用角度Angle表示/// </summary>public double Z;public Coord(double x = 0, double y = 0, double z = 0) {X = x;Y = y;Z = z;}/// <summary>/// 判断两个坐标是否重合【是否是同一个点】/// </summary>/// <param name="coord1"></param>/// <param name="coord2"></param>/// <returns></returns>public static bool operator ==(Coord coord1, Coord coord2) {return coord1.X == coord2.X && coord1.Y == coord2.Y && coord1.Z == coord2.Z;}/// <summary>/// 判断两个坐标是否不重合【不同的坐标点】/// </summary>/// <param name="coord1"></param>/// <param name="coord2"></param>/// <returns></returns>public static bool operator !=(Coord coord1, Coord coord2){return coord1.X != coord2.X || coord1.Y != coord2.Y || coord1.Z != coord2.Z;}/// <summary>/// 打印坐标,返回坐标字符串/// </summary>/// <returns></returns>public override string ToString(){return $"({X},{Y},{Z})";}public override bool Equals(object obj){return base.Equals(obj);}public override int GetHashCode(){return base.GetHashCode();}}
}

新建关键的坐标转换类CoordinateTransHelper

关键算法代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace CoordinateTransDemo
{/// <summary>/// 坐标转换的算法/// 斯内科Snake/// </summary>public class CoordinateTransHelper{/// <summary>/// 坐标系1是标准坐标系/// 坐标系2相对于坐标系1MARK点距离差值的最大值/// </summary>public static double Max_MarkDistanceDIF = 1;/// <summary>/// 计算坐标系2相对于坐标系1的旋转角,结果是弧度/// </summary>public static double Crd2ToCrd1Angle = 0;/// <summary>/// 坐标转换方法【单个】/// </summary>/// <param name="Crd2Mark1">坐标系2的Mark1的点坐标</param>/// <param name="Crd2Mark2">坐标系2的Mark2的点坐标</param>/// <param name="Crd1Mark1">坐标系1的Mark1的点坐标</param>/// <param name="Crd1Mark2">坐标系1的Mark2的点坐标</param>/// <param name="InPoint">需要转换的初始坐标</param>/// <param name="OutPoint">转换后的结果坐标</param>/// <returns></returns>public static bool GetCalcCoordCoordinate(Coord Crd2Mark1, Coord Crd2Mark2, Coord Crd1Mark1, Coord Crd1Mark2, Coord InPoint, out Coord OutPoint, out string errorMsg){errorMsg = string.Empty;OutPoint = new Coord();OutPoint.X = 0;OutPoint.Y = 0;try{                Crd2ToCrd1Angle = GetRotateAngle(Crd2Mark1, Crd2Mark2, Crd1Mark1, Crd1Mark2);//计算坐标系2相对于坐标系1的缩放系数double Crd2ToCrd1Zoom =Math.Sqrt(Math.Pow((Crd2Mark1.X - Crd2Mark2.X), 2) + Math.Pow((Crd2Mark1.Y - Crd2Mark2.Y), 2))/Math.Sqrt(Math.Pow((Crd1Mark1.X - Crd1Mark2.X), 2) + Math.Pow((Crd1Mark1.Y - Crd1Mark2.Y), 2));//输出坐标计算OutPoint.X = ((InPoint.X - Crd1Mark1.X) * Math.Cos(Crd2ToCrd1Angle) - (InPoint.Y - Crd1Mark1.Y) * Math.Sin(Crd2ToCrd1Angle)) * Crd2ToCrd1Zoom + Crd2Mark1.X;OutPoint.Y = ((InPoint.X - Crd1Mark1.X) * Math.Sin(Crd2ToCrd1Angle) + (InPoint.Y - Crd1Mark1.Y) * Math.Cos(Crd2ToCrd1Angle)) * Crd2ToCrd1Zoom + Crd2Mark1.Y;return true;}catch (Exception ex){errorMsg = "计算异常:" + ex.Message + ex.StackTrace;return false;}}/// <summary>/// 坐标转换方法【批量】/// </summary>/// <param name="Crd2Mark1">坐标系2的Mark1的点坐标</param>/// <param name="Crd2Mark2">坐标系2的Mark2的点坐标</param>/// <param name="Crd1Mark1">坐标系1的Mark1的点坐标</param>/// <param name="Crd1Mark2">坐标系1的Mark2的点坐标</param>/// <param name="InPoint">需要转换的初始坐标集合</param>/// <param name="OutPoint">转换后的结果坐标集合</param>public static bool GetCalcCoordCoordinate(Coord Crd2Mark1, Coord Crd2Mark2, Coord Crd1Mark1, Coord Crd1Mark2, Coord[] InPointArray, out Coord[] OutPointArray,out string errorMessage) {errorMessage = string.Empty;OutPointArray = new Coord[0];if (InPointArray == null || InPointArray.Length == 0) {errorMessage = "需要转化的源坐标数组不能为空";return false;}OutPointArray = new Coord[InPointArray.Length];Crd2ToCrd1Angle = GetRotateAngle(Crd2Mark1, Crd2Mark2, Crd1Mark1, Crd1Mark2);//计算坐标系2相对于坐标系1的缩放系数double Crd2ToCrd1Zoom =Math.Sqrt(Math.Pow((Crd2Mark1.X - Crd2Mark2.X), 2) + Math.Pow((Crd2Mark1.Y - Crd2Mark2.Y), 2))/Math.Sqrt(Math.Pow((Crd1Mark1.X - Crd1Mark2.X), 2) + Math.Pow((Crd1Mark1.Y - Crd1Mark2.Y), 2));Coord OutPoint;for (int i = 0; i < OutPointArray.Length; i++){Coord InPoint = InPointArray[i];OutPoint = new Coord();//输出坐标计算OutPoint.X = ((InPoint.X - Crd1Mark1.X) * Math.Cos(Crd2ToCrd1Angle) - (InPoint.Y - Crd1Mark1.Y) * Math.Sin(Crd2ToCrd1Angle)) * Crd2ToCrd1Zoom + Crd2Mark1.X;OutPoint.Y = ((InPoint.X - Crd1Mark1.X) * Math.Sin(Crd2ToCrd1Angle) + (InPoint.Y - Crd1Mark1.Y) * Math.Cos(Crd2ToCrd1Angle)) * Crd2ToCrd1Zoom + Crd2Mark1.Y;OutPointArray[i] = OutPoint;}return true;}/// <summary>/// 计算坐标系2相对于坐标系1的旋转角,结果是弧度/// </summary>/// <param name="Crd2Mark1"></param>/// <param name="Crd2Mark2"></param>/// <param name="Crd1Mark1"></param>/// <param name="Crd1Mark2"></param>/// <returns></returns>public static double GetRotateAngle(Coord Crd2Mark1, Coord Crd2Mark2, Coord Crd1Mark1, Coord Crd1Mark2) {if (Crd1Mark1 == Crd1Mark2 || Crd2Mark1 == Crd2Mark2){throw new Exception("请检查参数配置,Mark点不能重合");}//计算坐标系2相对于坐标系1MARK点距离差值double MarkDistanceDIF = 0;//线段的长度进行比较MarkDistanceDIF = Math.Sqrt(Math.Pow((Crd2Mark1.X - Crd2Mark2.X), 2) + Math.Pow((Crd2Mark1.Y - Crd2Mark2.Y), 2))- Math.Sqrt(Math.Pow((Crd1Mark1.X - Crd1Mark2.X), 2) + Math.Pow((Crd1Mark1.Y - Crd1Mark2.Y), 2));if (Math.Abs(MarkDistanceDIF) > Math.Abs(Max_MarkDistanceDIF)){throw new Exception($"坐标系2相对于坐标系1MARK点距离差值:{MarkDistanceDIF},大于限制值:{Max_MarkDistanceDIF}");}//类似于将线段AB平移到原点开始变成线段OP,与 X轴正方向的夹角∠POX//线段向量OP共有8种可能:第一象限,第二象限,第三象限,第四象限,X+,Y+,X-,Y-double Crd1DertaX = Crd1Mark2.X - Crd1Mark1.X;double Crd1DertaY = Crd1Mark2.Y - Crd1Mark1.Y;double Crd2DertaX = Crd2Mark2.X - Crd2Mark1.X;double Crd2DertaY = Crd2Mark2.Y - Crd2Mark1.Y;double Crd1Angle = 0;//坐标系1的MARK点连成的直线指向第一象限if (Crd1DertaX > 0 && Crd1DertaY > 0){Crd1Angle = Math.Abs(Math.Atan((Crd1Mark2.Y - Crd1Mark1.Y) / (Crd1Mark2.X - Crd1Mark1.X)));}else{}//坐标系1的MARK点连成的直线指向第二象限if (Crd1DertaX < 0 && Crd1DertaY > 0){Crd1Angle = (180 * Math.PI / 180) - Math.Abs(Math.Atan((Crd1Mark2.Y - Crd1Mark1.Y) / (Crd1Mark2.X - Crd1Mark1.X)));}else{}//坐标系1的MARK点连成的直线指向第三象限if (Crd1DertaX < 0 && Crd1DertaY < 0){Crd1Angle = (180 * Math.PI / 180) + Math.Abs(Math.Atan((Crd1Mark2.Y - Crd1Mark1.Y) / (Crd1Mark2.X - Crd1Mark1.X)));}else{}//坐标系1的MARK点连成的直线指向第四象限if (Crd1DertaX > 0 && Crd1DertaY < 0){Crd1Angle = (360 * Math.PI / 180) - Math.Abs(Math.Atan((Crd1Mark2.Y - Crd1Mark1.Y) / (Crd1Mark2.X - Crd1Mark1.X)));}else{}double Crd2Angle = 0;//坐标系2的MARK点连成的直线指向第一象限if (Crd2DertaX > 0 && Crd2DertaY > 0){Crd2Angle = Math.Abs(Math.Atan((Crd2Mark2.Y - Crd2Mark1.Y) / (Crd2Mark2.X - Crd2Mark1.X)));}else{}//坐标系2的MARK点连成的直线指向第二象限if (Crd2DertaX < 0 && Crd2DertaY > 0){Crd2Angle = (180 * Math.PI / 180) - Math.Abs(Math.Atan((Crd2Mark2.Y - Crd2Mark1.Y) / (Crd2Mark2.X - Crd2Mark1.X)));}else{}//坐标系2的MARK点连成的直线指向第三象限if (Crd2DertaX < 0 && Crd2DertaY < 0){Crd2Angle = (180 * Math.PI / 180) + Math.Abs(Math.Atan((Crd2Mark2.Y - Crd2Mark1.Y) / (Crd2Mark2.X - Crd2Mark1.X)));}else{}//坐标系2的MARK点连成的直线指向第四象限if (Crd2DertaX > 0 && Crd2DertaY < 0){Crd2Angle = (360 * Math.PI / 180) - Math.Abs(Math.Atan((Crd2Mark2.Y - Crd2Mark1.Y) / (Crd2Mark2.X - Crd2Mark1.X)));}else{}//坐标系1的MARK点连成的直线倾角是0度,即X+if (Crd1DertaX > 0 && Crd1DertaY == 0){Crd1Angle = 0;}//坐标系1的MARK点连成的直线倾角是90度,即Y+if (Crd1DertaX == 0 && Crd1DertaY > 0){Crd1Angle = (90 * Math.PI / 180);}//坐标系1的MARK点连成的直线倾角是180度,即X-if (Crd1DertaX < 0 && Crd1DertaY == 0){Crd1Angle = (180 * Math.PI / 180);}//坐标系1的MARK点连成的直线倾角是270度,即Y-if (Crd1DertaX == 0 && Crd1DertaY < 0){Crd1Angle = (270 * Math.PI / 180);}//坐标系2的MARK点连成的直线倾角是0度if (Crd2DertaX > 0 && Crd2DertaY == 0){Crd2Angle = 0;}//坐标系2的MARK点连成的直线倾角是90度if (Crd2DertaX == 0 && Crd2DertaY > 0){Crd2Angle = (90 * Math.PI / 180);}//坐标系2的MARK点连成的直线倾角是180度if (Crd2DertaX < 0 && Crd2DertaY == 0){Crd2Angle = (180 * Math.PI / 180);}//坐标系2的MARK点连成的直线倾角是270度if (Crd2DertaX == 0 && Crd2DertaY < 0){Crd2Angle = (270 * Math.PI / 180);}//计算坐标系2相对于坐标系1的旋转角,结果是弧度Crd2ToCrd1Angle = Crd2Angle - Crd1Angle;return Crd2ToCrd1Angle;}}
}

测试坐标转换代码如下:

文件FormCoordinateTrans.cs

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 CoordinateTransDemo
{public partial class FormCoordinateTrans : Form{public FormCoordinateTrans(){InitializeComponent();//转换坐标为红色dgvcTransformX.HeaderCell.Style.ForeColor = Color.Red;dgvcTransformY.HeaderCell.Style.ForeColor = Color.Red;this.dgvData.EnableHeadersVisualStyles = false;rtxtMessage.ReadOnly = true;}private void FormCoordinateTrans_Load(object sender, EventArgs e){dgvData.Rows.Clear();int poleCount = 10;Coord[] photoCoords = new Coord[poleCount];photoCoords[0] = new Coord(123, 34.68);photoCoords[1] = new Coord(222.1, 222.2);photoCoords[2] = new Coord(333.11, 333.22);photoCoords[3] = new Coord(-1, -8.23);photoCoords[4] = new Coord(234, -345.3);photoCoords[5] = new Coord(-80, 125);photoCoords[6] = new Coord(-90, 0);photoCoords[7] = new Coord(0, 180);for (int i = 0; i < poleCount; i++){dgvData.Rows.Add(i + 1, photoCoords[i].X, photoCoords[i].Y, photoCoords[i].Z, 0.0F, 0.0F);}}private void btnTransform_Click(object sender, EventArgs e){Coord[] inPointArray = new Coord[dgvData.Rows.Count];for (int i = 0; i < dgvData.Rows.Count; i++){inPointArray[i] = new Coord(Convert.ToSingle(dgvData["dgvcPoleX", i].Value),Convert.ToSingle(dgvData["dgvcPoleY", i].Value),Convert.ToSingle(dgvData["dgvcPoleAngle", i].Value));}string errorMessage;Coord[] outPointArray;Coord Crd2Mark1 = new Coord(double.Parse(txtCrd2Mark1_X.Text), double.Parse(txtCrd2Mark1_Y.Text));Coord Crd2Mark2 = new Coord(double.Parse(txtCrd2Mark2_X.Text), double.Parse(txtCrd2Mark2_Y.Text));Coord Crd1Mark1 = new Coord(double.Parse(txtCrd1Mark1_X.Text), double.Parse(txtCrd1Mark1_Y.Text));Coord Crd1Mark2 = new Coord(double.Parse(txtCrd1Mark2_X.Text), double.Parse(txtCrd1Mark2_Y.Text));bool result = CoordinateTransHelper.GetCalcCoordCoordinate(Crd2Mark1, Crd2Mark2, Crd1Mark1, Crd1Mark2, inPointArray, out outPointArray, out errorMessage);double rotateAngle = CoordinateTransHelper.Crd2ToCrd1Angle * 180 / Math.PI;DisplayMessage($"获取转换后的坐标操作结果:【{result}】,旋转角度【{rotateAngle}°】,旋转弧度【{CoordinateTransHelper.Crd2ToCrd1Angle}】");for (int i = 0; i < dgvData.Rows.Count; i++){float srcPoleX = Convert.ToSingle(dgvData["dgvcPoleX", i].Value);float srcPoleY = Convert.ToSingle(dgvData["dgvcPoleY", i].Value);if (srcPoleX == 0 && srcPoleY == 0){//如果坐标是(0,0)认为是无效坐标dgvData["dgvcTransformX", i].Value = 0;dgvData["dgvcTransformY", i].Value = 0;continue;}dgvData["dgvcTransformX", i].Value = outPointArray[i].X;dgvData["dgvcTransformY", i].Value = outPointArray[i].Y;}}/// <summary>/// 显示推送消息/// </summary>/// <param name="msg"></param>private void DisplayMessage(string msg){this.BeginInvoke(new Action(() =>{if (rtxtMessage.TextLength > 20480){rtxtMessage.Clear();}rtxtMessage.AppendText($"{DateTime.Now.ToString("HH:mm:ss.fff")}->{msg}\n");rtxtMessage.ScrollToCaret();}));}}
}

运行如图:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/667553.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C++ 之LeetCode刷题记录(二十八)

&#x1f604;&#x1f60a;&#x1f606;&#x1f603;&#x1f604;&#x1f60a;&#x1f606;&#x1f603; 开始cpp刷题之旅。 目标&#xff1a;执行用时击败90%以上使用 C 的用户。 144. 二叉树的前序遍历 给你二叉树的根节点 root &#xff0c;返回它节点值的 前序 遍…

Facebook群控:利用IP代理提高聊单效率

在当今社交媒体竞争激烈的环境中&#xff0c;Facebook已经成为广告营销和推广的重要平台&#xff0c;为了更好地利用Facebook进行推广活动&#xff0c;群控技术应运而生。 本文将深入探讨Facebook群控的定义、作用以及如何利用IP代理来提升群控效率&#xff0c;为你提供全面的…

Adobe Camera Raw for Mac v16.1.0中文激活版

Adobe Camera Raw for Mac是一款强大的RAW格式图像编辑工具&#xff0c;它能够处理和编辑来自各种数码相机的原始图像。以下是关于Adobe Camera Raw for Mac的一些主要特点和功能&#xff1a; 软件下载&#xff1a;Adobe Camera Raw for Mac v16.1.0中文激活版 RAW格式支持&…

微信小程序使用ucharts折线图,有负数显示0刻度线

当数据有负数和正数的时候默认不会显示0刻度线&#xff0c;不方便看出正负对比 实现思路&#xff1a;显示的刻度线是根据数据的最大值和最小值自动分配到刻度线上面&#xff0c;把最大值和最小值设置为一样&#xff0c;然后平均分配给五个刻度线中间的刻度线就会为0就实现了显…

安卓平台valgrind交叉编译

背景 通过上次的文章valgrind跨平台调试及其问题分析,为同事们在大部分平台下进行内存问题分析提供了帮助。但是也遇到了阻塞情况&#xff1a;android 平台&#xff0c;无法交叉编译通过。大家对于编译这件事&#xff0c;似乎天然有一种排斥&#xff0c;本能的拒绝&#xff0c…

qt5入门-事件

参考&#xff1a; Qt 事件(event)_w3cschool https://www.w3cschool.cn/learnroadqt/xvme1j4c.html 本地环境&#xff1a; win10专业版&#xff0c;64位 事件的概念 将事件抽象为一个对象&#xff0c;当用户发起一个行为&#xff0c;就把对应的事件加入事件队列&#xff0c;对…

云计算、Docker、K8S问题

1 云计算 云计算作为一种新兴技术&#xff0c;已经在现代社会中得到了广泛应用。它以其高效、灵活和可扩展特性&#xff0c;成为了许多企业和组织在数据处理和存储方面的首选方案。 1.1 什么是云计算&#xff1f;它有哪些特点&#xff1f; 云计算是一种通过网络提供计算资源…

大型软件编程实例分享,诊所门诊处方笺管理系统多台电脑同时使用的软件教程

大型软件编程实例分享&#xff0c;诊所门诊处方笺管理系统多台电脑同时使用的软件教程 一、前言 以下教程以 佳易王诊所门诊电子处方管理系统V17.2 为例说明 软件资源可以点击最下方官网卡片了解详情 软件左侧为导航栏 1、系统参数设置&#xff1a;可以设置打印等参数 2、…

【数据分享】1929-2023年全球站点的逐年降雪深度数据(Shp\Excel\免费获取)

气象数据是在各项研究中都经常使用的数据&#xff0c;气象指标包括气温、风速、降水、能见度等指标&#xff0c;说到气象数据&#xff0c;最详细的气象数据是具体到气象监测站点的数据&#xff01; 之前我们分享过1929-2023年全球气象站点的逐年平均气温数据、逐年最高气温数据…

Debian系统显示中文

开发板上的debian默认不显示中文。 安装字体 sudo apt install fonts-wqy-zenhei 安装locals sudo apt install locales &#xff08;无必要&#xff09;设置/etc/locale.gen、设置/etc/locale.conf 运行dpkg-reconfigure locales dpkg-reconfigure locales 可以选择UT…

Sqli靶场23-->30

不知不觉鸽了几天了&#xff0c;没办法去旅游摸鱼是这样的了&#xff0c;抓紧时间来小更一下 23.过滤注释符号 先手工注入一下&#xff0c;就能发现两个单引号不报错&#xff0c;但是一旦上到注释符号的话就会报错&#xff0c;可以猜测出是对注释符号进行了过滤&#xff0c;我…

【Crypto | CTF】BUUCTF 萌萌哒的八戒

天命&#xff1a;这年头连猪都有密码&#xff0c;真是奇葩&#xff0c;怪不得我一点头绪都没有 拿到软件&#xff0c;发现是.zip的压缩包&#xff0c;打不开&#xff0c;改成7z后缀名&#xff0c;打开了 发现是一张图片 也只有下面这行东西是感觉是密码了&#xff0c;又不可能…

前端面试题——Vue的双向绑定

前言 双向绑定机制是Vue中最重要的机制之一&#xff0c;甚至可以说是Vue框架的根基&#xff0c;它将数据与视图模板相分离&#xff0c;使得数据处理和页面渲染更为高效&#xff0c;同时它也是前端面试题中的常客&#xff0c;接下来让我们来了解什么是双向绑定以及其实现原理。…

eslint报错文档大量红色报错符号 不自动修正

确保eslint在工作 控制台大量报错信息 确保setting.json 开了保存的时候自动格式化代码 这个时候保存的时候代码可以自动被格式化 但是 文档中和控制台中仍然有大量的报错 信息 此时此刻说明 格式化文档的文件不是按照eslint 格式化的 可以网上找找现成可用的setting.json抄…

一站式SpringBoot学习平台:让编程变得轻松有趣!

介绍&#xff1a;Spring Boot是一个开源的Java框架&#xff0c;旨在简化Spring应用程序的开发和部署过程。 Spring Boot由Pivotal团队设计并推出&#xff0c;它的核心优势在于极大地简化了传统Spring应用的初始搭建和开发流程。具体来说&#xff0c;Spring Boot的主要特点包括&…

Bard 最新更新:全球开放访问Gemini Pro并生成图片

深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同&#xff0c;从行业内部的深度分析和实用指南中受益。不要错过这个机会&#xff0c;成为AI领域的领跑者。点击订阅&#xff0c;与未来同行&#xff01; 订阅&#xff1a;https://rengongzhineng.io/ 。 今…

【讲座分享】| 复旦大学张奇教授——《自然语言发表论文如何打怪升级?NLP顶会论文发表》

文章目录 1 基础关1.1 基础书籍1.2 提高书籍1.3 课程链接1.4 编程实战 2 阅读关2.1 分层过滤2.2 集团作战&#xff0c;信息获取2.3 论文如何泛读 3 动机 方向关3.1 快速发论文3.2 好的研究 4 写作关4.1 论文写作流程4.2 从读者角度出发4.3 每一部分怎么写4.3.1 Abstract摘要4.3…

Sklearn、TensorFlow 与 Keras 机器学习实用指南第三版(八)

原文&#xff1a;Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow 译者&#xff1a;飞龙 协议&#xff1a;CC BY-NC-SA 4.0 第十八章&#xff1a;强化学习 强化学习&#xff08;RL&#xff09;是当今最激动人心的机器学习领域之一&#xff0c;也是最古老…

跟着pink老师前端入门教程-day18

3、CSS3 3D转换 生活中的环境是3D的&#xff0c;照片就是3D物体在2D平面呈现的例子 特点&#xff1a;近大远小&#xff0c;物体后面遮挡不可见 3.1 三维坐标系 三维坐标系其实就是指立体空间&#xff0c;立体空间是由3个轴共同组成的 x轴&#xff1a;水平向右 注意&#x…

SpringBoot 使用定时任务(SpringTask)

Spring3.0以后自带的task&#xff0c;可以将它看成一个轻量级的Quartz&#xff0c;而且使用起来比Quartz简单许多。 使用步骤&#xff1a; 1.导入坐标 在spring-boot-starter-web坐标中&#xff0c;就包含了SpringTask&#xff0c;所以一般的Web项目都包含了。 <depende…