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
Post a Comment