# point.py
# Walker M. White (wmw2)
# October 7, 2012
"""Module with a simple Point class.
This module has a simpler version of the Point class than
what we saw in previous labs. It shows off the minimum that
we need to get started with a class."""
import math
class Point(object):
"""Instance is a point in 3D space"""
x = 0.0 # x coordinate; must be a float
y = 0.0 # y coordinate; must be a float
z = 0.0 # z coordinate; must be a float
def __init__(self,x=0.0,y=0.0,z=0.0):
"""Constructor: makes a new Point
Precondition: x, y, and z are floats"""
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 distanceTo(self,other):
"""Returns: Distance from self to other
Precondition: other is a Point"""
assert type(other) == Point, `other`+' is not a Point'
dx = (self.x-other.x)*(self.x-other.x)
dy = (self.y-other.y)*(self.y-other.y)
dz = (self.z-other.z)*(self.z-other.z)
return math.sqrt(dx+dy+dz)