Revision 8 as of 2004-11-24 20:25:57

Clear message

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 (but interpoint distance can now be calculated). 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 
   3 import math # required because of 'sqrt' in 'distanceTo' function.
   4 
   5 def normalize(x1, y1, x2, y2):
   6     '''I am not sure what this is for, care to comment?'''
   7     return min(x1,x2), min(y1,y2), max(x1,x2), max(y1,y2)
   8 
   9 class Point:
  10     def __init__(self, x, y):
  11         self.x = x
  12         self.y = y
  13     def __add__(self, other):
  14         return Point(self.x+other.x, self.y+other.y)
  15     def __sub__(self, other):
  16         return Point(self.x-other.x, self.y-other.y)
  17     def __mul__( self, scalar ):
  18         return Point(self.x*scalar, self.y*scalar)
  19     def __div__(self, scalar):
  20         return Point(self.x/scalar, self.y/scalar)
  21     def __str__(self):
  22         return "(%s, %s)" % (self.x, self.y)
  23     def __repr__(self):
  24         return "%s(%r, %r)" % (self.__class__.__name__, self.x, self.y)
  25     def XY(self):
  26         '''Returns a tuple (x,y).'''
  27         return self.x,self.y
  28     def Clone(self):
  29         '''Returns a full copy of this point.''' 
  30         return Point(self.x, self.y)
  31     def Integerize(self):
  32         '''Rounds co-ordinate values to integers.'''
  33         self.x = int(self.x)
  34         self.y = int(self.y)
  35     def Floatize(self):
  36         '''Converts co-ordinate values to floating point (is this necessary?)'''
  37         self.x = float(self.x)
  38         self.y = float(self.y)
  39     def distanceTo(self,pt):
  40         '''Distance between 2 points.'''
  41         dy = self.y - pt.y
  42         dx = self.x - pt.x
  43         return sqrt(dy*dy + dx*dx)
  44 
  45 class Rect:
  46     def __init__(self, pt1, pt2):
  47         self.Set(pt1, pt2)
  48     def Contains(self, pt):
  49         x,y = pt.XY()
  50         return self.left <= x <= self.right and self.top <= y <= self.bottom
  51     def Set( self, pt1, pt2 ):
  52         extrema = normalize(pt1.x, pt1.y, pt2.x, pt2.y)
  53         self.left, self.top, self.right, self.bottom = extrema
  54     def Overlaps(self, other):
  55         return (self.right > other.left and self.left < other.right
  56                 and self.top < other.bottom and self.bottom > other.top)
  57     def GetTL(self):
  58         return Point(self.left, self.top)
  59     def GetBR(self):
  60         return Point(self.right, self.bottom)
  61     def ExpandedBy(self, n):
  62         p1 = Point(self.left-n, self.top+n)
  63         p2 = Point(self.right+n, self.bottom+n)
  64         return Rect(p1, p2)
  65     def TransformedByFunction(self, foo):
  66         p1 = Point(self.left,  self.top)
  67         p2 = Point(self.right, self.bottom)
  68         return Rect(foo(p1), foo(p2))
  69     def __str__( self ):
  70         return "<Rect (%s,%s)-(%s,%s)>" % (self.left,self.top,
  71                                            self.right,self.bottom)
  72     def __repr__(self):
  73         return "%s(%r, %r)" % (self.__class__.__name__,
  74                                Point(self.left, self.top),
  75                                Point(self.right, self.bottom))

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