Basic Geometry (Due 05 February 2017)

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.

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 
  def __init__ (self, x = 0, y = 0):
    self.x = x
    self.y = y

  # get distance
  def dist (self, other):
    return math.hypot (self.x - other.x, self.y - other.y)

  # get a string representation of a Point object
  def __str__ (self):
    return '(' + str(self.x) + ", " + str(self.y) + ")"

  # test for equality
  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 = 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 intersects this circle (non-zero area of overlap)
  def does_intersect (self, c):
   
  # determine the smallest circle that circumscribes a rectangle
  # the circle goes through all the vertices of the rectangle
  def circle_circumscribes (self, r):

  # string representation of a circle
  def __str__ (self):
    
  # test for equality of radius
  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)
  def length (self):

  # determine width of Rectangle (distance along the y axis)
  def width (self):

  # determine the perimeter
  def perimeter (self):
    
  # determine the area
  def area (self):

  # determine if a point is strictly inside the Rectangle
  def point_inside (self, p)

  # determine if another Rectangle is strictly inside this Rectangle
  def rectangle_inside (self, r):

  # determine if two Rectangles overlap (non-zero area of overlap)
  def does_intersect (self, other):

  # determine the smallest rectangle that circumscribes a circle
  # sides of the rectangle are tangents to circle c
  def rect_circumscribe (self, c):

  # give string representation of a rectangle
  def __str__ (self):

  # determine if two rectangles have the same length and width
  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
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-16
  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 -1.0                           # coordinates of Q
2.0 1.0 3.0                        # center and radius of C
-2.0 -3.0 5.0                      # center and radius 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
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

For this assignment you may work with a partner. Both of you must read the paper on Pair Programming. .

The file that you will be turning in will be called Geom.py. We are looking for clean and structured design. The file will have a header of the following form:

#  File: Geom.py

#  Description:

#  Student Name:

#  Student UT EID:

#  Partner Name:

#  Partner UT EID:

#  Course Name: CS 313E

#  Unique Number: 

#  Date Created:

#  Date Last Modified:

If you are working with a partner both of you will be submitting a single program (from either account) but make sure that you have your partner's name and eid in your program. If you are working alone, then remove the two lines that has the partner's name and eid in the header.

Use the Canvas system to submit your Geom.py file. We should receive your work by 11 PM on Sunday, 5 Feb 2017. There will be substantial penalties if you do not adhere to the guidelines. Remember Python is case sensitive. The name of your file must match exactly what we have specified.