This assignment is on object oriented programming. You will be developing several classes that are fundamental in Geometry - Point, Line, and Triangle. 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.
import math class Point (object): # constructor def __init__ (self, x = 0, y= 0): # get distance def dist (self, other): # string representation of a Point object def __str__ (self): # test for equality def __eq__ (self, other): class Line (object): # constructor (assign default values if user defined points are the same) def __init__ (self, p1_x = 0, p1_y = 0, p2_x = 1, p2_y = 1): # determine if line is parallel to x axis def is_parallelX (self): # determine if line is parallel to y axis def is_parallelY (self): # determine if two lines are parallel def is_parallel (self, other): # get slope of line # return float('inf') if line is parallel to the y-axis def slope (self): # get x axis intercept if line is not parallel to the x axis def x_intercept (self): # get y axis intercept if line is not parallel to the y axis def y_intercept (self): # return intersection point if two lines are not parallel def intersection_point (self, other): # determine if two lines are perpendicular to each other def is_perpendicular (self, other): # determine if a point is on the line or an extension of it def on_line (self, p): # determine the perpendicular distance of a point to the line # if the point is not on the line def dist (self, p): # determine if two points are on the same side of the line # if one or both the points are on the line return False def on_same_side (self, p, q): # string representation of a line # y = m * x + b # y = c # x = c def __str__ (self): # determine if two lines are equal, have same slope and intercept def __eq__ (self, other): class Triangle (object): # constructor (assign default vertices (0,0), (1,0), and (0, 1)) # assign default vertices if user defined points do not form a triangle def __init__ (self, v1_x = 0, v1_y =0, v2_x = 1, v2_y = 0, v3_x = 0, v3_y = 1): # determine if three points form a triangle def is_triangle (self, v1_x, v1_y, v2_x, v2_y, v3_x, v3_y): # calculate area of triangle # if sides are a, b, and c, then s = (a + b + c) / 2 # area = math.sqrt (s * (s - a) * (s -b) * (s - c)) def area (self): # calculate perimeter def perimeter (self): # determine if a point is inside the triangle def is_point_inside (self, p): # determine if the triangle is completely inside the other triangle def is_inside_triangle (self, other): # determine if the triangle overlaps the other triangle def does_overlap_triangle (self, other): # determine if a line passes through the triangle even if it is one point def does_intersect (self, line): # string representation of all three vertices def __str__ (self): # determine if two triangles are congruent # three sides of one are equal to three sides of the other def __eq__ (self, other): def main(): # open file "geometry.txt" for reading # read the coordinates of the first Point P # read the coordinates of the second Point Q # print distance between P and Q # print the slope and intercept of the line passing through P and Q # read the coordinates of the third Point A # read the coordinates of the fourth Point B # print the slope and intercept of the line passing through A and B # print if the lines PQ and AB are parallel or not # print if the lines PQ and AB are perpendicular or not # print if coordinates of the intersection point if PQ is not parallel to AB # 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 # read the coordinates of the vertices R, S, and T # print the perimeter of triangle RST # print the area of triangle RST # print if the line PQ passes through the triangle RST # print if the line AB passes through the triangle RST # read the coordinates of the vertices J, K, and L # print if triangle JKL is inside triangle RST # print if triangle JKL overlaps triangle RST # print if triangle JKL is congruent to triangle RST # close file "geometry.txt" 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): delta = 1.0e-16 return (abs (x - y) < delta)
You will be reading your input from the file geometry.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 -1.0 4.0 -3.0 1.0 2.0 2.0 # coordinates of the vertices R, S, and T -2.0 2.0 -3.0 -2.0 2.0 -1.0 # coordinates of the vertices J, K, and LThe structure of your output will be as follows:
Coordinates of P: (-3.0, 2.0) Coordinates of Q: (2.0, -3.0) Distance between P and Q: Slope and Intercept of PQ: Coordinates of A: (2.0, 1.0) Coordinates of B: (-1.0, -2.0) Slope and Intercept of AB: PQ (is / is not) parallel to AB PQ (is / is not) perpendicular to AB Coordinates of intersection point of PQ and AB: Coordinates of G: (1.0, 1.0) Coordinates of H: (-2.0, -1.0) G and H (are / are not) on the same side of PQ G and H (are / are not) on the same side of AB Vertices of triangle RST: (-1.0, 4.0), (-3.0, 1.0), (2.0, 2.0) Perimeter of triangle RST: Area of triangle RST: PQ (does / does not) pass through triangle RST AB (does / does not) pass through triangle RST Vertices of triangle JKL: (-2.0, 2.0), (-3.0, -2.0), (2.0, -1.0) Triangle JKL (is / is not) inside triangle RST Triangle JKL (does / does not) overlap triangle RST Triangle JKL (is / is not) congruent to triangle RST
We will be looking at good documentation, design, and adherence to the coding convention discussed in class. You may use the same variable names used in the problem statement or come up with your own. Your code must have the following header:
# File: Geometry.py # Description: # Student Name: # Student UT EID: # Course Name: CS 313E # Unique Number: 53580 # Date Created: # Date Last Modified:
Use the turnin program to submit your Geometry.py file. We should receive your work by 11 PM on Friday 31 January 2014. There will be substantial penalties if you do not adhere to the guidelines. The student assistant in charge of this assignment is Daniel Monroy (dmonroy@utexas.edu).