|
| 1 | +def flatten_rect(r): |
| 2 | + ((x,y),(w,h)) = r |
| 3 | + return (x,y,w,h) |
| 4 | + |
| 5 | +def rect_overlaps(r1, r2): |
| 6 | + # check if r1 is completely outside of r2 |
| 7 | + # andreturn the inverse. |
| 8 | + ((x1,y1),(w1,h1)) = r1 |
| 9 | + ((x2,y2),(w2,h2)) = r2 |
| 10 | + return not ((x1<x2 and x1+w1 <= x2 or x1>=x2+w2) or (y1<y2 and y1+h1 <= y2 or y1>=y2+h2)) |
| 11 | + |
| 12 | +def clip_rect(r1, r2): |
| 13 | + if rect_overlaps(r1, r2): |
| 14 | + ((x1,y1),(w1,h1)) = r1 |
| 15 | + ((x2,y2),(w2,h2)) = r2 |
| 16 | + |
| 17 | + nx = max(x1, x2) |
| 18 | + ny = max(y1, y2) |
| 19 | + |
| 20 | + w = min(x1 + w1, x2 + w2) - nx |
| 21 | + h = min(y1 + h1, y2 + h2) - ny |
| 22 | + |
| 23 | + return ((nx, ny), (w, h)) |
| 24 | + else: |
| 25 | + return ((0,0),(0,0)) |
| 26 | + |
| 27 | + |
| 28 | +## |
| 29 | +# Regression Tests |
| 30 | +## |
| 31 | +import unittest |
| 32 | +from test import test_support |
| 33 | + |
| 34 | +class TestRectTools(unittest.TestCase): |
| 35 | + def test_flatten(self): |
| 36 | + assert flatten_rect(((1,2),(3,4))) == (1,2,3,4) |
| 37 | + |
| 38 | + def test_overlaps(self): |
| 39 | + assert rect_overlaps( ((1,1),(1,1)), ((1,1),(1,1)) ) == True |
| 40 | + assert rect_overlaps( ((1,1),(1,1)), ((2,1),(1,1)) ) == False |
| 41 | + assert rect_overlaps( ((1,1),(1,1)), ((1,2),(1,1)) ) == False |
| 42 | + assert rect_overlaps( ((1,1),(1,1)), ((0,1),(1,1)) ) == False |
| 43 | + assert rect_overlaps( ((1,1),(1,1)), ((1,0),(1,1)) ) == False |
| 44 | + assert rect_overlaps( ((1,1),(1,1)), ((0,1),(2,1)) ) == True |
| 45 | + assert rect_overlaps( ((1,1),(1,1)), ((1,0),(1,2)) ) == True |
| 46 | + assert rect_overlaps( ((1,1),(1,1)), ((0,0),(2,2)) ) == True |
| 47 | + assert rect_overlaps( ((10,10),(20,20)), ((5,5),(200,200)) ) == True |
| 48 | + assert rect_overlaps( ((5,5),(200,200)), ((10,10),(20,20)) ) == True |
| 49 | + assert rect_overlaps( ((0,0),(20,20)), ((5,5),(200,200)) ) == True |
| 50 | + assert rect_overlaps( ((5,5),(200,200)), ((0,0),(20,20)) ) == True |
| 51 | + assert rect_overlaps( ((0,0),(20,20)), ((25,5),(200,200)) ) == False |
| 52 | + assert rect_overlaps( ((0,0),(20,20)), ((25,25),(200,200)) ) == False |
| 53 | + assert rect_overlaps( ((25,5),(200,200)), ((0,0),(20,20)) ) == False |
| 54 | + assert rect_overlaps( ((25,25),(200,200)), ((0,0),(20,20)) ) == False |
| 55 | + |
| 56 | + def test_clip(self): |
| 57 | + assert clip_rect( ((25,5),(200,200)), ((0,0),(20,20)) ) == ((0,0),(0, 0)) |
| 58 | + assert clip_rect( ((25,5),(200,200)), ((0,0),(30,30)) ) == ((25,5),(5, 25)) |
| 59 | + assert clip_rect( ((1,1),(1,1)), ((1,1),(1,1)) ) == ((1,1),(1,1)) |
| 60 | + assert clip_rect( ((100,100),(10,10)), ((10,10),(200,200)) ) == ((100,100),(10,10)) |
| 61 | + assert clip_rect( ((0,0),(55,100)), ((45,0),(55,200)) ) == ((45,0),(10,100)) |
| 62 | + |
| 63 | + |
| 64 | +def test_main(): |
| 65 | + test_support.run_unittest( |
| 66 | + TestRectTools, |
| 67 | + #... list other tests ... |
| 68 | + ) |
| 69 | + |
| 70 | +if __name__ == "__main__": |
| 71 | + test_main() |
0 commit comments