Lecture Notes on 11 August 2014 import math class Point (object): # constructor def __init__ (self, x = 0, y = 0): self.x = x self.y = y # get distance to another Point def dist (self, other): return math.hypot (self.x - other.x, self.y - other.y) # string representation of a Point def __str__ (self): return '(' + str(self.x) + ', ' + str(self.y) + ')' # test if two Points are the same def __eq__ (self, other): tol = 1.0e-16 return (abs(self.x - other.x) < tol) and (abs(self.y - other.y) < tol) class Circle (object): # constructor def __init__ (self, radius = 0, x = 0, y = 0): self.radius = radius self.center = Point (x, y) # compute circumference def circumference (self): return 2.0 * math.pi * self.radius # compute area def area (self): return math.pi * self.radius * self.radius # determine if a Point is inside a circle def point_within (self, p): distance = self.center.dist (p) return (distance < self.radius) # determine if a circle other is completely within def circle_within (self, other): distance = self.center.dist (other.center) return (distance + other.radius) < self.radius # string representation of a circle def __str__ (self): return 'Radius: ' + str(self.radius) + ' Center: ' + str (self.center) # test for equality def __eq__ (self, other): tol = 1.0e-16 return (abs(self.radius - other.radius) < tol) def main(): # create Point objects p = Point() q = Point (3, 5) # print the Point objects print (p) print (q) # change the coordinates of a Point object p.x += 1 p.y += 2 print (p) # get distance between two Point objects print (p.dist(q)) print (q.dist(p)) # test for equality of two Point objects if (p == q): print (str(p) + ' and ' + str(q) + ' are equal') else: print (str(p) + ' and ' + str(q) + ' are not equal') # create Circle objects circleA = Circle (3.0, 0, 5) print (circleA) circleB = Circle (1618, 8, 6) print (circleB.area()) if (circleB.circle_within(circleA)): print (str(circleA) + " is within " + str(circleB)) else: print (str(circleA) + " is not within " + str(circleB)) main()