如何判断一个线相交在C#中的多边形? [英] How to tell if a line intersects a polygon in C#?
问题描述
我有一个问题非常类似: <一href="http://stackoverflow.com/questions/30080/how-to-know-if-a-line-intersects-a-plane-in-c-basic-2d-geometry">http://stackoverflow.com/questions/30080/how-to-know-if-a-line-intersects-a-plane-in-c-basic-2d-geometry
I have a question very similar to this: http://stackoverflow.com/questions/30080/how-to-know-if-a-line-intersects-a-plane-in-c-basic-2d-geometry
我正在寻找一种方法,告诉如果一条线相交的任意多边形在C#。
I am searching for a method that tells if a line is intersecting an arbitrary polygon in C#.
我觉得从克里斯 - marasti - 格奥尔格·算法是非常有益的,但是缺少最重要的方法 - 线对线相交
I think the algorithm from chris-marasti-georg was very helpful but missing the most important method - line to line intersection.
有谁知道的交线的方法来完成克里斯Marasti - 乔格的code或有什么相似?
Does anyone know of a line intersection method to complete Chris Marasti-Georg's code or have anything similar?
有没有在C#中内置的code这个?
Is there a built-in code for this in c#?
这方法是与Bing地图算法,增强了禁区功能的使用。由此产生的路径不能穿过禁区(任意多边形)。
This method is for use with the Bing Maps algorithm enhanced with a forbidden area feature. The resulting path must not pass through forbidden area (the arbitrary polygon).
感谢您! /埃里克
推荐答案
没有内建code的边缘检测内置在.NET框架。
There is no builtin code for edge detection built into the .NET framework.
下面是code(移植到C#),做你所需要的(实际的算法是在comp.graphics.algorithms发现在谷歌组):
Here's code (ported to C#) that does what you need (the actual algorithm is found at comp.graphics.algorithms on Google groups) :
public static PointF FindLineIntersection(PointF start1, PointF end1, PointF start2, PointF end2)
{
float denom = ((end1.X - start1.X) * (end2.Y - start2.Y)) - ((end1.Y - start1.Y) * (end2.X - start2.X));
// AB & CD are parallel
if (denom == 0)
return PointF.Empty;
float numer = ((start1.Y - start2.Y) * (end2.X - start2.X)) - ((start1.X - start2.X) * (end2.Y - start2.Y));
float r = numer / denom;
float numer2 = ((start1.Y - start2.Y) * (end1.X - start1.X)) - ((start1.X - start2.X) * (end1.Y - start1.Y));
float s = numer2 / denom;
if ((r < 0 || r > 1) || (s < 0 || s > 1))
return PointF.Empty;
// Find intersection point
PointF result = new PointF();
result.X = start1.X + (r * (end1.X - start1.X));
result.Y = start1.Y + (r * (end1.Y - start1.Y));
return result;
}
这篇关于如何判断一个线相交在C#中的多边形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!