有人可以帮助我将这个短代码片段翻译成python吗?

时间:2013-02-10 16:19:15

标签: python actionscript matrix numpy

我正试图用this tutorial围绕等距瓷砖的坐标系。除了最后一个片段之外,我已经把它弄清楚了,我在下面复制以避免不必要的点击=)

/**
* 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?),但我们的想法是获取屏幕坐标并将它们投影到地图空间。下图给出了正在完成的工作的示意图:

enter image description here

给定一个点P,我们希望找到up轴和right轴上的交点。不幸的是,我的矩阵代数(非常)生锈,因此我无法推断出代码中的内容。 python翻译将有助于我解决这个问题。

重要的一点是:我使用2D numpy数组来表示我的地图,因此理想情况下矩阵变换应该通过numpy来处理。

非常感谢你!

1 个答案:

答案 0 :(得分:4)

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;