做两线段相交于C#或vb.net使用XNA? [英] Do two line segments intersect in c# or vb.net using xna?

查看:128
本文介绍了做两线段相交于C#或vb.net使用XNA?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找的年龄,如果发现两条线段(2套X,Y共同ords每个)拦截的一个合适的方法。我见过许多人(包括:<一href="http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect">How你发现,其中两条线段相交?),但所有的一个我见过有瑕疵。主要是它们没有检测到碰撞,如果线是平行的,但彼此重叠。

I have been looking for ages for a suitable method of finding if two line segments (2 sets of x,y co-ords each) intercept. I have seen many (including: How do you detect where two line segments intersect?), but all the one I have seen have flaws. Mainly that they do not detect a collision if the lines are parallel but overlap each other.

我也不需要交点退换,只是一个布尔值就可以了。

I also do not need the point of intersection to be returned, just a boolean would be fine.

我会很感激,如果有人可以点我在正确的方向余弦显然我吸的几何形状。 :(

I would be really grateful if someone could point me in the right direction cos apparently I suck at geometry. :(

推荐答案

当然,还有数学上优雅的方式来做到这一点,但如果你正在寻找一个易于理解的使用基本代数算法,试试这个(这不是code):

There are certainly mathematically elegant ways to do it, but if you are looking for an easy-to-understand algorithm using basic algebra, try this (this is NOT code):

让我们通过自己的终端定义两个线段:

Let's define two line segments by their endpoints:

l0 : { (x0, y0), (x1, y1) }
l1 : { (x2, y2), {x3, y3) }

首先,获得每条线的斜率截距形式。你可以看一下公式M,B或自己获得他们:

First, obtain the slope-intercept form of each line. You can look up the formulae for m, b or derive them yourself:

l0 : m0 * x + b0
l1 : m1 * x + b1

如果(M0!= M1)的线不平行。要找到潜在的交叉点解决 10 = L1

if (m0 != m1) the lines are not parallel. To find the potential intersection point solve l0 = l1:

x = (b1 - b0) / (m0 - m1)
y = m0 * x + b0

该段相交,当且仅当(X,Y)位于两个部分。请注意,这足以只检查的x坐标,因为我们已经建立了(X,Y)是两条线:

The segments will intersect if and only if (x, y) lies on both segments. Note that it is sufficient to check only the x-coordinate since we have already established that (x, y) is on both lines:

<打击>

(x0 <= x <= x1) AND (x2 <= x <= x3) AND (y0 <= y <= y1) AND (y2 <= y <= y3)

min(x0, x1) <= x <= max(x0, x1) AND min(x2, x3) <= x <= max(x2, x3)

如果线是平行的,你想知道他们是否重叠,替换一条线的端点对于x以上(您只需要在对面线测试,虽然)

If the lines are parallel and you want to know if they overlap, substitute the endpoints of one line for x above (you only need test against the opposite line, though)

希望这有助于。祝你好运!

Hope this helps. Good luck!

这篇关于做两线段相交于C#或vb.net使用XNA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆