# 判断线段与多边形、多边形与多边形是否相交的算法（C#）

0

```public bool GetLineIntersection(Graphic Graphic, Graphic DrawPolygonGraphic)
{
double distAB, theCos, theSin, newX, ABpos;
double X = 0;
double Y = 0;
DrawPolygon = DrawPolygonGraphic.Geometry as Polygon;

if (Graphic.Geometry.GetType().ToString() == "ESRI.ArcGIS.Client.Geometry.Polyline")
{
polyline = Graphic.Geometry as Polyline;
foreach (var pointCollection in DrawPolygon.Rings)
{
PointCollection drawlist = pointCollection;
foreach (var pc in polyline.Paths)
{
PointCollection linelist = pc;
for (int i = 0; i < drawlist.Count - 1; i++)
{
for (int j = 0; j < linelist.Count - 1; j++)
{
if ((drawlist[i + 1].Y - drawlist[i].Y) * (linelist[j].X - linelist[j + 1].X) - (drawlist[i + 1].X - drawlist[i].X) * (linelist[j].Y - linelist[j + 1].Y) == 0)
{
return false;
}
X = ((drawlist[i + 1].X - drawlist[i].X) * (linelist[i].X - linelist[i + 1].X) * (linelist[i].Y - drawlist[i].Y) - linelist[i].X * (drawlist[i + 1].X - drawlist[i].X) *
(linelist[i].Y - linelist[i + 1].Y) + drawlist[i].X * (drawlist[i + 1].Y - drawlist[i].Y) * (linelist[i].X - linelist[i + 1].X)) /
((drawlist[i + 1].Y - drawlist[i].Y) * (linelist[i].X - linelist[i + 1].X) - (drawlist[i + 1].X - drawlist[i].X) * (linelist[i].Y - linelist[i + 1].Y));

Y = ((drawlist[i + 1].Y - drawlist[i].Y) * (linelist[i].Y - linelist[i + 1].Y) * (linelist[i].X - drawlist[i].X) - linelist[i].Y * (drawlist[i + 1].Y - drawlist[i].Y) *
(linelist[i].X - linelist[i + 1].X) + drawlist[i].Y * (drawlist[i + 1].X - drawlist[i].X) * (linelist[i].Y - linelist[i + 1].Y)) /
((drawlist[i + 1].X - drawlist[i].X) * (linelist[i].Y - linelist[i + 1].Y) - (drawlist[i + 1].Y - drawlist[i].Y) * (linelist[i].X - linelist[i + 1].X));

if ((X - drawlist[i].X) * (X - drawlist[i + 1].X) <= 0 && (X - linelist[i].X) * (X - linelist[i + 1].X) <= 0 &&
(Y - drawlist[i].Y) * (Y - drawlist[i + 1].Y) <= 0 && (Y - linelist[i].Y) * (Y - linelist[i + 1].Y) <= 0)
{
return true;
}
}
}
}
}
}
return false;
}```
CynthiaTT | 菜鸟二级 | 园豆：204

您需要登录以后才能回答，未注册用户请先注册