Some remarks on wx.Rect class
Where is additional constructor-like global functions which can be used for creating wx.Rect: wx.RectPS - create rectangle from (point,size)pair. wx.RectPP - create rectangle from (point,point) pair.
Actually wx.RectPP accepts points in _any_ order, not only (left_top,right-down) as you can imagine from it's documentation. wxRectPP returns rectangle which lays _around_ both points, say:
- wx.RectPP( (0,0),(1,1) ) == wx.Rect( 0,0,2,2 )
Where is also a function wx.IntersectRect( r1,r2 ) which returns intersection of two rectangles or None if they don't intersects.
Here is small unittest example:
Toggle line numbers
1 import unittest
2 import wx
3
4 class TestRectPP( unittest.TestCase ):
5 def test_rect_surrounds_both_points( self ):
6 self.assertEqual( wx.RectPP((0,0),(7,5)), wx.Rect(0,0,8,6) )
7
8 def test_points_order_is_irrelevant( self ):
9 p1 = wx.Point( 1,2 )
10 p2 = wx.Point( 5,6 )
11 r1 = wx.RectPP( p1, p2 )
12 r2 = wx.RectPP( p2, p1 )
13 self.assertEqual( r1, r2 )
14
15 class TestIntersection( unittest.TestCase ):
16 def test_equals( self ):
17 r1 = wx.RectPS( (0,0),(4,4) )
18 r2 = wx.RectPS( (0,0),(4,4) )
19 r3 = wx.IntersectRect( r1,r2 )
20 self.assertEqual( r3, wx.RectPS((0,0),(4,4)) )
21
22 def test_some_intersection( self ):
23 r1 = wx.RectPS( (0,0),(4,4) )
24 r2 = wx.RectPS( (1,2),(4,4) )
25 r3 = wx.IntersectRect( r1,r2 )
26 self.assertEqual( r3, wx.Rect(1,2,3,2) )
27
28 def test_no_intersection_returns_None( self ):
29 r1 = wx.RectPP( (0,0),(10,10) )
30 r2 = wx.RectPP( (12,12),(20,20) )
31 r3 = wx.IntersectRect( r1,r2 )
32 self.assertEqual( r3, None )
33
34 def suite():
35 suite = unittest.TestSuite()
36 suite.addTest( unittest.makeSuite( TestRectPP ) )
37 suite.addTest( unittest.makeSuite( TestIntersection ) )
38 return suite
39
40 if __name__ == '__main__':
41 runner = unittest.TextTestRunner()
42 runner.run( suite() )
(feel free to edit my "english") Vladimir Ignatov
See also: PythonInfo:PointsAndRectangles