0

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 :)

supersuraccoon
  • 1,621
  • 4
  • 20
  • 36
  • This isn't a stackoverflow question as it is completely unrelated to programming. It should be on [This math site](http://math.stackexchange.com/) . Although it may not be received well there because duplicates. But might as well try – Matthew Ciaramitaro Apr 13 '17 at 03:47
  • By the way the cross product of two vectors on the two dimensional plane xy is never on the two dimensional plane xy. it's normal (perpendicular) to the plane, so you can't figure it out using cross product. You should just find the angle between the oa and both os and oe. that will tell you if its between them or outside of their range – Matthew Ciaramitaro Apr 13 '17 at 03:51
  • I'm voting to close this question as off-topic because it is not about computer programming. – Rory Daulton Apr 13 '17 at 09:09

1 Answers1

5

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)
MBo
  • 77,366
  • 5
  • 53
  • 86
  • In the second case, if `c_se < 0`, then shouldn't the condition to check be `cross(oe, op) >= 0 OR cross(op, os) >= 0`? – Matt Jun 12 '23 at 20:47
  • @Matt Your expression seems wrong. It denotes: p lies in 180-degrees sector CCW from e, or s lies in 180-degrees sector CCW from p - these conditions don't guarantee that p is in needed region. But we can revert comparisons - in this case expression becomes equivalent to mine: `cross(oe, op) <= 0 OR cross(op, os) <= 0` – MBo Jun 13 '23 at 03:27
  • 1
    Oops, you're right - my apologies for the confusion. What I meant to say was `cross(os, op) >= 0 OR cross(op, oe) >= 0` which is equivalent. In other words, the only difference between the first and second branch is that `AND` is replaced with `OR`. – Matt Jun 13 '23 at 12:06
  • @Matt Seems you are right, simpler expression without negative logic is better. – MBo Jun 13 '23 at 12:24