高效的数学算法来计算交点

编程入门 行业动态 更新时间:2024-10-13 20:15:17
本文介绍了高效的数学算法来计算交点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

对于比赛我开发我需要一个算法,可以计算交叉点。我已经解决了这个问题,但我已经做它的方式是非常讨厌的,我希望有人在这里可能有一个更好的解决方案。

For a game I am developing I need an algorithm that can calculate intersections. I have solved the problem, but the way I have done it is really nasty and I am hoping someone here might have a more elegant solution.

一对重present它们之间的连线的终点点。鉴于两对点,做画线相交,如果是这样,在什么时候?

A pair of points represent the end points of a line drawn between them. Given two pairs of points, do the drawn lines intersect, and if so, at what point?

因此​​,例如调用线(AX,AY) - (BX,BY)和(CX,CY) - (DX,DY)

So for example call the lines (A.x, A.y)-(B.x, B.y) and (C.x, C.y)-(D.x, D.y)

谁能想到了解决办法?在任何语言中的解决方案就可以了。

Can anyone think of a solution? A solution in any language will do.

编辑:有一点我应该更清楚,该算法必须返回false,如果交点超出线段的长度。 I.E.这是不是一种相贯线: lines.gif

A point I should have made clearer, the algorithm must return false if the point of intersection is beyond the lengths of the line segments. I.E. this isn't an intersecition: lines.gif

推荐答案

大多数问题的答案已经在这里似乎遵循的总体思路是:

Most of the answers already here seem to follow the general idea that:

  • 找到两条直线传球给定的点的交集。
  • 确定的交点属于这两个线段。
  • 但是,当路口不经常发生,更好的方法可能是扭转这些步骤:

    But when intersection does not occur often, a better way probably is to reverse these steps:

  • EX preSS中的 Y = AX + B 的(行通过A,B)和 Y = CX + D 的(行传递的形式直线C,D)
  • 在看C和D是在同一侧的 Y = AX + B 的
  • 在看,如果A和B是在同一侧的 Y = CX + D 的
  • 如果答案以上都否,然后出现是的交集。否则就没有交集。
  • 找到的交集,如果存在的话。
  • express the straight lines in the form of y = ax + b (line passing A,B) and y = cx + d (line passing C,D)
  • see if C and D are on the same side of y = ax+b
  • see if A and B are on the same side of y = cx+d
  • if the answer to the above are both no, then there is an intersection. otherwise there is no intersection.
  • find the intersection if there is one.
  • 请注意:做第2步,只是检查(赛扬 - 一(CX) - b)和(240 - 一(DX) - B)具有相同的符号。步骤3是类似的。步骤5是从两个等式只是标准的数学。

    Note: to do step 2, just check if (C.y - a(C.x) - b) and (D.y - a(D.x) - b) have the same sign. Step 3 is similar. Step 5 is just standard math from the two equations.

    此外,如果你需要在每个线段与(N-1)等线段,precomputing步骤1中的所有线路节省您的时间进行比较。

    Furthermore, if you need to compare each line segment with (n-1) other line segments, precomputing step 1 for all lines saves you time.

    更多推荐

    高效的数学算法来计算交点

    本文发布于:2023-11-29 21:19:18,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1647658.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:高效   交点   算法   数学

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!