This wiki is in the process of being archived due to lack of usage and the resources necessary to serve it — predominately to bots, crawlers, and LLM companies. Edits are discouraged.
Pages are preserved as they were at the time of archival. For current information, please visit python.org.
If a change to this archive is absolutely needed, requests can be made via the infrastructure@python.org mailing list.

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..!) (See: wxPyWiki:wx.Rect)

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 """Point and Rectangle classes.
   2 
   3 This code is in the public domain.
   4 
   5 Point  -- point with (x,y) coordinates
   6 Rect  -- two points, forming a rectangle
   7 """
   8 
   9 import math
  10 
  11 
  12 class Point:
  13     
  14     """A point identified by (x,y) coordinates.
  15     
  16     supports: +, -, *, /, str, repr
  17     
  18     length  -- calculate length of vector to point from origin
  19     distance_to  -- calculate distance between two points
  20     as_tuple  -- construct tuple (x,y)
  21     clone  -- construct a duplicate
  22     integerize  -- convert x & y to integers
  23     floatize  -- convert x & y to floats
  24     move_to  -- reset x & y
  25     slide  -- move (in place) +dx, +dy, as spec'd by point
  26     slide_xy  -- move (in place) +dx, +dy
  27     rotate  -- rotate around the origin
  28     rotate_about  -- rotate around another point
  29     """
  30     
  31     def __init__(self, x=0.0, y=0.0):
  32         self.x = x
  33         self.y = y
  34     
  35     def __add__(self, p):
  36         """Point(x1+x2, y1+y2)"""
  37         return Point(self.x+p.x, self.y+p.y)
  38     
  39     def __sub__(self, p):
  40         """Point(x1-x2, y1-y2)"""
  41         return Point(self.x-p.x, self.y-p.y)
  42     
  43     def __mul__( self, scalar ):
  44         """Point(x1*x2, y1*y2)"""
  45         return Point(self.x*scalar, self.y*scalar)
  46     
  47     def __div__(self, scalar):
  48         """Point(x1/x2, y1/y2)"""
  49         return Point(self.x/scalar, self.y/scalar)
  50     
  51     def __str__(self):
  52         return "(%s, %s)" % (self.x, self.y)
  53     
  54     def __repr__(self):
  55         return "%s(%r, %r)" % (self.__class__.__name__, self.x, self.y)
  56     
  57     def length(self):