c++ - How can this code retrieve a 2D vector from a cross-product of two 2D vectors? -
i in lost. have been trying implement code at:http://www.blackpawn.com/texts/pointinpoly/default.html
however, don't know how possible cross-product present there between 2 2d vectors can result in 2d vector. not make sense me. present in examples of intersection between polygons , lines, in fine book "realtime collision detection" - scalar triples between 2d vectors appear in codes (see page 189, instance).
the issue that, far can think of it, pseudo cross-product of 2 2d vectors can result in scalar (v1.xv2.y-v1.yv2.x) or @ in 3d vector if 1 adds 2 zeros, since scalar represents z dimension. how can result in 2d vector?
i not first 1 ask and, coincidently, when trying use same code example: cross product of 2 2d vectors however, can seen, answer, original question when updated , comments in thread ended being quite mess, if dare so.
does know how should these 2d vectors cross-product of 2 2d vectors? if code provided, can handle c#, javascript , c++.
edit - here piece of code in book mentioned above:
int intersectlinequad(point p, point q, point a, point b, point c, point d, point &r) { vector pq = q - p; vector pa = - p; vector pb = b - p; vector pc = c - p; // determine triangle test against testing against diagonal first vector m = cross(pc, pq); float v = dot(pa, m); // scalartriple(pq, pa, pc); if (v >= 0.0f) { // test intersection against triangle abc float u = -dot(pb, m); // scalartriple(pq, pc, pb); if (u < 0.0f) return 0; float w = scalartriple(pq, pb, pa); ....
for the page linked, seems talk triangle in 3d space:
because triangle can oriented in way in 3d-space, ...
hence vectors talk 3d vectors, , text , code makes perfect sense. note 2d vectors makes sense, if consider cross product 3d vector pointing out of screen. , mention on page too:
if take cross product of [b-a] , [p-a], you'll vector pointing out of screen.
their code correct too, both 2d , 3d cases:
function sameside(p1,p2, a,b) cp1 = crossproduct(b-a, p1-a) cp2 = crossproduct(b-a, p2-a) if dotproduct(cp1, cp2) >= 0 return true else return false
for 2d, both cp1
, cp2
vectors pointing out of screen, , (3d) dot product need check; checking product of corresponding z components same. if 3d, correct. (though write return dotproduct(cp1, cp2) >= 0
.)
for int intersectlinequad()
, can guess situation same: quad
, whatever is, 3d object, vector
, point
in code. however, if add more details function supposed do, help.
in fact, obvious problem stated in 2d can extended 3d, , approach valid in 3d valid 2d case too, need imagine third axis pointing out of screen. think valid (though confusing) technique describe 2d problem in 3d terms. might doing work, because values 0 in such approach, in turn (almost) same code work in general 3d case too.
Comments
Post a Comment