Skip to content

Commit 7b621e4

Browse files
author
Jan Bölsche
committed
moved rect tools to rect.py, added tests for rect tools
1 parent 77c3963 commit 7b621e4

File tree

2 files changed

+72
-28
lines changed

2 files changed

+72
-28
lines changed

rect.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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()

wrappers.py

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import string
44
import pyglet
55
from pyglet.gl import *
6+
from rect import *
67

78
class Visible(object):
89
instanceCount = 0
@@ -129,34 +130,6 @@ def draw(self):
129130
self.label.draw()
130131
glPopMatrix()
131132

132-
133-
def flatten_rect(r):
134-
((x,y),(w,h)) = r
135-
return (x,y,w,h)
136-
137-
def rect_overlaps(r1, r2):
138-
# check if r1 is completely outside of r2
139-
# andreturn the inverse.
140-
((x1,y1),(w1,h1)) = r1
141-
((x2,y2),(w2,h2)) = r2
142-
return not ((x1<x2 and x1+w1 <= x2 or x1>=x2+w2) or (y1<y2 and y1+h1 <= y2 or y1>=y2+h2))
143-
144-
def clip_rect(r1, r2):
145-
if rect_overlaps(r1, r2):
146-
((x1,y1),(w1,h1)) = r1
147-
((x2,y2),(w2,h2)) = r2
148-
149-
nx = max(x1, x2)
150-
ny = max(y1, y2)
151-
152-
w = min(x1 + w1, x2 + w2) - nx
153-
h = min(y1 + h1, y2 + h2) - ny
154-
155-
return ((nx, ny), (w, h))
156-
else:
157-
return ((0,0),(0,0))
158-
159-
160133
class ClippingContainer(Visible):
161134
instanceCount = 0
162135

0 commit comments

Comments
 (0)