1、先在项目文件下添加两个类分别命名为 Point和hanshu
point.cs 类 代码为:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 四参数坐标转换
{
class Point
{
public double x;
public double y;
}
}
hanshu.cs 类 代码为:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 四参数坐标转换
{
class hanshu
{
//矩阵转置函数
public static void Transpose_Matrix(ref double[,] ab, ref double[,] ba)
{
int p = ab.GetLength(0);
int q = ab.GetLength(1);
ba = new double[q, p];
for (int i = 0; i < q; i++)
{
for (int j = 0; j < p; j++)
ba[i, j] = ab[j, i];
}
}
//乘法矩阵
public static void Multiplication_Matrix(ref double[,] a, ref double[,] b, ref double[,] ab2)
{
ab2 = new double[a.GetLength(0), b.GetLength(1)];
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < b.GetLength(1); j++)
{
for (int k = 0; k < a.GetLength(1); k++)
{
ab2[i, j] += a[i, k] * b[k, j];
}
}
}
}
//逆矩阵函数
public static void Inverse(ref double[,] Array, ref double[,] NI)
{
int m = Array.GetLength(0);
int n = Array.GetLength(1);
double[,] array = new double[2 * m + 1, 2 * n + 1];
for (int k = 0; k < 2 * m + 1; k++) //初始化数组
{
for (int t = 0; t < 2 * n + 1; t++)
{
array[k, t] = 0.00000000;
}
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
array[i, j] = Array[i, j];
}
}
for (int k = 0; k < m; k++)
{
for (int t = n; t <= 2 * n; t++)
{
if ((t - k) == m)
{
array[k, t] = 1.0;
}
else
{
array[k, t] = 0;
}
}
}
//得到逆矩阵
for (int k = 0; k < m; k++)
{
if (array[k, k] != 1)
{
double bs = array[k, k];
array[k, k] = 1;
for (int p = k + 1; p < 2 * n; p++)
{
array[k, p] /= bs;
}
}
for (int q = 0; q < m; q++)
{
if (q != k)
{
double bs = array[q, k];
for (int p = 0; p < 2 * n; p++)
{
array[q, p] -= bs * array[k, p];
}
}
else
{
continue;
}
}
}
NI = new double[m, n];
for (int x = 0; x < m; x++)
{
for (int y = n; y < 2 * n; y++)
{
NI[x, y - n] = array[x, y];
}
}
}
}
}
给出三个点,两个用来计算四参数,两个用来检验。
Program.cs 下的代码为:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 四参数坐标转换
{
class Program
{
static void Main(string[] args)
{
//输入坐标数值
Point p1 = new Point();
Point p2 = new Point();
Point p3 = new Point();
p1.x = 543527.6559;
p1.y = 3374659.695;
p2.x = 543361.189;
p2.y = 3366244.473;
p3.x = 536097.9604;
p3.y = 3368549.362;
Point pp1 = new Point();
Point pp2 = new Point();
Point pp3 = new Point();
pp1.x = 543468.4082;
pp1.y = 3374710.408;
pp2.x = 543301.9006;
pp2.y = 3366295.805;
pp3.x = 536038.776;
pp3.y = 3368600.703;
double[,] B = { { 1, 0, p1.x, p1.y }, { 0, 1, p1.y, -p1.x }, { 1, 0, p2.x, p2.y }, { 0, 1, p2.y, -p2.x } };
double[,] l = { {pp1.x}, {pp1.y}, {pp2.x}, {pp2.y} };
//定义调用函数变量
double [,] ab = { };
double[,] ba = { };
double[,] a = { };
double[,] b = { };
double[,] ab2 = { };
double[,] Array = { };
double[,] NI = { };
double[,] a1 = { };
double[,] a2 = { };
double[,] sub = { };
double[,] M = { };
//计算B的转置矩阵
double[,] BT = { };
hanshu.Transpose_Matrix(ref B, ref BT);
//计算BTB
double[,] BTB = new double[4 ,4];
double[,] BTl = {};
double[,] BTBBTPl = { };
hanshu.Multiplication_Matrix(ref BT, ref B, ref BTB);
//计算BTl
hanshu.Multiplication_Matrix(ref BT, ref l, ref BTl);
//计算BTB的逆矩阵
double[,] BTB_ = { };
hanshu.Inverse(ref BTB, ref BTB_);
hanshu.Multiplication_Matrix(ref BTB_, ref BTl, ref BTBBTPl);
Console.WriteLine("△x= {0}",BTBBTPl[0,0]);
Console.WriteLine("△y= {0}",BTBBTPl[1,0]);
Console.WriteLine(" a= {0}",BTBBTPl[2,0]);
Console.WriteLine(" b= {0}",BTBBTPl[3,0]);
//hanshu.OutPut(ref BTBBTPl);
Console.WriteLine();
//计算m,θ
double m=new double();
double sita = new double();
sita = Math.Atan(BTBBTPl[3, 0] / BTBBTPl[2, 0]);
m = Math.Sqrt(BTBBTPl[3, 0] * BTBBTPl[3, 0] + BTBBTPl[2, 0] * BTBBTPl[2, 0]) - 1;
//检核
Point ppp3 = new Point();
ppp3.x = (1 +m) * (Math.Cos(sita) * p3.x + Math.Sin(sita ) * p3.y) + BTBBTPl[0,0];
ppp3.y = (1 + m) * (-Math.Sin(sita ) * p3.x + Math.Cos(sita) * p3.y) + BTBBTPl[1,0];
double v3x = ppp3.x - pp3.x;
double v3y = ppp3.y - pp3.y;
Console.WriteLine("用第三个点进行检验,检验结果为:");
Console.WriteLine();
Console.WriteLine("△3x={0}", v3x);
Console.WriteLine("△3y={0}", v3y);
}
}
}
最终运行结果为: