Question: Please write this program in Python, thanks! This assignment is on object oriented programming. You will be developing two classes that are fundamental in Geometry
Please write this program in Python, thanks!
This assignment is on object oriented programming. You will be developing two classes that are fundamental in Geometry - Point and Line. In main() you will test the various functions that you have written for the classes.
Here is the skeleton of the code that you will be writing. You will have to write the bodies of all the functions that I have listed. You can always add more functions. For all floating point comparisons you must use a tolerance of 1.0e-6.
import math class Point (object): # constructor with default values def __init__ (self, x = 0, y = 0): # get distance to other which is another Point object def dist (self, other): # create a string representation of a Point (x, y) def __str__ (self): # test for equality between two points def __eq__ (self, other): class Line (object): # line is defined by two Point objects p1 and p2 # constructor assign default values if user does not define # the coordinates of p1 and p2 or the two points are the same def __init__ (self, p1_x = 0, p1_y = 0, p2_x = 1, p2_y = 1): # returns True if the line is parallel to the x axis # and False otherwise def is_parallel_x (self): # returns True if the line is parallel to the y axis # and False otherwise def is_parallel_y (self): # determine slope for the line # return float ('inf') if line is parallel to the y-axis def slope (self): # determine the y-intercept of the line # return None if line is parallel to the y axis def y_intercept (self): # determine the x-intercept of the line # return None if line is parallel to the x axis def x_intercept (self): # returns True if line is parallel to other and False otherwise def is_parallel (self, other): # returns True if line is perpendicular to other and False otherwise def is_perpendicular (self, other): # returns True if Point p is on the line or an extension of it # and False otherwise def is_on_line (self, p): # determine the perpendicular distance of Point p to the line # return 0 if p is on the line def perp_dist (self, p): # returns a Point object which is the intersection point of line # and other or None if they are parallel def intersection_point (self, other): # return True if two points are on the same side of the line # and neither points are on the line # return False if one or both points are on the line or both # are on the same side of the line def on_same_side (self, p1, p2): # string representation of the line - one of three cases # y = c if parallel to the x axis # x = c if parallel to the y axis # y = m * x + b def __str__ (self): def main(): # open file "geom.txt" for reading # read the coordinates of the first Point P # read the coordinates of the second Point Q # print the coordinates of points P and Q # print distance between P and Q # print the slope of the line PQ # print the y-intercept of the line PQ # print the x-intercept of the line PQ # read the coordinates of the third Point A # read the coordinates of the fourth Point B # print the string representation of the line AB # print if the lines PQ and AB are parallel or not # print if the lines PQ and AB (or extensions) are perpendicular or not # print coordinates of the intersection point of PQ and AB if not parallel # read the coordinates of the fifth Point G # read the coordinates of the sixth Point H # print if the the points G and H are on the same side of PQ # print if the the points G and H are on the same side of AB # close file "geom.txt" 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 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-6 return (abs (x - y) < tol)
You will be reading your input from the file geom.txt. The format of the file will be exactly like this:
-3.0 2.0 # coordinates of P 2.0 -3.0 # coordinates of Q 2.0 1.0 # coordinates of A -1.0 -2.0 # coordinates of B 1.0 1.0 # coordinates of G -2.0 -1.0 # coordinates of H
The structure of your output will be as follows:
Coordinates of P: Coordinates of Q: Distance between P and Q: Slope of PQ: Y-Intercept of PQ: X-Intercept of PQ: Line AB: PQ (is / is not) parallel to AB PQ (is / is not) perpendicular to AB Intersection point of PQ and AB: # print only if PQ and AB not parallel G and H (are / are not) on the same side of PQ G and H (are / are not) on the same side of AB
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
