Question
Any hints or comments are appreciated: This assignment is on object oriented programming. You will be developing several classes that are fundamental in Geometry -
Any hints or comments are appreciated:
This assignment is on object oriented programming. You will be developing several classes that are fundamental in Geometry - Point, Circle, and Rectangle. In main() you will test the various functions that you have written for the classes. Do not change the names of the methods, attributes, or arguments.
Here is the skeleton of the code that we started writing in class. Write the bodies of the functions that we did not complete. You can always add more functions.
import math class Point (object): # constructor # x and y are floats def __init__ (self, x = 0, y = 0): self.x = x self.y = y # get distance # other is a Point object def dist (self, other): return math.hypot (self.x - other.x, self.y - other.y) # get a string representation of a Point object # takes no arguments # returns a string def __str__ (self): return '(' + str(self.x) + ", " + str(self.y) + ")" # test for equality # other is a Point object # returns a Boolean 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 # x, y, and radius are floats def __init__ (self, radius = 1, x = 0, y = 0): self.radius = radius self.center = Point (x, y) # compute cirumference def circumference (self): return 2.0 * math.pi * self.radius # compute area def area (self): return math.pi * self.radius * self.radius # determine if point is strictly inside circle def point_inside (self, p): return (self.center.dist(p) < self.radius) # determine if a circle is strictly inside this circle def circle_inside (self, c): distance = self.center.dist (c.center) return (distance + c.radius) < self.radius # determine if a circle c overlaps this circle (non-zero area of overlap) # but neither is completely inside the other # the only argument c is a Circle object # returns a boolean def circle_overlap (self, c): # determine the smallest circle that circumscribes a rectangle # the circle goes through all the vertices of the rectangle # the only argument, r, is a rectangle object def circle_circumscribe (self, r): # string representation of a circle # takes no arguments and returns a string def __str__ (self): return "Radius: " + str(self.radius) + ", Center: " + str(self.center) # test for equality of radius # the only argument, other, is a circle # returns a boolean def __eq__ (self, other): tol = 1.0e-16 class Rectangle (object): # constructor def __init__ (self, ul_x = 0, ul_y = 1, lr_x = 1, lr_y = 0): if ((ul_x < lr_x) and (ul_y > lr_y)): self.ul = Point (ul_x, ul_y) self.lr = Point (lr_x, lr_y) else: self.ul = Point (0, 1) self.lr = Point (1, 0) # determine length of Rectangle (distance along the x axis) # takes no arguments, returns a float def length (self): # determine width of Rectangle (distance along the y axis) # takes no arguments, returns a float def width (self): # determine the perimeter # takes no arguments, returns a float def perimeter (self): # determine the area # takes no arguments, returns a float def area (self): # determine if a point is strictly inside the Rectangle # takes a point object p as an argument, returns a boolean def point_inside (self, p) # determine if another Rectangle is strictly inside this Rectangle # takes a rectangle object r as an argument, returns a boolean # should return False if self and r are equal def rectangle_inside (self, r): # determine if two Rectangles overlap (non-zero area of overlap) # takes a rectangle object r as an argument returns a boolean def rectangle_overlap (self, r): # determine the smallest rectangle that circumscribes a circle # sides of the rectangle are tangents to circle c # takes a circle object c as input and returns a rectangle object def rectangle_circumscribe (self, c): # give string representation of a rectangle # takes no arguments, returns a string def __str__ (self): return "UL: " + str(self.ul) + ", LR: " + str(self.lr) # determine if two rectangles have the same length and width # takes a rectangle other as argument and returns a boolean def __eq__ (self, other): def main(): # open the file geom.txt # create Point objects P and Q # print the coordinates of the points P and Q # find the distance between the points P and Q # create two Circle objects C and D # print C and D # compute the circumference of C # compute the area of D # determine if P is strictly inside C # determine if C is strictly inside D # determine if C and D intersect (non zero area of intersection) # determine if C and D are equal (have the same radius) # create two rectangle objects G and H # print the two rectangles G and H # determine the length of G (distance along x axis) # determine the width of H (distance along y axis) # determine the perimeter of G # determine the area of H # determine if point P is strictly inside rectangle G # determine if rectangle G is strictly inside rectangle H # determine if rectangles G and H overlap (non-zero area of overlap) # find the smallest circle that circumscribes rectangle G # goes through the four vertices of the rectangle # find the smallest rectangle that circumscribes circle D # all four sides of the rectangle are tangents to the circle # determine if the two rectangles have the same length and width # close the file geom.txt # This line above main is for grading purposes. It will not affect how # your code will run while you develop and test it. # DO NOT REMOVE THE LINE ABOVE MAIN if __name__ == "__main__": main()
Note that in this program you will be checking for the equality of two floating point numbers. Since there is a finite precision in the representation of floating point numbers, it is not always possible to determine exact equality. A working solution is to determine equality is to take the absolute value of the difference of the floating point numbers and see if the difference is less than a pre-determined tolerance. This tolerance is arbitrary and is often dictated by the problem that you are trying to solve. Here is a function that tests for equality of two floating point numbers.
def is_equal (a, b): tol = 1.0e-16 return (abs (x - y) < tol)
The structure of your output will be as follows:
Coordinates of P: Coordinates of Q: Distance between P and Q: Circle C: Circle D: Circumference of C: Area of D: P (is / is not) inside C C (is / is not) inside D C (does / does not) intersect D C (is / is not) equal to D Rectangle G: Rectangle H: Length of G: Width of H: Perimeter of G: Area of H: P (is / is not) inside G G (is / is not) inside H G (does / does not ) overlap H Circle that circumscribes G: Rectangle that circumscribes D: Rectangle G (is / is not) equal to H
This is the input file file geom.txt.
-3.0 2.0 # coordinates of P 2.0 -1.0 # coordinates of Q 1.0 3.0 2.0 # radius and center of C 5.0 -2.0 -3.0 # radius and center of D 2.0 6.0 8.0 4.0 # coord ul and lr of rectangle G -3.0 2.0 4.0 -3.0 # coord ul and lr of rectangle H
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started