空间直角坐标转换之仿射变换(共9页).doc
精选优质文档-倾情为你奉上空间直角坐标转换之仿射变换一、仿射变换仿射变换是空间直角坐标变换的一种,它是一种二维坐标到二维坐标之间的线性变换,保持二维图形的“平直线”和“平行性”,其可以通过一系列的原子变换的复合来实现,包括平移(Translation)、缩放(Scale)、翻转(Flip)、旋转(Rotation)和剪切(Shear)。此类变换可以用一个3×3的矩阵来表示,其最后一行为(0, 0, 1)。该变换矩阵将原坐标(x, y)变换为新坐标(x', y'),这里原坐标和新坐标皆视为最末一行为(1)的三维列向量,原列向量左乘变换矩阵得到新的列向量:x' m00 m01 m02 x m00*x+m01*y+m02y' = m10 m11 m12 y = m10*x+m11*y+m121 0 0 1 1 1 如果将它写成按旋转、缩放、平移三个分量的复合形式,则其代数式如下: x = m00*x+m01*y+m02; y = m10*x+m11*y+m12;其示意图如下:几种典型的仿射变换:1.public static AffineTransform getTranslateInstance(double tx, double ty) 平移变换,将每一点移动到(x+tx, y+ty),变换矩阵为: 1 0 tx 0 1 ty 0 0 1 (译注:平移变换是一种“刚体变换”,rigid-body transformation,中学学过的物理,都知道啥叫“刚体”吧,就是不会产生形变的理想物体,平移当然不会改变二维图形的形状。同理,下面的“旋转变换”也是刚体变换,而“缩放”、“错切”都是会改变图形形状的。) 2.public static AffineTransform getScaleInstance(double sx, double sy) 缩放变换,将每一点的横坐标放大(缩小)至sx倍,纵坐标放大(缩小)至sy倍,变换矩阵为: sx 0 0 0 sy 0 0 0 1 3.public static AffineTransform getShearInstance(double shx, double shy) 剪切变换,变换矩阵为: 1 shx 0 shy 1 0 0 0 1 相当于一个横向剪切与一个纵向剪切的复合 1 0 0 1 shx 0 shy 1 0 0 1 0 0 0 1 0 0 1 (译注:“剪切变换”又称“错切变换”,指的是类似于四边形不稳定性那种性质,街边小商店那种铁拉门都见过吧?想象一下上面铁条构成的菱形拉动的过程,那就是“错切”的过程。) 4.public static AffineTransform getRotateInstance(double theta) 旋转变换,目标图形围绕原点顺时针旋转theta弧度,变换矩阵为: cos(theta) -sin(theta) 0 sin(theta) cos(theta) 0 0 0 1 5.public static AffineTransform getRotateInstance(double theta, double x, double y) 旋转变换,目标图形以(x, y)为轴心顺时针旋转theta弧度,变换矩阵为: cos(theta) -sin(theta) x-x*cos+y*sin sin(theta) cos(theta) y-x*sin-y*cos 0 0 1 相当于两次平移变换与一次原点旋转变换的复合:1 0 -xcos(theta) -sin(theta) 01 0 x0 1 -ysin(theta) cos(theta) 00 1 y0 0 1 0 0 1 0 0 1二、仿射变换四参数求解A、C#自定义函数实现求解:1、求解旋转参数Rotaion: 1/*/<summary> 2 3 /获取旋转角度 4 5 /</summary> 6 7 /<param name="fromCoordPoint1">源点1</param> 8 9 /<param name="toCoordPoint1">目标点1</param>1011 /<param name="fromCoordPoint2">源点2</param>1213 /<param name="toCoordPoint2">目标点2</param>1415 /<returns>返回旋转角度</returns>1617 private double GetRotation(CoordPoint fromPoint1, CoordPoint toPoint1,CoordPoint fromPoint2,CoordPoint toPoint2)1819 2021 double a = (toPoint2.Y - toPoint1.Y) * (fromPoint2.X - fromPoint1.X) - (toPoint2.X - toPoint1.X) * (fromPoint2.Y - fromPoint1.Y);2223 double b = (toPoint2.X - toPoint1.X) * (fromPoint2.X - fromPoint1.X) + (toPoint2.Y - toPoint1.Y) * (fromPoint2.Y - fromPoint1.Y);2425 2627 if (Math.Abs(b) > 0)2829 return Math.Tan(a / b);3031 else3233 return Math.Tan(0); 3435 2、求解缩放比例参数(Scale): 1 /*/<summary> 2 3 /获取缩放比例因子 4 5 /</summary> 6 7 /<param name="fromCoordPoint1">源点1</param> 8 9 /<param name="toCoordPoint1">目标点1</param>1011 /<param name="fromCoordPoint2">源点2</param>1213 /<param name="toCoordPoint2">目标点2</param>1415 /<param name="rotation">旋转角度</param>1617 /<returns>返回旋转因子</returns>1819 private double GetScale(CoordPoint fromPoint1, CoordPoint toPoint1, CoordPoint fromPoint2, CoordPoint toPoint2, double rotation)2021 2223 double a = toPoint2.X - toPoint1.X;2425 double b = (fromPoint2.X - fromPoint1.X) * Math.Cos(rotation) - (fromPoint2.Y - fromPoint1.Y)*Math.Sin(rotation);2627 if (Math.Abs(b) > 0)2829 return a / b;3031 else3233 return 0;3435 3、求解X方向偏移距离参数(XTranslate): 1/*/<summary> 2 3 /得到X方向偏移量 4 5 /</summary> 6 7 /<param name="fromCoordPoint1">源点1</param> 8 9 /<param name="toCoordPoint1">目标点1</param>1011 /<param name="rotation">旋转角度</param>1213 /<param name="scale">缩放因子</param>1415 /<returns>返回X方向偏移量</returns>1617 private double GetXTranslation(CoordPoint fromPoint1,CoordPoint toPoint1,double rotation,double scale)1819 2021 return (toPoint1.X - scale * (fromPoint1.X * Math.Cos(rotation) - fromPoint1.Y * Math.Sin(rotation);2223 2425 4、求解Y方向偏移距离参数(YTranslate): 1 /*/<summary> 2 3 /得到Y方向偏移量 4 5 /</summary> 6 7 /<param name="fromCoordPoint1">源点1</param> 8 9 /<param name="toCoordPoint1">目标点1</param>1011 /<param name="rotation">旋转角度</param>1213 /<param name="scale">缩放因子</param>1415 /<returns>返回Y方向偏移量</returns>1617 private double GetYTranslation(CoordPoint fromPoint1, CoordPoint toPoint1, double rotation, double scale)1819 2021 return (toPoint1.Y - scale * (fromPoint1.X * Math.Sin(rotation) + fromPoint1.Y * Math.Cos(rotation);2223 B、C#+AE求解: 1/*/<summary> 2 3 /从控制点定义仿射变换程式 4 5 /</summary> 6 7 /<param name="pFromPoints">源控制点</param> 8 9 /<param name="pToPoints">目标控制点</param>1011 /<returns>返回变换定义</returns>1213 private ITransformation GetAffineTransformation(IPoint pFromPoints, IPoint pToPoints)1415 1617 /实例化仿射变换对象1819 IAffineTransformation2D3GEN tAffineTransformation = new AffineTransformation2DClass();2021 /从源控制点定义参数2223 tAffineTransformation.DefineFromControlPoints(ref pFromPoints, ref pToPoints);2425 /查询引用接口2627 ITransformation tTransformation = tAffineTransformation as ITransformation;2829 return tTransformation;3031 三、空间对象转换 求出参数后,再利用公式对相应坐标点进行转换是一件相对简单的事件了。示例代码: /*/<summary> 2 3 /转换空间点 4 5 /</summary> 6 7 /<param name="pPoint">点</param> 8 9 /<returns>返回转换后的点</returns>1011 private IGeometry TransformPoint(IPoint pPoint)1213 1415 /*1617 /说明:采用相似变换模型(四参数变换模型)1819 / X= ax + by + c2021 / Y=-bx + ay + d2223 /*2425 double A = this.m_Scale * Math.Cos(this.m_RotationAngle);2627 double B = this.m_Scale * Math.Sin(this.m_RotationAngle);2829 IPoint tPoint = new PointClass();3031 tPoint.X = A * pPoint.X + B * pPoint.Y + this.m_DX;3233 tPoint.Y = B * (-1.0) * pPoint.X + A * pPoint.Y + this.m_DY;3435 return tPoint;3637 四、总结:本文主要介绍了如何利用仿射变换方程来进行空间直角坐标转换,对仿射变换的几种典型情况作了详细的讲解,对于具体如何求解参数给出了关键的实现代码,对于空间对象的变换给出了参考示例。专心-专注-专业