Search code examples
mathvector

How to check if a vector is within two vectors


Here is a image:

enter image description here

I have two vectors : os, oe

the range between them is always from os (start) to oe (end).

So in this image the range between is a angle of 270°.

Then I have two vector to check: oa, ob

As you can see the vector oa should be within the range formed by osoe and the vector ob should be outside.

I am wondering if there is a way to do the check using only vector math (such as cross product dot product).

I tried to use cross product with clockwise/counter clockwise check but it seems like when the angle in between is larger then 180°, things get complex.

Any advice will be appreciated, thanks :)


Solution

  • I denote vector to point p as op.

    Calculate cross product

     c_se = cross(os, oe)
    

    If c_se>=0 (angle in 0..180 range), then you have to check whether

    cross(os, op) >= 0 AND cross(op, oe) >= 0
    

    If c_se < 0 (angle in 180..360 range), then you have to check whether (OR instead of AND, as Matt noticed in comments)

    cross(os, op) >= 0 OR cross(op, oe) >= 0
    

    Old version - a bit more complex:

    NOT (cross(oe, op) >= 0 AND cross(op, os) >= 0)