使用方法如下:
Matrix2D matrix2D = new Matrix2D(m_CADCooridate, m_OtherCooridateInfo.CADCooridate, m_SDRQCooridate, m_OtherCooridateInfo.SDRQCooridate);
gpdPt = matrix2D.GetNewPt(cadPt);
可以用于CAD坐标与实际平面坐标的转换
/**以下是源码
* @author xuhaiyan
* @copyright opensource
* @date 2012-12-26
* @version 4.0
* @description
* @dependence .net 2.0
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace Common.Algorithm
{
public class Vector
{
public static readonly double ZERO = 1e-6;
private double m_Length;
private double m_Angle;
private Point2D m_StartPt=new Point2D(0,0);
private Point2D m_EndPt =null;
public double Length
{
get { return m_Length; }
set
{
checkLength(value);
m_Length = value;
calculateEndPt();
}
}
public double Angle
{
get { return m_Angle; }
set
{
checkAngle(value);
m_Angle = value;
calculateEndPt();
}
}
public Point2D StartPt
{
get { return m_StartPt; }
set {
checkPoint2D(value);
m_StartPt = value;
calculateEndPt();
}
}
public Point2D EndPt
{
get { return m_EndPt; }
}
public Vector()
{
}
public Vector(double length,double angle)
{
checkAngle(angle);
checkLength(length);
m_Angle = angle;
m_Length = length;
m_StartPt = new Point2D(0, 0);
calculateEndPt();
}
public Vector(double length, double angle,Point2D startPt)
{
checkAngle(angle);
checkLength(length);
checkPoint2D(startPt);
m_Angle = angle;
m_Length = length;
m_StartPt = startPt;
calculateEndPt();
}
public Vector( Point2D startPt,Point2D endPt)
{
checkPoint2D(startPt);
checkPoint2D(endPt);
m_StartPt = startPt;
m_EndPt = endPt;
calculateParameter();
}
private void checkPoint2D(Point2D pt)
{
if (pt == null )
{
throw new Exception("point invalid!");
}
}
private void checkLength(double length)
{
if (length <= 0)
{
throw new Exception("length must lager than 0!");
}
}
private void checkAngle(double angle)
{
if (angle < 0 || angle >= 2 * Math.PI)
{
throw new Exception("angle must be between [0 , 2π)!");
}
}
private void calculateEndPt()
{
if (m_StartPt!= null)
{
double deltaX = m_Length * Math.Sin(m_Angle);
double deltaY = m_Length * Math.Cos(m_Angle);
m_EndPt = new Point2D(m_StartPt.X + deltaX, m_StartPt.Y + deltaY);
}
}
private void calculateParameter()
{
if (m_StartPt != null && m_EndPt != null)
{
m_Length = Math.Sqrt(
Math.Pow((m_EndPt.X - m_StartPt.X), 2)
+ Math.Pow((m_EndPt.Y - m_StartPt.Y), 2));
double deltaX = m_EndPt.X - m_StartPt.X;
double deltaY = m_EndPt.Y - m_StartPt.Y;
if (deltaY > -ZERO && deltaY < ZERO) //same y
{
if (deltaX > 0)
{
m_Angle = 0.5 * Math.PI;
}
else
{
m_Angle = 1.5 * Math.PI;
}
}else
{
m_Angle = Math.Atan(deltaX / deltaY);
if (deltaX> 0 && deltaY <0) //2
{
m_Angle += Math.PI;
}
else if (deltaX < 0 && deltaY < 0) //3
{
m_Angle += Math.PI;
}
else if (deltaX < 0 && deltaY > 0) //4
{
m_Angle += 2 * Math.PI;
}
}
}
}
}
public class Matrix2D
{
private Point2D m_OldPt1;
private Point2D m_OldPt2;
private Point2D m_NewPt1;
private Point2D m_NewPt2;
private Vector m_OldVector = null;
private Vector m_NewVector = null;
private double m_Angle;
private double m_Scale;
private double m_TranslationX;
private double m_TranslationY;
public double Scale
{
get { return m_Scale; }
}
public double Angle
{
get { return m_Angle; }
}
public Matrix2D()
{
}
public Matrix2D(Point2D oldPt1, Point2D oldPt2, Point2D newPt1, Point2D newPt2)
{
m_OldPt1 = oldPt1;
m_OldPt2 = oldPt2;
m_NewPt1 = newPt1;
m_NewPt2 = newPt2;
m_OldVector = new Vector(oldPt1, oldPt2);
m_NewVector = new Vector(newPt1, newPt2);
m_Angle = m_NewVector.Angle - m_OldVector.Angle;
if ( m_Angle<0)
{
m_Angle += 2 * Math.PI;
}
m_Scale = m_NewVector.Length / m_OldVector.Length;
m_TranslationX = m_NewPt1.X - m_OldPt1.X;
m_TranslationY = m_NewPt1.Y - m_OldPt1.Y;
}
public Point2D GetNewPt(Point2D oldPt)
{
checkVector();
Vector oldV= new Vector(m_OldPt1, oldPt);
double newAngle = oldV.Angle + m_Angle;
if (newAngle >= 2*Math.PI) //only maybe lager than 2PI
{
newAngle -= 2 * Math.PI;
}
oldV.Angle = newAngle;
oldV.Length *= m_Scale;
oldV.StartPt = m_NewPt1;
Point2D pt = oldV.EndPt;
return pt;
}
public Point2D GetOldPt(Point2D newPt)
{
checkVector();
Vector newV = new Vector(m_NewPt1, newPt);
double angle = newV.Angle - m_Angle;
if (angle <0)
{
angle += 2 * Math.PI;
}
newV.Length /= m_Scale;
newV.StartPt = m_OldPt1;
Point2D pt = newV.EndPt;
return pt;
}
public void checkVector()
{
if (m_OldVector == null||m_NewVector == null)
{
throw new Exception("calculate Vector first!");
}
}
}
}