给定两个点和两个向量,求交点 [英] Given two points and two vectors, find point of intersection

查看:1059
本文介绍了给定两个点和两个向量,求交点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何检测两个线段相交的地方?

Possible Duplicate:
How do you detect where two line segments intersect?

给出两个点ab以及两个向量vu,我想找到第三个点c,它是按以下方式相交的点:

Given two points a and b plus two vectors v and u I want to find a third point c, which is the point of intersection in the following manner:

vector2 intersection(vector2 a, vector2 v, vector2 b, vector2 u)
{
    float r, s;

    a + r * v = b + s * u;
    r * v - s * u = b - a

    r * v.x - s * u.x = b.x - a.x
    r * v.y - s * u.y = b.y - a.y
}

除了使用高斯消除来解决该系统之外,还有其他方法吗?还是这是处理此问题的最佳方法(或至少是可接受的方法)?

Is there any other way than using gaussian elimination to solve this system? Or is this the best (or at least an acceptable) way to handle this?

编辑: vector2

typedef union vector2
{
    float v[2];
    struct { float x, y; };
} vector2;

ab的类型也为vector2,因为点和向量之间的唯一区别在于通过仿射变换对其进行变换的方式.

a and b are also of type vector2, because the only difference between a point and a vector is in the the way it is transformed by an affine transformation.

推荐答案

对我来说似乎是一个分配问题.这是可以帮助您编写代码的逻辑.

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

让我们将第一条射线称为R0.
R0上一个点的轨迹定义为P:

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

P = P0 + alpha x V0

对于第二条射线R1:

P = P1 + beta x V1

因为它们应该相交:

P0 + alpha x V0 = P1 + beta x V1

alphabeta是未知数,在x中任何y中都有两个方程式.

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.

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

求解alphabeta.

如果同时存在alphabeta的正解,则光线相交.
如果alphabeta都存在一个实数,但至少有一个负解,则扩展光线会相交.

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.

这篇关于给定两个点和两个向量,求交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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