Given two points and two vectors, find point of intersection [duplicate]

无人久伴 提交于 2019-12-01 08:57:28
Roee Gavirel

It's simple math.

But, first, check that you have intersection. If both vector are parallel you will fail to solve that:

// Edit this line to protect from division by 0 
if (Vy == 0 && Uy == 0) || ((Vy != 0 && Uy != 0 && (Vx/Vy == Ux/Uy)) // => Fail.

Then (I want show the calculation cause they long but):

R = (AxUy - AyUx + ByUx - BxUy) / (VyUx - VxUy)  
S = (Ax - Bx + RVx) / Ux  

Hope that helped you.

Ram

Looks like an assignment problem to me. Here is the logic that will help you write the code.

Let us call the first Ray as R0.
Locus of a point on R0 is defined as P:

P = P0 + alpha x V0

For the second ray R1:

P = P1 + beta x V1

Since they should intersect:

P0 + alpha x V0 = P1 + beta x V1

alpha and beta are unknowns and we have two equations in x any y.

Solve for the unknowns and get back the point of intersection.

i.e.,

P0.x + alpha * V0.x = P1.x + beta * V1.x
P0.y + alpha * V0.y = P1.y + beta * V1.y

solve for alpha and beta.

If there is a real positive solution for both alpha and beta, rays intersect.
If there is a real but at least one negative solution for both alpha and beta, extended rays intersect.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!