#------------------------------------------------------------------------------ # Point7.py #------------------------------------------------------------------------------ from math import sqrt from random import uniform class Point: """ Point class repesents a point (xcoord, ycoord) in the Euclidean plane. """ # __init__() # initialization function for Point class def __init__(self, x=0, y=0): """ Create a new point at (x, y) with x=0 and y=0 as default values. """ self.xcoord = x self.ycoord = y # end __init__() # __str__() # string representation of a Point object def __str__(self): """ Return a string representation of self. """ return 'Point({0:.3f}, {1:.3f})'.format(self.xcoord,self.ycoord) # end __str__() # __eq__() # compare two Points for equality def __eq__(self, other): """ Return True if self == other, false otherwise. """ if type(self) is type(other): return self.__dict__ == other.__dict__ else: return False # end __eq__() # getCoord()() def getCoord(self): """ Return xcoord and ycoord as a tuple. """ return (self.xcoord, self.ycoord) # end getCoord() # sqNorm() def sqNorm(self): """ Return the square of the distance from the origin to (xcoord, ycoord) """ return self.xcoord**2 + self.ycoord**2 # end sqNorm() # norm() def norm(self): """ Return the distance from self to origin. """ return sqrt(self.sqNorm()) # end norm() # dist() def dist(self, other): """ Return the distance from self to other. """ return sqrt( (self.xcoord-other.xcoord)**2 + (self.ycoord-other.ycoord)**2 ) # end dist() # midpoint() def midpoint(self, other): """ Return a new Point object that is halfway between self and other. """ x = (self.xcoord+other.xcoord)/2 y = (self.ycoord+other.ycoord)/2 return Point(x, y) # end midpoint() # randPoint() def randPoint(a, b, c, d): """ Return a new random Point satisfying a<=xcoord