Home > Software design >  Checking if vector passes through vertices c
Checking if vector passes through vertices c

Time:06-20

How can I check if a vector that starts at one point passes through another one point? It is on two-dimensional coordinates. Mainly uses c , but other languages are possible.

float2 startToTarget = target - start;
    if ((startToTarget.x) * vec.y - (startToTarget.y) * vec.x >= -floatingPoint && (startToTarget.x) * vec.y - (startToTarget.y) * vec.x <= floatingPoint)
        if ((startToTarget.x) * vec.x   (startToTarget.y) * vec.y >= -floatingPoint && (startToTarget.x) * vec.x   (startToTarget.y) * vec.y <= floatingPoint) intersecting = true;

CodePudding user response:

Calculate cross product of vector d (direction) and AB vector. If result is zero, then these vectors are collinear, so B point lies on the line, defined by point A and direction vector d.

To check direction, evaluate also dot product, it's sign is positive, when direction d coincides with direction AB

abx = B.x - A.x;
aby = B.y - A.y;

    //cross product           //dot product
if (abs(abx*dy - aby*dx) < 1.0e-10 and abx*dx   aby*dy>0) 
   {B lies on the ray A d}
  •  Tags:  
  • c
  • Related