[英]Could someone help me translate this short code snippet into python?
我正在尝试使用本教程将头部包裹在等距图块的坐标系周围。 我已经弄清楚了,除了最后一个代码段,我在下面复制了它,以避免不必要的点击=)
/**
* Intersect two line segments defined by A->B and C->D
*
* Returns the time of intersection along A->B
*/
public function RayIntersect(A : Vector2, B : Vector2, C : Vector2, D : Vector2) : Number
{
// turn C->D into a plane...
const n : Vector2 = D.Sub(C).m_Perp;
// ...and do the regular plane vs ray maths
const numerator : Number = C.Sub(A).Dot(n);
const denom : Number = B.Sub(A).Dot(n);
return numerator / denom;
}
我不太确定这是用什么语言编写的(Java?ActionScript?),但其想法是获取屏幕坐标并将其投影到地图空间上。 下图给出了正在执行的操作的示意图:
给定点P
,我们想找到沿up
轴和right
轴的交点。 不幸的是,我的矩阵代数是(非常)生锈的,所以我很难推断出代码中正在做什么。 python翻译将大大帮助我解决这个问题。
重要一点:我使用2D numpy数组表示地图,因此理想情况下,应通过numpy处理矩阵转换。
提前非常感谢您!
def ray_intersect(A, B, C, D):
"""
Intersect two line segments defined by A->B and C->D
Returns the time of intersection along A->B
"""
# turn C->D into a plane...
E = D-C
n = np.array((-E[1], E[0]))
# ...and do the regular plane vs ray maths
numerator = np.dot(C-A, n)
denom = np.dot(B-A, n)
return numerator / denom;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.