简体   繁体   中英

Could someone help me translate this short code snippet into python?

I'm trying to wrap my head around coordinate systems for isometric tiles using this tutorial . I've got it mostly figured out except for the last snippet, which I am copying below to avoid unnecessary clicking =)

/**
* 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;
}

I'm not quite sure what language this is written in (Java? ActionScript?), but the idea is to to take screen coordinates and to project them onto map space. The figure below gives a schematic overview of what's being done:

在此处输入图片说明

Given a point P , we want to find the point of intersection along the up axis and the right axis. Unfortunately my matrix algebra is (very) rusty so I'm having trouble deducing what's being done in the code. A python translation would go a long way towards helping me figure this out.

One important point: I'm using a 2D numpy array to represent my map, so matrix transformations should ideally be handled through numpy.

Thank you very much in advance!

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM