math - Intersection of two points in Ruby -


i want find intersection of 2 points in ruby. type of checks should there function works cases.

pseudocode code is

intersection(range1, range2)

  • notcommonr1 = part of range1 not common

  • common = common part between both ranges

  • notcommonr2 = part of range2 not common

for example

intersection([0, 3], [2, 4]) == { :range1 => [[0, 2]], :both => [2, 3], :range2 => [[3, 4]] } 

this straightforward; there aren't special checks make here; special case if there no common part between ranges.

def intersection(a, b)   # sort a1 < a2, b1 < b2, a1 < b1   a, b = [a.sort, b.sort].sort   a1, a2 =   b1, b2 = b    if a2 > b2     {range1: [[a1, b1], [b2, a2]], both: [[b1, b2]], range2: []}   elsif a2 >= b1     {range1: [[a1, b1]], both: [[b1, a2]], range2: [[a2, b2]]}   else     {range1: [[a1, a2]], both: [], range2: [[b1, b2]]}   end end 

depending on how use both, nil value may not ideal; use whatever indicates no common range.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -