math - Intersecting rectangles with Python -
given 2 rectangles r1 , r2 try test if 2 intersect. why don't following 2 functions produce same output?
function 1:
def separate_helper(r1, r2): r1_left, r1_top, r1_right, r1_bottom = r1 r2_left, r2_top, r2_right, r2_bottom = r2 if r1_right < r2_left: separate = true elif r1_left > r2_right: separate = true elif r1_top > r2_bottom: separate = true elif r1_bottom < r2_top: separate = true elif contains(r1, r2): separate = false else: separate = false return separate
function 2:
def separate_helper2(r1, r2): r1_left, r1_top, r1_right, r1_bottom = r1 r2_left, r2_top, r2_right, r2_bottom = r2 separate = r1_right < r2_left or \ r1_left > r2_right or \ r1_top > r2_bottom or \ r1_bottom < r2_top or \ not contains(r1, r2) return separate
function check if rectangle 1 contains rectangle 2:
def contains(r1, r2): r1_left, r1_top, r1_right, r1_bottom = r1 r2_left, r2_top, r2_right, r2_bottom = r2 return r1_right >= r2_right , r1_left <= r2_left , r1_top <= r2_top , r1_bottom >= r2_bottom
here's test case fails:
assert separate_helper([29, 35, 53, 90], [23, 47, 90, 86]) == separate_helper2([29, 35, 53, 90], [23, 47, 90, 86])
it fails when rectangle 1 contains rectangle 2, can't wrap head around why.
edit:
i'm using quickcheck python , nose test function. here's test code i'm using:
from qc import forall, lists, integers intersect import separate_helper, separate_helper2 @forall(tries=100, r1=lists(items=integers(), size=(4, 4)), r2=lists(items=integers(), size=(4, 4))) def test_separate(r1, r2): assert separate_helper(r1, r2) == separate_helper2(r1, r2)
look @ first version:
elif contains(r1, r2): separate = false else: separate = false
assuming through of proper-intersection cases, return false
whether r1
contains r2
or not.
but in second version:
... or \ not contains(r1, r2)
this return false
r1
not contain r2
, true
otherwise.
so, they're doing different things in precisely case "when rectangle 1 contains rectangle 2".
as side question: why should r1
containing r2
return different result r2
containing r1
?
Comments
Post a Comment