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

CynthiaGT 2015-07-10 04:26:04
判断线段与多边形、多边形与多边形是否相交(C#),包括线段、多边形包含在多边形内,线段与多边形某一条边重合的情况判断。求教各位大神!
我有写一个判断线段与多边形的边时候相交的算法,但是貌似没有判断成功,请各位大神指导!谢谢!
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;
}
...全文
2348 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
CynthiaGT 2015-07-13
  • 打赏
  • 举报
回复
你好!请问 RectangleF.IntersectsWith 方法怎么写?
xuzuning 2015-07-13
  • 打赏
  • 举报
回复
给你一个演示
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 多边形相交
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.HighQuality;

using (var p1 = new GraphicsPath())
{
p1.AddPolygon(new PointF[] { new PointF(10, 20), new PointF(80, 160), new PointF(200, 25)});
g.DrawPath(Pens.Green, p1);
var p2 = new GraphicsPath();
p2.AddPolygon(new PointF[] { new PointF(210, 200), new PointF(100, 100), new PointF(220, 30) });
g.DrawPath(Pens.Red, p2);

var box1 = p1.GetBounds();
var box2 = p2.GetBounds();

if (box1.IntersectsWith(box2))
{
var pen1 = new Pen(Color.Green, 1);
pen1.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
pen1.DashPattern = new float[] { 5, 5 };
g.DrawRectangle(pen1, box1.X, box1.Y, box1.Width, box1.Height);

var pen2 = new Pen(Color.Red, 1);
pen2.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
pen2.DashPattern = new float[] { 5, 5 };
g.DrawRectangle(pen2, box2.X, box2.Y, box2.Width, box2.Height);

var box3 = RectangleF.Intersect(box1, box2);
var pen3 = new Pen(Color.Black, 3);
pen3.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
pen3.DashPattern = new float[] { 1, 1 };
g.DrawRectangle(pen3, box3.X, box3.Y, box3.Width, box3.Height);

}
}
}
}
}

红、绿实线为多边形,红、绿虚线框分别是他们的控制矩形
黑虚线框是两个控制矩形的相交矩形
你可据此写出分析算法,而不是一味的循环
xuzuning 2015-07-10
  • 打赏
  • 举报
回复
点是否在多边形内(含边界上)
        private bool pointInPolygon(float x, float y, PointF[] polygon)
        {
            int i;
            int j = polygon.Length - 1;
            bool oddNodes = false;

            for (i = 0; i < polygon.Length; i++)
            {
                if ((polygon[i].Y < y && polygon[j].Y >= y || polygon[j].Y < y && polygon[i].Y >= y)
                    && (polygon[i].X <= x || polygon[j].X <= x))
                {
                    if (polygon[i].X + (y - polygon[i].Y) / (polygon[j].Y - polygon[i].Y) * (polygon[j].X - polygon[i].X) < x)
                    {
                        oddNodes = !oddNodes;
                    }
                }
                j = i;
            }
            return oddNodes;
        }
判断多边形、线段是否和另一多边形相交,可先判断他们的控制矩形是否相交 RectangleF.IntersectsWith 方法 更可以用 RectangleF.IntersectsWith 取回控制举行的交集 然后再做 点是否在多边形中的判断 用 GraphicsPath.IsVisible 可能更快

679

社区成员

发帖
与我相关
我的任务
社区描述
智能路由器通常具有独立的操作系统,包括OpenWRT、eCos、VxWorks等,可以由用户自行安装各种应用,实现网络和设备的智能化管理。
linuxpython 技术论坛(原bbs)
社区管理员
  • 智能路由器社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧