给定两个点和两个向量,求交点 [英] Given two points and two vectors, find point of intersection
问题描述
可能重复:
如何检测两个线段相交的地方?
Possible Duplicate:
How do you detect where two line segments intersect?
给出两个点a
和b
以及两个向量v
和u
,我想找到第三个点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;
a
和b
的类型也为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
alpha
和beta
是未知数,在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
求解alpha
和beta
.
如果同时存在alpha
和beta
的正解,则光线相交.
如果alpha
和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.
这篇关于给定两个点和两个向量,求交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!