Differences between revisions 2 and 3
Revision 2 as of 2003-10-02 03:36:42
Size: 3098
Editor: dsl254-010-130
Comment:
Revision 3 as of 2003-10-09 18:58:19
Size: 2986
Editor: 208
Comment: Regularize according to http://www.python.org/doc/essays/styleguide.html
Deletions are marked like this. Additions are marked like this.
Line 18: Line 18:
def normalize( x1,y1,x2,y2 ):
    return (min(x1,x2),min(y1,y2),max(x1,x2),max(y1,y2))
def normalize(x1, y1, x2, y2):
    return min(x1,x2), min(y1,y2), max(x1,x2), max(y1,y2)
Line 22: Line 22:
    def __init__( self, x,y ):     def __init__(self, x, y):
Line 25: Line 25:
    def __add__( self, other ):
        return Point( self.x+other.x, self.y+other.y )
    def __sub__( self, other ):
        return Point( self.x-other.x, self.y-other.y )
    def __add__(self, other):
        return Point(self.x+other.x, self.y+other.y)
    def __sub__(self, other):
        return Point(self.x-other.x, self.y-other.y)
Line 30: Line 30:
        return Point( self.x*scalar, self.y*scalar )
    def __div__( self, scalar ):
        return Point( self.x/scalar, self.y/scalar )
    def __str__( self
):
        return "(%s,%s)" % (self.x,self.y)
    def XY( self ):
        return Point(self.x*scalar, self.y*scalar)
    def __div__(self, scalar):
        return Point(self.x/scalar, self.y/scalar)
    def __str__(self
):
        return "(%s, %s)" % (self.x, self.y)
    def __repr__(self):
        return "%s(%r, %r)" % (self.__class__.__name__, self.x,
self.y)
    def XY(self):
Line 37: Line 39:
    def Clone( self ):
        return Point( self.x, self.y )
    def Integerize( self ):
        self.x = int( self.x )
        self.y = int( self.y
)
    def Floatize( self ):
        self.x = float( self.x )
        self.y = float( self.y )
    def Clone(self):
        return Point(self.x, self.y)
    def Integerize(self):
        self.x = int(self.x)
        self.y = int(self.y
)
    def Floatize(self):
        self.x = float(self.x)
        self.y = float(self.y)
Line 47: Line 49:
    def __init__( self, pt1,pt2 ):
        self.Set( pt1,pt2 )
    def Contains( self, pt ):
    def __init__(self, pt1, pt2):
        self.Set(pt1, pt2)
    def Contains(self, pt):
Line 51: Line 53:
        if (x >= self.left and
           
x <= self.right and
            y >=
self.top and
           
y <= self.bottom ):
            return 1
        return 0
        return self.left <= x <= self.right and self.top <= y <= self.bottom
Line 58: Line 55:
        l,t,r,b = normalize( pt1.x, pt1.y, pt2.x, pt2.y )
        self.left = l
        self.top = t
        self.right = r
        self.bottom = b
    def Overlaps( self, other ):
        return (self.right>other.left) and (self.top < other.bottom) and (self.left<other.right) and (self.bottom>other.top)
    def GetTL( self ):
        return Point( self.left, self.top )
    def GetBR( self ):
        return Point( self.right, self.bottom )
    def ExpandedBy( self, n ):
        p1 = Point( self.left-1, self.top+1 )
        p2 = Point( self.right+1, self.bottom+1 )
        return Rect( p1, p2 )
    def TransformedByFunction( self, foo ):
        p1 = Point( self.left, self.top )
        p2 = Point( self.right, self.bottom )
        p1 = foo( p1 )
        p2 = foo( p2 )
        return Rect p1, p2 )
        extrema = normalize(pt1.x, pt1.y, pt2.x, pt2.y)
        self.left, self.top, self.right, self.bottom = extrema
    def Overlaps(self, other):
        return (self.right > other.left and self.left < other.right
                and self.top < other.bottom and self.bottom > other.top)
    def GetTL(self):
        return Point(self.left, self.top)
    def GetBR(self):
        return Point(self.right, self.bottom)
    def ExpandedBy(self, n):
        p1 = Point(self.left-n, self.top+n)
        p2 = Point(self.right+n, self.bottom+n)
        return Rect(p1, p2)
    def TransformedByFunction(self, foo):
        p1 = Point(self.left, self.top)
        p2 = Point(self.right, self.bottom)
        return Rect(foo(p1), foo(p2))

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 __repr__(self):
  20         return "%s(%r, %r)" % (self.__class__.__name__, self.x, self.y)
  21     def XY(self):
  22         return self.x,self.y
  23     def Clone(self):
  24         return Point(self.x, self.y)
  25     def Integerize(self):
  26         self.x = int(self.x)
  27         self.y = int(self.y)
  28     def Floatize(self):
  29         self.x = float(self.x)
  30         self.y = float(self.y)
  31 
  32 class Rect:
  33     def __init__(self, pt1, pt2):
  34         self.Set(pt1, pt2)
  35     def Contains(self, pt):
  36         x,y = pt.XY()
  37         return self.left <= x <= self.right and self.top <= y <= self.bottom
  38     def Set( self, pt1, pt2 ):
  39         extrema = normalize(pt1.x, pt1.y, pt2.x, pt2.y)
  40         self.left, self.top, self.right, self.bottom = extrema
  41     def Overlaps(self, other):
  42         return (self.right > other.left and self.left < other.right
  43                 and self.top < other.bottom and self.bottom > other.top)
  44     def GetTL(self):
  45         return Point(self.left, self.top)
  46     def GetBR(self):
  47         return Point(self.right, self.bottom)
  48     def ExpandedBy(self, n):
  49         p1 = Point(self.left-n, self.top+n)
  50         p2 = Point(self.right+n, self.bottom+n)
  51         return Rect(p1, p2)
  52     def TransformedByFunction(self, foo):
  53         p1 = Point(self.left,  self.top)
  54         p2 = Point(self.right, self.bottom)
  55         return Rect(foo(p1), foo(p2))
  56     def __str__( self ):
  57         return "<Rect (%s,%s)-(%s,%s)>" % (self.left,self.top,
  58                                            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.