Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One-Dimensional Line-Segments/Ranges Intersection Test: Solution Name?

Tags:

math

range

I've worked out a method to test if two one-dimensional line-segments/ranges.

So defining a range as:

[min, max]

Given two instances of range:

a = [min, max] 
b = [min, max]

I use the following to test if they intersect:

(a.max - b.min) * (b.max - a.min) >= 0.

I think this is a one-dimensional cross-product, so my question is:

Is this solution classified as a one-dimensional cross-product or something else?

like image 865
BefittingTheorem Avatar asked Oct 13 '09 08:10

BefittingTheorem


1 Answers

How about:

intersects = !((a.max < b.min) || (b.max < a.min))

That's faster (no multiply involved and a decent compiler will optimize the NOT away) and just as readable.

like image 96
Aaron Digulla Avatar answered Sep 23 '22 06:09

Aaron Digulla