博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
wpf GIS 在地图上画正方形和圆形
阅读量:5327 次
发布时间:2019-06-14

本文共 4347 字,大约阅读时间需要 14 分钟。

1、效果图

 

2、代码

using System; using System.Collections.Generic; using System.Linq; using System.Text;

using System.Drawing; using System.Windows.Forms; using MeteoGIS.Data; using MeteoGIS.Realspace; using MeteoGIS.UI;

namespace NewTraffic3DSystem

{   

  public class GraphicsRender     {

 

        private bool mouseStatus = false;//鼠标状态,false为松开         private Point2D startPoint;//         //private Point startPoint;//鼠标按下的点         private Point2D endPoint;//         private double minStartX, minStartY, maxEndX, maxEndY;//最大重绘矩形的上下左右的坐标,这样重绘的效率更高。         private GeoStyle3D gs3D = new GeoStyle3D();         private SceneControl m_sceneControl = null;         public ShapeType drawingShapeType;       

  public enum ShapeType         {         

    Rectangle,             Circle,             Polygon       

  }

        public GraphicsRender(SceneControl sctrol)     

    {             m_sceneControl = sctrol;             m_sceneControl.Action = Action3D.Select;             drawingShapeType = ShapeType.Circle;         }         public void m_sceneControl_MouseDown(object sender, MouseEventArgs e)         {             if (e.Button == MouseButtons.Left)             {                 Point2D p2d = ConvertToGlobePoint2D(e.Location);                 mouseStatus = true;                 startPoint.X = p2d.X;                 startPoint.Y = p2d.Y;                 //重新一个矩形,重置最大重绘矩形的上下左右的坐标                 minStartX = p2d.X;                 minStartY = p2d.Y;                 maxEndX = p2d.X;                 maxEndY = p2d.Y;             }             else             {                 //重新一个矩形,重置最大重绘矩形的上下左右的坐标                 minStartX = 0;                 minStartY = 0;                 maxEndX = 0;                 maxEndY = 0;                 int indexNum = m_sceneControl.Scene.TrackingLayer.IndexOf("rect");                 if (indexNum != -1)                     m_sceneControl.Scene.TrackingLayer.Remove(indexNum);                 m_sceneControl.Action = Action3D.Pan;

                m_sceneControl.MouseDown -= new MouseEventHandler(m_sceneControl_MouseDown);

                m_sceneControl.MouseMove -= new MouseEventHandler(m_sceneControl_MouseMove);             }         }

        public void m_sceneControl_MouseMove(object sender, MouseEventArgs e)         {             if (mouseStatus)             {                 Point2D p2d = ConvertToGlobePoint2D(e.Location);                 endPoint.X = p2d.X; endPoint.Y = p2d.Y;                 //这一段是获取要绘制矩形的上下左右的坐标,如果不这样处理的话,只有从左上开始往右下角才能画出矩形。                 //这样处理的话,可以任意方向,当然中途可以更换方向。                 double realStartX = Math.Min(startPoint.X, endPoint.X);                 double realStartY = Math.Min(startPoint.Y, endPoint.Y);                 double realEndX = Math.Max(startPoint.X, endPoint.X);                 double realEndY = Math.Max(startPoint.Y, endPoint.Y);

                minStartX = Math.Min(minStartX, realStartX);                 minStartY = Math.Min(minStartY, realStartY);                 maxEndX = Math.Max(maxEndX, realEndX);                 maxEndY = Math.Max(maxEndY, realEndY);             }             int indexNum = m_sceneControl.Scene.TrackingLayer.IndexOf("rect");             if (indexNum != -1)                 m_sceneControl.Scene.TrackingLayer.Remove(indexNum);             gs3D.FillForeColor = Color.Gray;             gs3D.LineColor = Color.Red;             gs3D.LineWidth = 0.5;             gs3D.FillMode = FillMode3D.Line;             switch (drawingShapeType)             {                 case ShapeType.Rectangle:                     Point3Ds pnsRectangleNodes = new Point3Ds();                     Point3D pnt1 = new Point3D(minStartX, minStartY, 0.0f);                     Point3D pnt2 = new Point3D(minStartX, maxEndY, 0.0f);                     Point3D pnt3 = new Point3D(maxEndX, maxEndY, 0.0f);                     Point3D pnt4 = new Point3D(maxEndX, minStartY, 0.0f);                     pnsRectangleNodes.Add(pnt1);                     pnsRectangleNodes.Add(pnt2);                     pnsRectangleNodes.Add(pnt3);                     pnsRectangleNodes.Add(pnt4);                     GeoRegion3D tempRectangle = new GeoRegion3D(pnsRectangleNodes);

                    tempRectangle.Style3D = gs3D;                     m_sceneControl.Scene.TrackingLayer.Add(tempRectangle, "rect");                     break;                 case ShapeType.Circle:                     double radius = CalculateDistance(startPoint, endPoint) + 0.0001;                     Point3D pntCircleCenter = new Point3D(startPoint.X, startPoint.Y, 0.0f);                     //GeoCircle3D tempCircle3D = new GeoCircle3D(pntCircleCenter, radius);

                    Point3Ds pt = new Point3Ds();                     for (int i = 0; i < 128; i++)                     {                         pt.Add(new Point3D(pntCircleCenter.X + radius * Math.Cos(i * Math.PI * 2 / 128.0),                             pntCircleCenter.Y + radius * Math.Sin(i * Math.PI * 2 / 128.0),                                0.0));                     }                     pt.Add(new Point3D(pntCircleCenter.X + radius, pntCircleCenter.Y, 0.0));

                    GeoRegion3D tempCircle = new GeoRegion3D(pt);                     tempCircle.Style3D = gs3D;                     m_sceneControl.Scene.TrackingLayer.Add(tempCircle, "rect");                     break;             }

        }         public void m_sceneControl_MouseUp(object sender, MouseEventArgs e)         {             mouseStatus = false;         }         Point2D ConvertToGlobePoint2D(Point screenPoint)         {             Point2D globePoint = new Point2D();             Point3D globePoint3D = m_sceneControl.Scene.PixelToGlobe(screenPoint);             globePoint.X = globePoint3D.X;             globePoint.Y = globePoint3D.Y;             return globePoint;         }         double CalculateDistance(Point2D point1, Point2D point2)         {             double distance = 0;             distance = Math.Sqrt((point1.X - point2.X) * (point1.X - point2.X) + (point1.Y - point2.Y) * (point1.Y - point2.Y));             return distance;         }     } }

转载于:https://www.cnblogs.com/liwenxue88/p/3443710.html

你可能感兴趣的文章
我明白了一个词 念念相续
查看>>
HDU 3853 期望概率DP
查看>>
ffplay for mfc 代码备忘录
查看>>
android一些面试题目
查看>>
图像切割之(一)概述
查看>>
软工视频(21~25)-软件測试
查看>>
鼠标的操作(先保存到这里,之后进行整理与总结)
查看>>
扩展欧几里得与乘法逆元
查看>>
数据结构—堆排序
查看>>
深入理解 JavaScript 异步系列(3)—— ES6 中的 Promise
查看>>
自己写的一个wpf MVVM 的一个小demo
查看>>
用于记录程序运行时间
查看>>
邮件服务器安全问题
查看>>
结对编程项目作业5
查看>>
lua table 的操作(四)
查看>>
noip2010 引水入城
查看>>
抽象工厂模式(Abstract Factory)
查看>>
wp7 - 在Grid中实现元素叠加
查看>>
实验三
查看>>
mybatis源码阅读-初始化六个工具(六)
查看>>