在GIS中,如果一个面存在自相交的情况,那么他是一个存在错误拓扑关系的面,后续影响分析,如何判断呢?封装了一个checkCross的方法,作为判断。
using SuperMap.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SuperMap.SampleCode.Mapping
{
public static class gemoetryz
{
private static int dblcmp(double a, double b)
{
if (Math.Abs(a - b) <= 0.00006)
return 0;
if (a > b)
return 1;
else
return -1;
}
private static double dot(double x1, double y1, double x2, double y2)
{
return x1 * x2 + y1 * y2;
}
private static int point_on_line(Point2D a, Point2D b, Point2D c)
{
double res = dot(b.X - a.X, b.Y - a.Y, c.X - a.X, c.Y - a.Y);//[self dot:b.x - a.x y1:b.y - a.y x2:c.x - a.x y2:c.y - a.y]
return dblcmp(res, 0);
}
private static double cross(double x1, double y1, double x2, double y2)
{
return x1 * y2 - x2 * y1;
}
private static double ab_cross_ac(Point2D a, Point2D b, Point2D c)
{
return cross(b.X - a.X, b.Y - a.Y, c.X - a.X, c.Y - a.Y);
}
private static int ab_cross_cd(Point2D a, Point2D b, Point2D c, Point2D d)
{
double s1, s2, s3, s4;
int d1, d2, d3, d4;
Point2D p = new Point2D();
d1 = dblcmp(s1 = ab_cross_ac(a, b, c), 0);
d2 = dblcmp(s2 = ab_cross_ac(a, b, d), 0);
d3 = dblcmp(s3 = ab_cross_ac(c, d, a), 0);
d4 = dblcmp(s4 = ab_cross_ac(c, d, b), 0);
if ((d1 ^ d2) == -2 && (d3 ^ d4) == -2)
{
p.X = (float)((c.X * s2 - d.X * s1) / (s2 - s1));
p.Y = (float)((c.Y * s2 - d.Y * s1) / (s2 - s1));
return 1;
}
if(d1==0&&point_on_line(c,a,b)<=0)
{
p=c;
return 0;
}
if(d2==0&&point_on_line(d,a,b)<=0)
{
p=d;
return 0;
}
if(d3==0&&point_on_line(a,c,d)<=0)
{
p=a;
return 0;
}
if(d4==0&&point_on_line(b,c,d)<=0)
{
p=a;
return 0;
}
//如果不相交
return -1;
}
/// <summary>
/// 判断是否面有自相交
/// </summary>
/// <param name="region"></param>
/// <returns></returns>
public static Boolean checkCross(GeoRegion region)
{
Boolean isCorss=false;
Point2Ds points=region[0];
int count = points.Count;
for(int i=0;i<count-1;i++)
{
for(int j=i+1;j<count;j++)
{
Point2D point3=new Point2D();
if(j==count-1)
{
point3 = points[0];
}
else
{
point3=points[j+1];
}
Point2D point=points[i];
Point2D point1=points[i+1];
Point2D point2=points[j];
if(ab_cross_cd(point,point1,point2,point3)==1)
{
return true;
}
}
}
return isCorss;
}
}
}
网友评论