Differences between revisions 1 and 2
Revision 1 as of 2003-10-02 03:35:23
Size: 3128
Editor: dsl254-010-130
Comment: Original version.
Revision 2 as of 2003-10-02 03:36:42
Size: 3098
Editor: dsl254-010-130
Comment:
Deletions are marked like this. Additions are marked like this.
Line 17: Line 17:
"""
Public domain. Do whatever you like with this.
"""
# Code is Public Domain.
Line 84: Line 82:

Points & Rectangles

A pair of classes to provide points and rectangles.

Surprisingly, I haven't been able to find a single Python module providing such primitive support.

WxPython supports wxPoint and wxRect, but it lacks many basic functions (such as, say, adding two points together to produce a third point..!)

This code is lacking a zillion essential features (such as "Length"). I only put in the ones I needed immediately. Please add, refactor, optimize, rename stuff to be more standard, etc., as you see fit..!

If there's an actual, accessible, easy-to-include Python module, not tied to a graphics library, that does this stuff already, please write about it here! No sense in reinventing the wheel. I've looked, but haven't found one. Hence this.

   1 # Code is Public Domain.
   2 def normalize( x1,y1,x2,y2 ):
   3     return (min(x1,x2),min(y1,y2),max(x1,x2),max(y1,y2))
   4 
   5 class Point:
   6     def __init__( self, x,y ):
   7         self.x = x
   8         self.y = y
   9     def __add__( self, other ):
  10         return Point( self.x+other.x, self.y+other.y )
  11     def __sub__( self, other ):
  12         return Point( self.x-other.x, self.y-other.y )
  13     def __mul__( self, scalar ):
  14         return Point( self.x*scalar, self.y*scalar )
  15     def __div__( self, scalar ):
  16         return Point( self.x/scalar, self.y/scalar )
  17     def __str__( self ):
  18         return "(%s,%s)" % (self.x,self.y)
  19     def XY( self ):
  20         return self.x,self.y
  21     def Clone( self ):
  22         return Point( self.x, self.y )
  23     def Integerize( self ):
  24         self.x = int( self.x )
  25         self.y = int( self.y )
  26     def Floatize( self ):
  27         self.x = float( self.x )
  28         self.y = float( self.y )
  29 
  30 class Rect:
  31     def __init__( self, pt1,pt2 ):
  32         self.Set( pt1,pt2 )
  33     def Contains( self, pt ):
  34         x,y = pt.XY()
  35         if (x >= self.left and
  36             x <= self.right and
  37             y >= self.top and
  38             y <= self.bottom ):
  39             return 1
  40         return 0
  41     def Set( self, pt1, pt2 ):
  42         l,t,r,b = normalize( pt1.x, pt1.y, pt2.x, pt2.y )
  43         self.left   = l
  44         self.top    = t
  45         self.right  = r
  46         self.bottom = b
  47     def Overlaps( self, other ):
  48         return (self.right>other.left) and (self.top < other.bottom) and (self.left<other.right) and (self.bottom>other.top)
  49     def GetTL( self ):
  50         return Point( self.left, self.top )
  51     def GetBR( self ):
  52         return Point( self.right, self.bottom )
  53     def ExpandedBy( self, n ):
  54         p1 = Point( self.left-1, self.top+1 )
  55         p2 = Point( self.right+1, self.bottom+1 )
  56         return Rect( p1, p2 )
  57     def TransformedByFunction( self, foo ):
  58         p1 = Point( self.left,  self.top )
  59         p2 = Point( self.right, self.bottom )
  60         p1 = foo( p1 )
  61         p2 = foo( p2 )
  62         return Rect p1, p2 )
  63     def __str__( self ):
  64         return "<Rect (%s,%s)-(%s,%s)>" % (self.left,self.top,
  65                                            self.right,self.bottom)

PointsAndRectangles (last edited 2008-11-15 13:59:40 by localhost)

Unable to edit the page? See the FrontPage for instructions.