3128
Comment: Original version.
|
3739
|
Deletions are marked like this. | Additions are marked like this. |
Line 11: | Line 11: |
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..! | 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..! |
Line 17: | Line 17: |
""" Public domain. Do whatever you like with this. """ def normalize( x1,y1,x2,y2 ): return (min(x1,x2),min(y1,y2),max(x1,x2),max(y1,y2)) |
# Code is Public Domain. import math # required because of 'sqrt' in 'distanceTo' function. def normalize(x1, y1, x2, y2): '''I am not sure what this is for, care to comment?''' return min(x1,x2), min(y1,y2), max(x1,x2), max(y1,y2) |
Line 24: | Line 26: |
def __init__( self, x,y ): | def __init__(self, x, y): |
Line 27: | Line 29: |
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 32: | Line 34: |
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): '''Returns a tuple (x,y).''' |
Line 39: | Line 44: |
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): '''Returns a full copy of this point.''' return Point(self.x, self.y) def Integerize(self): '''Rounds co-ordinate values to integers.''' self.x = int(self.x) self.y = int(self.y) def Floatize(self): '''Converts co-ordinate values to floating point (is this necessary?)''' self.x = float(self.x) self.y = float(self.y) def distanceTo(self,pt): '''Distance between 2 points.''' dy = self.y - pt.y dx = self.x - pt.x return sqrt(dy*dy + dx*dx) |
Line 49: | Line 62: |
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 53: | Line 66: |
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 60: | Line 68: |
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)) |
Line 84: | Line 88: |
def __repr__(self): return "%s(%r, %r)" % (self.__class__.__name__, Point(self.left, self.top), Point(self.right, self.bottom)) |
|
Line 85: | Line 94: |
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))