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

        }

        #endregion

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

运行如图:

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Redis-缓存问题及解决方案

本文已收录于专栏 《中间件合集》 目录 概念说明缓存问题缓存击穿问题描述解决方案 缓存穿透问题描述解决方案 缓存雪崩问题描述解决方案提高缓存可用性过期时间配置熔断降级 总结提升 概念说明 Redis是一个开源的内存数据库&#xff0c;也可以用作缓存系统。它支持多种数据结构…

前端小案例——动态导航栏文字(HTML + CSS, 附源码)

一、前言 实现功能: 这案例是一个具有动态效果的导航栏。导航栏的样式设置了一个灰色的背景&#xff0c;并使用flex布局在水平方向上平均分配了四个选项。每个选项都是一个li元素&#xff0c;包含一个文本和一个横向的下划线。 当鼠标悬停在选项上时&#xff0c;选项的文本颜色…

华为自动驾驶干不过特斯拉?

文 | AUTO芯球 作者 | 李诞 什么&#xff1f; 华为的智能驾驶方案干不过蔚小理&#xff1f; 特斯拉的智能驾驶[FSD]要甩中国车企几条街&#xff1f; 这华为问界阿维塔刚刚推送“全国都能开”的城区“无图 NCA” 就有黑子来喷了 这是跪久了站不起来了吧 作为玩车14年&…

get通过发送Body传参-工具类

1、调用方式 String url "http://ip/xxx/zh/xxxxx/xxxx/userCode"; //进行url中的对应的参数 url2 url2.replace("ip",bancirili); url2 url2.replace("zh",zh); url2 url2.replace("userCode",userCode);String dateTime xxxx; //组…

04. 【Linux教程】安装 Linux 操作系统

通过前面的小节学习&#xff0c;我们已经对 Linux 操作系统有了简单的了解&#xff0c;同时也在 Windows 下安装了虚拟机软件 VMware &#xff0c;那么本节课我们就介绍下如何使用虚拟机软件安装 Linux 操作系统。 通过第一小节的学习我们知道 Linux 有很多的发行版本&#xf…

Vscode配置STM32开发环境(联合Keil MDK/IAR开发)

Vscode配置STM32开发环境&#xff08;替代Keil MDK/IAR&#xff09; 前言 使用了很长时间的Keil5 MDK&#xff0c;以及最近用了一段时间的IAR for ARM&#xff0c;总体来说体验都不是特别的好&#xff0c;Keil功能还行&#xff0c;也不卡顿&#xff0c;就是开发效率、界面样式…

1-2 动手学深度学习v2-基础优化方法-笔记

最常见的算法——梯度下降 当一个模型没有显示解的时候&#xff0c;该怎么办呢&#xff1f; 首先挑选一个参数的随机初始值&#xff0c;可以随便在什么地方都没关系&#xff0c;然后记为 w 0 \pmb{w_{0}} w0​在接下来的时刻里面&#xff0c;我们不断的去更新 w 0 \pmb{w_{0}…

asp.net core 依赖注入 实例化对象实例

在面向对象编程中&#xff0c;推荐使用面向接口编程&#xff0c;这样我们的代码就依赖于服务接口&#xff0c;而不是依赖于实现类&#xff0c;可以实现代码解耦。 名称解释&#xff1a; 我们把负责提供对象的注册和 获取功能的框架叫作“容器”&#xff0c; 注册到容器中的对象…

Android BitmapShader setLocalMatrix缩放Bitmap高度重新onMeasure,Kotlin

Android BitmapShader setLocalMatrix缩放Bitmap高度重新onMeasure&#xff0c;Kotlin <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas.android.com/apk/res/android"xmlns:tools"http://sc…

macbook死机了按什么键重启?CleanMyMac怎么清理MacBook

当您的macbook出现死机情况&#xff0c;您可以尝试以下几种方法来重启&#xff1a; 强制重启&#xff1a; 如果只是某个应用程序卡住&#xff0c;可以使用Command Option Esc组合键强制退出该应用。对于持续性的卡顿或死机&#xff0c;可以通过点击菜单栏上的苹果图标&#…

2024清洁能源、环境与智慧城市国际研讨会(ISCEESC2024)

2024清洁能源、环境与智慧城市国际研讨会(ISCEESC2024) 会议简介 2024年清洁能源、环境与智慧城市国际研讨会&#xff08;ISCEESC2024&#xff09;将在中国丽江举行。本次会议主要围绕清洁能源、环境和智慧城市等研究领域&#xff0c;旨在为该研究领域的专家学者提供一个国际…

【SpringBoot】RBAC权限控制

&#x1f4dd;个页人主&#xff1a;五敷有你 &#x1f525;系列专栏&#xff1a;SpringBoot⛺️稳重求进&#xff0c;晒太阳 权限系统与RBAC模型 权限 为了解决用户和资源的操作关系&#xff0c; 让指定的用户&#xff0c;只能操作指定的资源。 权限功能 菜单权限&a…

蓝桥杯Web应用开发-CSS 基础语法3(文本属性)

CSS 基础语法-文本属性 专栏持续更新中 文本属性 文本属性用于定义文本的样式&#xff0c;通过文本属性&#xff0c;可以改变文本的颜色、字间距、对齐方式、文本修饰和文本缩进等。常用文本属性如下表所示&#xff1a; 属 性可取值描述line-heightnormal、number、length、…

数字孪生网络攻防模拟与城市安全演练

在数字化浪潮的推动下&#xff0c;网络攻防模拟和城市安全演练成为维护社会稳定的不可或缺的环节。基于数字孪生技术我们能够在虚拟环境中进行高度真实的网络攻防模拟&#xff0c;为安全专业人员提供实战经验&#xff0c;从而提升应对网络威胁的能力。同时&#xff0c;在城市安…

Python基础学习-08 列表

Python 编程语言中有四种集合数据类型&#xff1a; 列表&#xff08;List&#xff09;是一种有序和可更改的集合。允许重复的成员。 选择集合类型时&#xff0c;了解该类型的属性很有用。 为特定数据集选择正确的类型可能意味着保留含义&#xff0c;并且可能意味着提高效率或…

ES6-对象的解构赋值

一、区别一下数组的解构赋值 - 对象的解构与数组有一个重要的不同。数组的元素是按次序排列的&#xff0c;变量的取值由它的位置决定&#xff1b;而对象的属性没有次序&#xff0c;变量必须与属性同名&#xff0c;才能取到正确的值二、说明 - 对象的解构赋值的内部机制&#…

STM32学习笔记三——深度讲解GPIO及其应用

目录 STM32GPIO端口位基本结构图&#xff1a; 结构图I/O引脚&#xff1a; GPIO输入输出总结 1.GPIO引脚的四种输入方式及其特点&#xff1a; 1)上拉输入(GPIO_Mode_IPU) 2)下拉输入(GPIO_Mode_IPD) 3)模拟输入(GPIO_Mode_AIN) 4)浮空输入(GPIO_Mode_IN_FLOATING…

信创ARM架构QT应用开发环境搭建

信创ARM架构QT应用开发环境搭建 前言交叉工具链Ubuntu上安装 32 位 ARM 交叉工具链Ubuntu上安装 64 位 ARM 交叉工具链 交叉编译 QT 库下载 QT 源码交叉编译 QT 源码 Qt Creator交叉编译配置配置 Qt Creator Kits创建一个测试项目 前言 有没有碰到过这种情况&#xff1f;开发出…

幻兽帕鲁服务器怎么搭建?Palworld多人联机教程

玩转幻兽帕鲁服务器&#xff0c;阿里云推出新手0基础一键部署幻兽帕鲁服务器教程&#xff0c;傻瓜式一键部署&#xff0c;3分钟即可成功创建一台Palworld专属服务器&#xff0c;成本仅需26元&#xff0c;阿里云服务器网aliyunfuwuqi.com分享2024年新版基于阿里云搭建幻兽帕鲁服…

Awesome Chrome Form UI - 界面设计与实现

上苍不会让所有幸福集中到某个人身上,得到了爱情未必拥有金钱;拥有金钱未必得到快乐;得到快乐未必拥有健康;拥有健康未必一切都会如愿以偿。知足常乐的心态才是淬炼心智、净化心灵的最佳途径。一切快乐的享受都属于精神,这种快乐把忍受变为享受,是精神对于物质的胜利。这…