# point.py # Walker M. White (wmw2) # October 7, 2012 """Module with a simple Point3 class. This module has a simpler version of the Point3 class than what we saw in previous classes. It shows off the minimum that we need to get started with a class.""" import math class Point3(object): """Instance is a point in 3D space Instance variables: x: x-coordinate [float] y: y-coordinate [float] z: z-coordinate [float] """ def __init__(self,x=0.0,y=0.0,z=0.0): """Initializer: makes a new Point3 Parameter x: The x-coordinate (optional) Precondition: x is a float Parameter y: The y-coordinate (optional) Precondition: y is a float Parameter z: The z-coordinate (optional) Precondition: z is a float""" self.x = x # x is parameter, self.x is field self.y = y # y is parameter, self.y is field self.z = z # z is parameter, self.z is field def __str__(self): """Returns: this Point as a string '(x, y, z)'""" return '('+str(self.x)+', '+str(self.y)+', '+str(self.z)+')' def __repr__(self): """Returns: unambiguous representation of Point""" return str(self.__class__)+str(self) def __eq__(self,other): """Returns: True if other == self; False otherwise Parameter other: The object to compare Precondition: None (other can be anything)""" return type(other) == Point3 and self.x == other.x and self.y == other.y and self.z == other.z def distanceTo(self, other): """Returns: Distance from self to other Parameter other: The point to reach Precondition: other is a Point3""" assert type(other) == Point3, `other`+' is not a Point' dx = (x-other.x)*(x-other.x) dy = (y-other.y)*(y-other.y) dz = (z-other.z)*(z-other.z) return math.sqrt(dx+dy+dz)