初中的时候就学过几何图形的伸缩、平移、旋转变换。在ArcEngine中,我们也可以借助ITransform2D
接口实现IGeometry
的伸缩、平移、旋转变换。先来看一下效果图:
代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
namespace WindowsFormsApplication1
{
public partial class MainForm : Form
{
private string type;
// 构造函数
public MainForm()
{
InitializeComponent();
}
// 缩放
private void btnTelescopic_Click(object sender, EventArgs e)
{
type = "缩放";
}
// 平移
private void btnPan_Click(object sender, EventArgs e)
{
type = "平移";
}
// 旋转
private void btnRotate_Click(object sender, EventArgs e)
{
type = "旋转";
}
// OnMouseDown事件
private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
IGeometry pSourceGeometry = axMapControl1.TrackPolygon();
IRgbColor pSourceRgbColor = CreateRgbColor(255, 0, 0);
Draw(pSourceGeometry, pSourceRgbColor, esriSimpleFillStyle.esriSFSVertical);
if (type == "缩放")
{
IGeometry pTargetGeometry = Telescopic(pSourceGeometry, 100);
IRgbColor pTargetRgbColor = CreateRgbColor(0, 0, 255);
Draw(pTargetGeometry, pTargetRgbColor, esriSimpleFillStyle.esriSFSBackwardDiagonal);
}
if (type == "平移")
{
IGeometry pTargetGeometry = Pan(pSourceGeometry, 200, 0);
IRgbColor pTargetRgbColor = CreateRgbColor(0, 255, 0);
Draw(pTargetGeometry, pTargetRgbColor, esriSimpleFillStyle.esriSFSBackwardDiagonal);
}
if (type == "旋转")
{
IPointCollection pPointCollection = pSourceGeometry as IPointCollection;
IGeometry pTargetGeometry = Rotate(pSourceGeometry, pPointCollection.get_Point(0), Math.PI / 4);
IRgbColor pTargetRgbColor = CreateRgbColor(0, 255, 255);
Draw(pTargetGeometry, pTargetRgbColor, esriSimpleFillStyle.esriSFSBackwardDiagonal);
}
}
// 伸缩变换
private IGeometry Telescopic(IGeometry pGeometry, double distance)
{
IEnvelope pEnvelope = pGeometry.Envelope;
double x = pEnvelope.XMin + pEnvelope.Width / 2;
double y = pEnvelope.YMin + pEnvelope.Height / 2;
// 伸缩比例
double sx = (pEnvelope.Width + 2 * distance) / pEnvelope.Width;
double sy = (pEnvelope.Height + 2 * distance) / pEnvelope.Height;
// 中心点坐标
IPoint pPoint = new ESRI.ArcGIS.Geometry.Point();
pPoint.SpatialReference = pGeometry.SpatialReference;
pPoint.PutCoords(x, y);
// 返回结果
ITransform2D pTransform2D = pGeometry as ITransform2D;
pTransform2D.Scale(pPoint, sx, sy);
return pTransform2D as IGeometry;
}
// 平移变换
private IGeometry Pan(IGeometry pGeometry, double dx, double dy)
{
ITransform2D pTransform2D = pGeometry as ITransform2D;
pTransform2D.Move(dx, dy);
return pTransform2D as IGeometry;
}
// 旋转变换
private IGeometry Rotate(IGeometry pGeometry, IPoint pPoint, double angle)
{
IPointCollection pPointCollection = pGeometry as IPointCollection;
ITransform2D pTransform2D = pGeometry as ITransform2D;
pTransform2D.Rotate(pPoint, angle);
return pTransform2D as IGeometry;
}
// 绘制图形
private void Draw(IGeometry pGeometry, IRgbColor pRgbColor, esriSimpleFillStyle style)
{
// 创建符号
ISimpleFillSymbol pSimpleFillSymbol = new SimpleFillSymbol();
pSimpleFillSymbol.Color = pRgbColor;
pSimpleFillSymbol.Style = style;
// 创建元素
IFillShapeElement pFillShapeElement = new PolygonElement() as IFillShapeElement;
pFillShapeElement.Symbol = pSimpleFillSymbol;
IElement pElement = pFillShapeElement as IElement;
pElement.Geometry = pGeometry;
// 绘制元素
IActiveView pActiveView = axMapControl1.ActiveView;
IGraphicsContainer pGraphicsContainer = pActiveView.GraphicsContainer;
pGraphicsContainer.AddElement(pElement, 0);
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
// 创建颜色
private IRgbColor CreateRgbColor(int r, int g, int b)
{
IRgbColor pRgbColor = new RgbColor();
pRgbColor.Red = r;
pRgbColor.Green = g;
pRgbColor.Blue = b;
return pRgbColor;
}
}
}
需要注意的是,在上面的代码中,放大距离我设置的是100,如果想要缩小,只需要把距离设置成负数即可。平移距离我只设置了X方向的平移距离,旋转变换的旋转点我设置为多边形的起点,这些都可以根据需求自行修改。