Efficient maths algorithm to calculate intersections

后端 未结 9 787
既然无缘
既然无缘 2020-11-28 21:40

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 someon

9条回答
  •  囚心锁ツ
    2020-11-28 22:36

    Well, mathematics and scalar products can help there.
    - Say you want to intersect segments [AB] and [CD]
    - Suppose the line intersection of the lines is M

    M is inside segment [ÅB] if and only if
    Vector(AB) . Vector(AM) >= 0
    and
    Vector(AB) . Vector(MB) >= 0

    Where the dot "." denotes a scalar product : u . v = ux * vx + uy * vy

    So, your algo is :
    - find M
    - M is inside both segment if and only if the 4 eq below are true
    Vector(AB) . Vector(AM) >= 0
    Vector(AB) . Vector(MB) >= 0
    Vector(CD) . Vector(CM) >= 0
    Vector(CD) . Vector(MD) >= 0

    Hope this helps

提交回复
热议问题