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

人盡茶涼 提交于 2020-01-02 05:01:32

问题


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?


回答1:


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.




回答2:


A one-dimensional cross product is merely x*y for x and y both real numbers. So I guess you could call this a one-dimensional cross product, but that's just a fancy name for a multiplication.

It's a cute trick, but I don't think it has any special consequence mathematically. Cross-products are all about vectors, not line segments.



来源:https://stackoverflow.com/questions/1558901/one-dimensional-line-segments-ranges-intersection-test-solution-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!