ArcEngine几何变换中的策略模式

时间:2023-03-08 20:21:54

使用策略模式可以减少分支语句,switch...Case,同时便于策略的扩展。

1. ITransform2D接口的Transform方法:

 [C#]public void Transform (
esriTransformDirection direction,
ITransformation transformation);

大部分的Geometry对象都实现了ITransform接口,比如:IPoint,IPolygon的基类

ITransformation是策略的抽象接口,如下:

ArcEngine几何变换中的策略模式

2. ITransform3D接口的Transform3D方法:

 [C#]public void Transform3D (
esriTransformDirectiondirection,
ITransformation3Dtransformation);

ITransformation3D是策略的抽象接口,如下:

ArcEngine几何变换中的策略模式

   private void RotateAroundPoint()
{
//Point to be rotated
IPoint rotatePoint = new ESRI.ArcGIS.Geometry.Point();
rotatePoint.PutCoords(, );
//Point around which to rotate
IPoint centerPoint = new ESRI.ArcGIS.Geometry.Point();
centerPoint.PutCoords(, );
//Rotation Angle
double angle = * Math.PI / 180.0;
//Rotate Around pCenter
IAffineTransformation2D3GEN affineTransformation = new AffineTransformation2D() as IAffineTransformation2D3GEN;
affineTransformation.Move(-centerPoint.X, -centerPoint.Y);//将参考点移动到原点
affineTransformation.Rotate(angle);//围绕原点旋转
affineTransformation.Move(centerPoint.X, centerPoint.Y);//再平移回原来的位置
ITransform2D transformator = rotatePoint as ITransform2D;
transformator.Transform(esriTransformDirection.esriTransformForward, affineTransformation as ITransformation); IPoint rotatePoint2 = new ESRI.ArcGIS.Geometry.Point();
rotatePoint2.PutCoords(, );
IAffineTransformation2D affineTransformation2D = new AffineTransformation2D() as IAffineTransformation2D;
// affineTransformation2D.Move(-centerPoint.X, -centerPoint.Y);//将参考点移动到原点
affineTransformation2D.Rotate(angle);//围绕原点旋转
//affineTransformation2D.Move(centerPoint.X, centerPoint.Y);//再平移回原来的位置
ITransform2D transformator2 = rotatePoint2 as ITransform2D;
transformator2.Transform(esriTransformDirection.esriTransformForward, affineTransformation2D); //Set up Comparison Point
//This is the point the transformation should result in
IPoint comparePoint = new ESRI.ArcGIS.Geometry.Point();
comparePoint.PutCoords(, );
transformator = comparePoint as ITransform2D;
transformator.Rotate(centerPoint, angle);
System.Windows.Forms.MessageBox.Show(
"Using IAffineTransformation2D3GEN.Rotate: Point X:" + rotatePoint.X + ", Y:" + rotatePoint.Y + "\n" +
"Using IAffineTransformation2D::Rotate, Point X:" + rotatePoint2.X + ", Y:" + rotatePoint2.Y + "\n" +
"Using ITransform2D::Rotate, Point X: " + comparePoint.X + ", Y:" + comparePoint.Y + "\n" +
"Did X coordinates match? " + (rotatePoint.X == comparePoint.X) + "\n" +
"Did Y coordinates match? " + (rotatePoint.Y == comparePoint.Y)
); }

测试代码

ArcEngine几何变换中的策略模式

相关文章