#------------------------------------------------------------------------------ # Point6.py #------------------------------------------------------------------------------ from math import sqrt class Point: """ Point class encapsulates two numbers (xcoord, ycoord). """ # __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 '('+str(self.xcoord)+', '+str(self.ycoord)+')' # end __str__() # norm() def norm(self): """ Return the distance from self to origin. """ return sqrt(self.xcoord**2 + self.ycoord**2) # 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() # end class Point #-- main (test Point class) --------------------------------------------------- p = Point(1.5, -3.5) q = Point(3.7, 8.2) print(p.xcoord, p.ycoord) print(p.norm()) print(q.xcoord, q.ycoord) print(q.norm()) print(q.dist(p)) print(p.dist(q)) m = p.midpoint(q) print(m.xcoord, m.ycoord) print(p) print(q) print(m)