【问题标题】:Could someone help me translate this short code snippet into python?有人可以帮我把这个简短的代码片段翻译成 python 吗?
【发布时间】:2013-02-11 00:30:52
【问题描述】:

我正在尝试使用this tutorial 围绕等距图块的坐标系展开思考。除了最后一个 sn-p 之外,我基本上已经弄清楚了,我在下面复制它以避免不必要的点击 =)

/**
* 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 处理矩阵变换。

非常感谢您!

【问题讨论】:

  • 查看 github.com/natural/java2python 以获取执行此操作的工具
  • 绝对不是 Java。它可能是 ActionScript (Flash)。
  • 参数列表A : Vector - 不能是纯Java。 (变量声明也一样)
  • 两件事表明这不是Java:Java没有const关键字; Java中的参数和局部变量类型出现在标识符之前(Vector2 a等)。
  • @blz:这不能通过花哨的导入来完成,那些不能改变语言语法。该页面的作者也没有声称它是Java,最后的演示是Flash。这是动作脚本。

标签: python actionscript matrix numpy


【解决方案1】:
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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-03
    • 2016-03-08
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 2015-07-14
    相关资源
    最近更新 更多