Revision 7 as of 2004-11-17 06:09:21

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     return min(x1,x2), min(y1,y2), max(x1,x2), max(y1,y2)
   7 
   8 class Point:
   9     def __init__(self, x, y):
  10         self.x = x
  11         self.y = y
  12     def __add__(self, other):
  13         return Point(self.x+other.x, self.y+other.y)
  14     def __sub__(self, other):
  15         return Point(self.x-other.x, self.y-other.y)
  16     def __mul__( self, scalar ):
  17         return Point(self.x*scalar, self.y*scalar)
  18     def __div__(self, scalar):
  19         return Point(self.x/scalar, self.y/scalar)
  20     def __str__(self):
  21         return "(%s, %s)" % (self.x, self.y)
  22     def __repr__(self):
  23         return "%s(%r, %r)" % (self.__class__.__name__, self.x, self.y)
  24     def XY(self):
  25         return self.x,self.y
  26     def Clone(self):
  27         return Point(self.x, self.y)
  28     def Integerize(self):
  29         self.x = int(self.x)
  30         self.y = int(self.y)
  31     def Floatize(self):
  32         self.x = float(self.x)
  33         self.y = float(self.y)
  34     def distanceTo(self,pt):
  35         '''Distance between 2 points.'''
  36         dy = self.y - pt.y
  37         dx = self.x - pt.x
  38         return sqrt(dy*dy + dx*dx)
  39 
  40 class Rect:
  41     def __init__(self, pt1, pt2):
  42         self.Set(pt1, pt2)
  43     def Contains(self, pt):
  44         x,y = pt.XY()
  45         return self.left <= x <= self.right and self.top <= y <= self.bottom
  46     def Set( self, pt1, pt2 ):
  47         extrema = normalize(pt1.x, pt1.y, pt2.x, pt2.y)
  48         self.left, self.top, self.right, self.bottom = extrema
  49     def Overlaps(self, other):
  50         return (self.right > other.left and self.left < other.right
  51                 and self.top < other.bottom and self.bottom > other.top)
  52     def GetTL(self):
  53         return Point(self.left, self.top)
  54     def GetBR(self):
  55         return Point(self.right, self.bottom)
  56     def ExpandedBy(self, n):
  57         p1 = Point(self.left-n, self.top+n)
  58         p2 = Point(self.right+n, self.bottom+n)
  59         return Rect(p1, p2)
  60     def TransformedByFunction(self, foo):
  61         p1 = Point(self.left,  self.top)
  62         p2 = Point(self.right, self.bottom)
  63         return Rect(foo(p1), foo(p2))
  64     def __str__( self ):
  65         return "<Rect (%s,%s)-(%s,%s)>" % (self.left,self.top,
  66                                            self.right,self.bottom)
  67     def __repr__(self):
  68         return "%s(%r, %r)" % (self.__class__.__name__,
  69                                Point(self.left, self.top),
  70                                Point(self.right, self.bottom))

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