2d cross product definition

后端 未结 3 493
逝去的感伤
逝去的感伤 2021-01-23 21:12

In determine if line segment is inside polygon I noticed the accepted answer has an unusual 2d cross roduct definition of:

(u1, u2) x (v1, v2) := (u1 - v2)*(u2 -         


        
3条回答
  •  情歌与酒
    2021-01-23 22:07

    not a math expert but CROSS product in ND is defined as operation of N-1 vectors resulting in vector perpendicular to each. The stuff is computed as determinant of matrix where its first line are unit direction vectors (i,j,k,...) and each of the other lines holds each vector operand. So for 2D it is:

    cross( (x0,y0) ) = | i  j  | = i*y0 - j*x0 = (y0,-x0)
                       | x0 y0 |
    

    which is perpendicular to (x0,y0). So what you have is not a 2D cross product !!!

    Its usual in CG to need a normal vector to some 2D surface that is obtained by 3D cross product:

    cross( (x0,y0,z0),(x1,y1,z1) ) = | i  j  k  | = i*(y0*x1-z0*y1) + j*(z0*x1-x0*z1) + k*(x0*y1-y0*x1)
                                     | x0 y0 z0 |
                                     | x1 y1 z1 |
    

    Now if the two vectors (x0,y0,z0),(x1,y1,z1) are 2D then z0,z1 are both zero so:

    cross( (x0,y0,z0),(x1,y1,z1) ) = i*(y0*x1-0*y1) + j*(0*x1-x0*0) + k*(x0*y1-y0*x1)
    cross( (x0,y0,z0),(x1,y1,z1) ) = k*(x0*y1-y0*x1)
    cross( (x0,y0,z0),(x1,y1,z1) ) = (0,0,x0*y1-y0*x1)
    

    This is more similar to your definition but does not look the same so what you have is one of these:

    1. something different not a cross product
    2. cross product converted with some math identity I do not see yet.
    3. bug in that answer (minor typo, or copied wrong line of code ... happen to me all the time too)
    4. more equations fused together (cross is just a small part of that answer)

    in context of the linked answer you need the 3D cross products z coordinate result:

    z = x0*y1-y0*x1
    

    which sign will tell you if the points is CW or CCW in respect to polygon winding rule and one of its edges...

    But to be absolutely clear you should ask this Niklas B. in that question thread directly (using comment) since you're low rep I will do it for you and link your question there ...

提交回复
热议问题