在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → Delaunay三角剖分算法(基于ArcGIS)

Delaunay三角剖分算法(基于ArcGIS)

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.04M
  • 下载次数:12
  • 浏览次数:77
  • 发布时间:2021-04-20
  • 实例类别:C#语言基础
  • 发 布 人:starmyth
  • 文件格式:.7z
  • 所需积分:2
 相关标签: Delaunay

实例介绍

【实例简介】Delaunay
【实例截图】

【核心代码】

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;

namespace drawtest
{
    public partial class Form1 : Form
    {
       public struct Vertex
       {
           public double x;
           public double y;
           public int ID;
       }

        public struct Triangle
        {
            public int V1Index;
            public int V2Index;
            public int V3Index;
        }

        public static int maxVertex = 300;
        public static int maxTiangle = 500;
        Vertex[] Vertices = new Vertex[maxVertex];
        Triangle[] TinTriangles = new Triangle[maxTiangle];
        public int[,] edges = new int[2, maxTiangle * 3];

        public Form1()
        {
            InitializeComponent();
        }

        private bool InCircle(float xp, float yp, float x1, float y1, float x2, float y2, float x3, float y3, double xc, double yc, double r)
        {
            double eps, m1, m2, mx1, mx2, my1, my2, dx, dy, rsqr, drsqr;
            eps = 0.00000001;
            if (Math.Abs(y1 - y2) < eps && Math.Abs(y2 - y3) < eps)
            {
                MessageBox.Show("INCIRCUM - F - Points are coincident !!");
                return false;
            }
            if (Math.Abs(y2 - y1) < eps)
            {
                m2 = (-(Convert.ToDouble(x3) - Convert.ToDouble(x2)) / (Convert.ToDouble(y3) - Convert.ToDouble(y2)));
                mx2 = Convert.ToDouble((x2   x3) / 2.0);
                my2 = Convert.ToDouble((y2   y3) / 2.0);
                xc = Convert.ToDouble((x2   x1) / 2.0);
                yc = Convert.ToDouble(m2 * (xc - mx2)   my2);
            }
            else if (Math.Abs(y3 - y2) < eps)
            {
                m1 = (-(Convert.ToDouble(x2) - Convert.ToDouble(x1)) / (Convert.ToDouble(y2) - Convert.ToDouble(y1)));
                mx1 = Convert.ToDouble((x1   x2) / 2.0);
                my1 = Convert.ToDouble((y1   y2) / 2.0);
                xc = Convert.ToDouble((x3   x2) / 2.0);
                yc = Convert.ToDouble(m1 * (xc - mx1)   my1);
            }
            else
            {
                m1 = (-(Convert.ToDouble(x2) - Convert.ToDouble(x1)) / (Convert.ToDouble(y2) - Convert.ToDouble(y1)));
                m2 = (-(Convert.ToDouble(x3) - Convert.ToDouble(x2)) / (Convert.ToDouble(y3) - Convert.ToDouble(y2)));
                mx1 = Convert.ToDouble((x1   x2) / 2.0);
                mx2 = Convert.ToDouble((x2   x3) / 2.0);
                my1 = Convert.ToDouble((y1   y2) / 2.0);
                my2 = Convert.ToDouble((y2   y3) / 2.0);
                xc = Convert.ToDouble((m1 * mx1 - m2 * mx2   my2 - my1) / (m1 - m2));
                yc = Convert.ToDouble(m1 * (xc - mx1)   my1);
            }
            dx = (Convert.ToDouble(x2) - Convert.ToDouble(xc));
            dy = (Convert.ToDouble(y2) - Convert.ToDouble(yc));
            rsqr = Convert.ToDouble(dx * dx   dy * dy);
            r = Convert.ToDouble(Math.Sqrt(rsqr));
            dx = Convert.ToDouble(xp - xc);
            dy = Convert.ToDouble(yp - yc);
            drsqr = Convert.ToDouble(dx * dx   dy * dy);
            if (drsqr <= rsqr)
            {
                return true;
            }
            return false;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            IFeatureLayer pFeatureLayer;
            IFeatureClass pFeatureClass;
            IFeature pFeature;
            IQueryFilter pQueryFilter = new QueryFilterClass();
            IPointCollection pPntCollection = new PolygonClass();
            IPointCollection pPntCollection1 = new PolygonClass();
            object missing = Type.Missing;
            int EdgeNum = 0;
            int TriangleNum = 0;
            int i;


            pFeatureLayer = axMapControl1.Map.get_Layer(0) as IFeatureLayer;
            pQueryFilter.WhereClause = "";
            pFeatureClass = pFeatureLayer.FeatureClass;
            int PatchNumber = pFeatureClass.FeatureCount(pQueryFilter);
            int index = 0;
            pPntCollection = pFeatureLayer.FeatureClass.GetFeature(0).Shape as IPointCollection;
            int vertexnum = pPntCollection.PointCount;

            for ( i = 0; i < pPntCollection.PointCount; i  )
            {
                Vertices[i 1].x = pPntCollection.get_Point(i).X;
                Vertices[i 1].y = pPntCollection.get_Point(i).Y;
                Vertices[i 1].ID = pPntCollection.get_Point(i).ID;
            }

            //生成Delaunay
            bool[] Complete = new bool[maxTiangle ];
            double xMin, xMax, yMin, yMax;
            double xMid, yMid, dMax;
            double dx, dy;
            //一般变量
            int  j, k;
            double xc = 0;
            double yc = 0;
            double r = 0;
            bool IsinCirlce = false;

            //找到离散点的最大值和最小值,构建supertriangle
            xMin = Vertices[1].x;
            yMin = Vertices[1].y;
            xMax = xMin;
            yMax = yMin;

            for (  i = 2; i <= vertexnum ; i  )
            {
                if (Vertices[i].x < xMin)
                    xMin = Vertices[i].x;
                if (Vertices[i].x > xMax)
                    xMax = Vertices[i].x;
                if (Vertices[i].y < yMin)
                    yMin = Vertices[i].y;
                if (Vertices[i].y > yMax)
                    yMax = Vertices[i].y;
            }

            dx = xMax - xMin;
            dy = yMax - yMin;

            if (dx > dy)
                dMax = dx;
            else dMax = dy;

            xMid = (xMax   xMin) / 2;
            yMid = (yMax   yMin) / 2;

            //将包含所有点的supertriangle作为第一个三角形
            Vertices[vertexnum   1].x = xMid - 2 * dMax;
            Vertices[vertexnum   1].y = yMid - dMax;
            Vertices[vertexnum   2].x = xMid;
            Vertices[vertexnum   2].y = yMid   2 * dMax;
            Vertices[vertexnum   3].x = xMid   2 * dMax;
            Vertices[vertexnum   3].y = yMid - dMax;

            TinTriangles[1].V1Index =vertexnum   1;
            TinTriangles[1].V2Index = vertexnum   2;
            TinTriangles[1].V3Index = vertexnum   3;

            Complete[1] = false;
            TriangleNum = 1;

            for ( i = 1; i <= vertexnum; i  )
            {
                EdgeNum = 0;
                j = 0;

                do
                {
                    j = j   1;
                    if (Complete[j] != true)
                    {
                        IsinCirlce = InCircle((float ) Vertices[i].x,(float ) Vertices[i].y,(float ) Vertices[TinTriangles[j].V1Index].x,(float ) Vertices[TinTriangles[j].V1Index].y,(float ) Vertices[TinTriangles[j].V2Index].x,(float)Vertices[TinTriangles[j].V2Index].y,(float ) Vertices[TinTriangles[j].V3Index].x,(float ) Vertices[TinTriangles[j].V3Index].y, xc, yc, r);

                    }

                    if (IsinCirlce)
                    {
                        edges [0,EdgeNum 1 ] = TinTriangles[j].V1Index;
                        edges[1, EdgeNum   1] = TinTriangles[j].V2Index;
                        edges[0, EdgeNum   2] = TinTriangles[j].V2Index;
                        edges[1, EdgeNum   2] = TinTriangles[j].V3Index;
                        edges[0, EdgeNum   3] = TinTriangles[j].V3Index;
                        edges[1, EdgeNum   3] = TinTriangles[j].V1Index;
                        EdgeNum = EdgeNum   3;
                        TinTriangles[j].V1Index = TinTriangles[TriangleNum].V1Index;
                        TinTriangles[j].V2Index = TinTriangles[TriangleNum].V2Index;
                        TinTriangles[j].V3Index = TinTriangles[TriangleNum].V3Index;
                        Complete[j] = Complete[TriangleNum];
                        j = j - 1;
                        TriangleNum = TriangleNum - 1;
                    }
                }
                while (j < TriangleNum);

                for (j = 1; j < EdgeNum; j  )
                {
                    if (edges[0,j ] != 0 && edges[1,j ] != 0)
                    {
                        for (k = j   1; k <= EdgeNum; k  )
                        {
                            if (edges[0,k ] != 0 && edges[1,k ] != 0)
                            {
                                if (edges[0,j ] == edges[1,k ])
                                {
                                    if (edges[1,j ] == edges[0,k ])
                                    {
                                        edges[0,j ] = 0;
                                        edges[1,j ] = 0;
                                        edges[0,k ] = 0;
                                        edges[1,k ] = 0;
                                    }
                                }
                            }

                        }
                    }
                }

                for (j = 1; j <= EdgeNum; j  )
                {
                    if (edges[0,j ] != 0 && edges[1,j ] != 0)
                    {
                        TriangleNum = TriangleNum   1;
                        TinTriangles[TriangleNum].V1Index = edges[0,j ];
                        TinTriangles[TriangleNum].V2Index = edges[1,j ];
                        TinTriangles[TriangleNum].V3Index = i;
                        Complete[TriangleNum] = false;
                    }
                }
            }

            i = 0;
            do
            {
                i = i   1;
                if (TinTriangles[i].V1Index > vertexnum || TinTriangles[i].V2Index > vertexnum || TinTriangles[i].V3Index > vertexnum)
                {
                    TinTriangles[i].V1Index = TinTriangles[TriangleNum].V1Index;
                    TinTriangles[i].V2Index = TinTriangles[TriangleNum].V2Index;
                    TinTriangles[i].V3Index = TinTriangles[TriangleNum].V3Index;
                    i = i - 1;
                    TriangleNum = TriangleNum - 1;
                }
            }
            while (i < TriangleNum);

            int many = TriangleNum;
 
        



            //画Delaunay
            ILine pLine = new LineClass();
            ILine pLine1 = new LineClass();
            ILine pLine2 = new LineClass();

            //IPointCollection pPntCollection1 = new PolylineClass();
            IPoint pPoint1 = new PointClass();
            IPoint pPoint2 = new PointClass();
            IPoint pPoint3 = new PointClass();
            IGraphicsContainer pContainer = axMapControl1.Map as IGraphicsContainer;
            index = 0;
           



            for (i = 1; i <= TriangleNum; i  )
            {


                pPoint1.X = Vertices[TinTriangles[i].V1Index].x;
                pPoint1.Y = Vertices[TinTriangles[i].V1Index].y;
                pPoint2.X = Vertices[TinTriangles[i].V2Index].x;
                pPoint2.Y = Vertices[TinTriangles[i].V2Index].y;
                pPoint3.X = Vertices[TinTriangles[i].V3Index].x;
                pPoint3.Y = Vertices[TinTriangles[i].V3Index].y;
                pLine.PutCoords(pPoint1, pPoint2);
                pLine1.PutCoords(pPoint2, pPoint3);
                pLine2.PutCoords(pPoint1, pPoint3);

                ISegmentCollection pSeg = new PolylineClass() as ISegmentCollection;
                pSeg.AddSegment((ISegment)pLine, ref missing, ref missing);
                IPolyline pPolyline = pSeg as IPolyline;

                ISimpleLineSymbol pSimpleLineSymbol = new SimpleLineSymbolClass();
                pSimpleLineSymbol.Style = esriSimpleLineStyle.esriSLSDot;
                ILineElement pLineElement = new LineElementClass();
                IElement pElement = pLineElement as IElement;
                pElement.Geometry = pPolyline as IGeometry;

                pContainer.AddElement(pLineElement as IElement, index  );

                ISegmentCollection pSeg1 = new PolylineClass() as ISegmentCollection;
                pSeg1.AddSegment((ISegment)pLine1, ref missing, ref missing);
                IPolyline pPolyline1 = pSeg1 as IPolyline;

                ISimpleLineSymbol pSimpleLineSymbol1 = new SimpleLineSymbolClass();
                pSimpleLineSymbol1.Style = esriSimpleLineStyle.esriSLSDot;
                ILineElement pLineElement1 = new LineElementClass();
                IElement pElement1 = pLineElement1 as IElement;
                pElement1.Geometry = pPolyline1 as IGeometry;

                pContainer.AddElement(pLineElement1 as IElement, index  );

                ISegmentCollection pSeg2 = new PolylineClass() as ISegmentCollection;
                pSeg2.AddSegment((ISegment)pLine2, ref missing, ref missing);
                IPolyline pPolyline2 = pSeg2 as IPolyline;

                ISimpleLineSymbol pSimpleLineSymbol2 = new SimpleLineSymbolClass();
                pSimpleLineSymbol2.Style = esriSimpleLineStyle.esriSLSDot;
                ILineElement pLineElement2 = new LineElementClass();
                IElement pElement2 = pLineElement2 as IElement;
                pElement2.Geometry = pPolyline2 as IGeometry;

                pContainer.AddElement(pLineElement2 as IElement, index  );
            }



            axMapControl1.CenterAt(pPoint2);
            Application.DoEvents();

            axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, pContainer as object, axMapControl1.ActiveView.Extent);

            MessageBox.Show("OK");

                
    
            
            
        }




    }
}

标签: Delaunay

实例下载地址

Delaunay三角剖分算法(基于ArcGIS)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警