In: Computer Science
To do in Python:
An ellipse is a curve in a plane surrounding two focal points such that the sum of the distances to the two focal points is constant for every point on the curve. – Wikipedia. In this assignment, you will use a Monte Carlo Simulation to estimate the overlap between two Ellipses. The assignments is divided into 4 parts: 1. Ellipse Class 2. Structure Chart 3. Implementation 4. Unit Testing.
Part 1: Ellipse Class
A. Write a class to capture the necessary information of an
ellipse.
B. Write code to thoroughly test your implementation of the ellipse
class including:
a. A constructor that accepts no variables.
b. A constructor that accepts all appropriate variables.
c. Setting and getting the appropriate variables.
d. A function that returns the area of the ellipse
e. A function that returns the circumference of the ellipse.
Part 2: Structure Chart (15 Points)
Using the top-down strategy discussed in class, produce a structure
chart to capture the necessary steps for computing the overlap of
two ellipses using the Monte Carlo Technique.
A. Each block of the chart should capture the requirements of a
function.
B. Label each edge with the input and output of the function,
similar to the examples given in class.
Part 3:
Implement your solution described by your structure chart. Your function should accept two Ellipse objects (defined in part 1) and return the area of their overlap. Use a Monte Carlo simulation to estimate the overlap. If they do not overlap, return 0.
Part 4
For this part, you will thoroughly test your implementation. For
EVERY function, beginning with the lowest level functions:
A. Print the name of the function.
B. Print the doctstring of the function.
C. Print the rationale for the test (e.g. “Testing when a point
falls within an Ellipse.”)
D. Print the specific input used to evaluate that rationale.
E. Print the result.
F. Repeat C – E until all relevant cases are tested for that
function.
Solution
PART 1
Screenshot
import math
class Ellipse:
'creates and defines an Ellipse'
def __init__(self, x1=0, y1=0, x2=0, y2=0, w=1):
self.x1 = x1
if type(self.x1) ==str:
raise TypeError("Must be integer or float. Please try again")
self.y1 = y1
if type(self.y1) ==str:
raise TypeError("Must be integer or float. Please try again")
self.x2 = x2
if type(self.x2) ==str:
raise TypeError("Must be integer or float. Please try again")
self.y2 = y2
if type(self.y2) ==str:
raise TypeError("Must be integer or float. Please try again")
self.w = w
if self.w < max ( abs(self.x1 - self.x2), abs(self.y1 - self.y2)
):
raise ValueError("Width must be greater than the largest distance
on x or y axis.")
self.F1 = Point(self.x1,self.y1)
self.F2 = Point(self.x2,self.y2)
def setF1(self, x1,y1):
self.F1 = Point(self.x1,self.y1)
def setF2(self, x2,y2):
self.F2 = Point(self.x2,self.y2)
def setW(self, w):
self.w = w
def getF1(self):
return self.F1
def getF2(self):
return self.F2
def getW(self):
return self.w
def getPair(self):
return (self.F1, self.F2)
def getSemiMajor(self):
'calculates and returns the lenght of the semi major axis'
self.semiMajor = self.F1.distance(self.F2)*.5 + (self.w -
self.F1.distance(self.F2))*.5 #half the d between F1F2 plus half
w-d
return self.semiMajor
def getSemiMinor(self):
self.semiMinor = ( (0.5*self.w)**2 - (
(0.5*self.F1.distance(self.F2) )**2) )**0.5 #using geometry to
solve for semiminor
return self.semiMinor
def getArea(self):
'calculate the area of the Ellipse'
semiMajor = self.getSemiMajor()
semiMinor = self.getSemiMinor()
return math.pi*semiMajor*semiMinor
def getCircum(self):
'circumference of the Ellipse using Ramanujan approximation
formula'
semiMajor = self.getSemiMajor()
semiMinor = self.getSemiMinor()
return math.pi*( 3*(semiMajor + semiMinor) - ( (3*semiMajor +
semiMinor) *(semiMajor + 3*semiMinor) )**0.5)
def isBoundary(self, otherXcoord, otherYcoord):
'determines is a given point is on the edge of the ellipse'
otherPoint = Point(otherXcoord,otherYcoord)
if self.F1.distance(otherPoint) + self.F2.distance(otherPoint) ==
self.w: #uses definition to determine if on boundary
return True
else:
return False
def pointInside(self, other):
'determines whether a given point is inside the boundary of the
ellipse'
if self.F1.distance(other) + self.F2.distance(other) < self.w:
#if the distance to the point is less than to a point on the
boundary, then it is inside
return True
def __repr__(self):
'canonical string representation Point(x, y)'
return 'Foci: ({}, {}), Width: {}.'.format(self.F1, self.F2,
self.w)
class Point:
'class that represents a point in the plane'
def __init__(self, xcoord=0, ycoord= 0):
'initialize coordinates to (xcoord, ycoord)'
self.x = xcoord
if type(self.x) ==str:
raise TypeError("Must be integer or float. Please try again")
self.y = ycoord
if type(self.y) ==str:
raise TypeError("Must be integer or float. Please try again")
self.p = (xcoord,ycoord)
def setx(self, xcoord):
'set x coordinate of point to xcoord'
self.x = xcoord
def sety(self, ycoord):
'set y coordinate of point to ycoord'
self.y = ycoord
def setXY(self, xcoor, ycoord):
self.p = (self.x, self.y)
def getX(self):
return self.x
def getY(self):
return self.y
def getPair(self):
'return coordinates of the point as a tuple'
return (self.x, self.y)
def distance(self, point2):
'calculates the distance between 2 cartesian points'
xdiff = point2.getX()-self.getX()
ydiff = point2.getY()-self.getY()
dist = (xdiff**2 + ydiff**2)**0.5 #using euclidean distance
calculation
return dist
def __repr__(self):
'canonical string representation Point(x, y)'
return 'Point({}, {})'.format(self.x, self.y)
print('')
print('**********INSTRUCTIONS FOR TESTING SECTION:
************************')
print('Uncomment each section below to run it, the put back into
#.')
print('When finished, return all code into comments(#)')
print('')
#
=============================================================================
# print('TESTING CLASSES WITH AND WITHOUT CONSTRUCTORS
***********************')
# print('')
# print("Creating an Ellipse using constructors
'Ellipse(1,1,2,2,4)' :")
# e1 = Ellipse(1,1,2,2,4)
# print("Result of initializing an Ellipse with constructors: ",
e1)
# print('')
# print("Creating an Ellipse without using constructors 'Ellipse()'
:")
# e2 = Ellipse()
# print("Result of initializing an Ellipse without constructors: ",
e2)
#
# print('')
# print("Creating a Point using constructors 'Point(5,9)' :")
# p1 = Point(5,9)
# print("Result of initializing a Point with constructors: ",
p1)
# print('')
# print("Creating a Point without using constructors 'Point()'
:")
# p2 = Point()
# print("Result of initializing an Ellipse without constructors: ",
p2)
#
# print('')
# print("TESTING VALID INPUTS OF ELLIPSE
***********************************************")
# print('')
# print("Creating an Ellipse using string inputs
'Ellipse(cat,20,20,20,50) - uncomment to run test' :")
# e3 = Ellipse('cat',20,20,20,50) #UNCOMMENT TO RUN TEST
# print("Result of initializing an Ellipse with bad constructors:
", e3) #UNCOMMENT TO RUN TEST
# print('')
# print("Creating an Ellipse with bad inputs - width too small
'Ellipse(-5,10,2,-5,10)' :")
# e4 = Ellipse(-5,10,2,-5,10) #COMMENT THIS TO RUN TESTS
BELOW
# print("Result of initializing an Ellipse with bad width: ",
e4)
#
# print('')
# print("TESTING VALID INPUTS OF POINT
***********************************************")
# print('')
# print("Creating a Poin using string inputs 'Point('cat',2) -
uncomment to run test' :")
# p1 = Point('cat',2) #UNCOMMENT TO RUN TEST
# print("Result of initializing an Ellipse with bad constructors:
", p1) #UNCOMMENT TO RUN TEST
# print("Creating a Poin using string inputs 'Point(2,'cat') -
uncomment to run test' :")
# p2 = Point(2,'cat') #UNCOMMENT TO RUN TEST
# print("Result of initializing an Ellipse with bad constructors:
", p2) #UNCOMMENT TO RUN TEST
#
=============================================================================
print('******TESTING of CLASS METHODS IS IN A4P4 UNIT TESTING
FILE***********************')
#TESTING OF ELLIPSE CLASS AREA AND CIRCUMFERENCE METHODS ARE IN
A4P4
#TESTING OF POINT CLASS DISTANCE METHOD IS IN A4P4
---
PART 2
PART 3
from classEllipse import Ellipse
from classEllipse import Point
import random
def main():
instructions()
e1, e2 = get2Ellipses()
x1,y1,x2,y2,x3,y3,x4,y4 = computeBox(e1,e2)
area = simulation(e1,e2)
printResults(area)
def instructions():
print('')
print("This program calculates the overlap area of 2 ellipses -if
there is any.")
print("To begin, you will be prompted to created 2
ellipses.")
print("When promted, type 'Ellipse(num1,num2,num3,num4,num5)'
providing your own numbers.")
print("The numbers correspond to foucs1(num1,num2),
focus2(num3,num4) and the width of the Ellipse.")
print("The width has to be longer than distance between the 2
foci.")
def get2Ellipses():
'creates and returns 2 Ellipses'
ellipseList = []
ellipse1 = input('Use the Elipse class and your 5 numbers for
ellipse1 here: ')
ellipseList.append(ellipse1)
ellipse2 = input('Use the Ellipse class and your 5 numbers for
ellipse2 here: ')
ellipseList.append(ellipse2)
return ellipse1, ellipse2
def simulation(ellipse1, ellipse2, bLRx, bLRy, bURx, bURy, bLLx,
bLLy, bULx, bULy):
'this calculates the area of the box, takes the percentage of
points in overlap and calculates the area of overlap'
#get each coordinate point of the box that surrounds the 2
ellipses
#bLRx, bLRy, bURx, bURy, bLLx, bLLy, bULx, bULy =
computeBox(ellipse1, ellipse2)
#create the 4 points of the box
LR = Point(bLRx, bLRy)
UR = Point(bURx, bURy)
LL = Point(bLLx, bLLy)
UL = Point(bULx, bULy)
#calculate the lenght of the sides
lenSide1 = LR.distance(UR)
lenSide2 = LR.distance(LL)
areaSquare = lenSide1*lenSide2 #calculate the area of the square
surround the 2 ellipses
#run the Monte Carlo simulation and get result
percOverlap = simulateMany(ellipse1,ellipse2,bLRx, bLRy, bURx,
bURy, bLLx, bLLy, bULx, bULy)
areaOverlap = areaSquare*percOverlap
return areaOverlap #returns areaOverlap
def computeBox(ellipse1, ellipse2):
'computes size of box big enough to surround both ellipses and
returns the coordinates of the 4 corners'
w1 = ellipse1.getW()
w2 = ellipse2.getW()
#w1 = ellipse1.getW()
#w2 = ellipse2.getW()
if w1 > w2 : # I am using 1/2width as my buffer and I want the
longer width from the 2 ellipses
wbuff = w1
else:
wbuff = w2
#box coordinates for lower right corner of box
bLRx = max(ellipse1.x1,ellipse1.x2,ellipse2.x1, ellipse2.x2) +
0.5*wbuff
bLRy = min(ellipse1.y1,ellipse1.y2,ellipse2.y1, ellipse2.y2) -
0.5*wbuff
#box coordinates for upper right corner of box
bURx = max(ellipse1.x1,ellipse1.x2,ellipse2.x1, ellipse2.x2) +
0.5*wbuff
bURy = max(ellipse1.y1,ellipse1.y2,ellipse2.y1, ellipse2.y2) +
0.5*wbuff
#box coordinates for lower left corner of box
bLLx = min(ellipse1.x1,ellipse1.x2,ellipse2.x1, ellipse2.x2) -
0.5*wbuff
bLLy = min(ellipse1.y1,ellipse1.y2,ellipse2.y1, ellipse2.y2) -
0.5*wbuff
#box coordinate for lower right corner of box
bULx = min(ellipse1.x1,ellipse1.x2,ellipse2.x1, ellipse2.x2) -
0.5*wbuff
bULy = max(ellipse1.y1,ellipse1.y2,ellipse2.y1, ellipse2.y2) +
0.5*wbuff
return bLRx, bLRy, bURx, bURy, bLLx, bLLy, bULx, bULy #coordinates
for 4 corners of bx
def simulateMany(ellipse1, ellipse2, bLRx, bLRy, bURx, bURy,
bLLx, bLLy, bULx, bULy):
'uses for loop to run simulation of random points 10,000 times,
counts successful results, calculates and returns percentage
successful'
count = 0
n = 10000
startX = int(min(bLRx, bURx, bLLx, bULx))
endX = int(max(bLRx, bURx, bLLx, bULx))
startY = int(min(bLRy, bURy, bLLy, bULy))
endY = int(max(bLRy, bURy, bLLy, bULy))
for i in range(0, n):
x = random.randint(startX, endX)
y = random.randint(startY, endY)
point = Point(x,y)
if simulateOnce(ellipse1, ellipse2, point) == True:
count += 1
print("Count is {}, number of random points is
{}".format(count,n))
return count/n
def simulateOnce(ellipse1, ellipse2, point):
'runs one simulation at a time, if point is in both ellipse 1 and
ellipse 2, then it returns True'
if ellipse1.pointInside(point) == True and
ellipse2.pointInside(point) == True:
return True
def printResults(areaOverlap):
'prints results of simlulation - total area of overlap of the 2
ellipses'
print('The area of overlap is: ', areaOverlap)
PART 4
import classEllipse
from A4P3 import computeBox
from A4P3 import simulateOnce
from A4P3 import simulateMany
from A4P3 import simulation
def main():
print('')
print('I recommend running this test a few times and comparing
results.')
print('')
TestPointDistance()
TestGetSemiMajor()
TestGetSemiMinor()
TestgetArea()
TestgetCircum()
TestIsBoundary() #didn't use this in program, but left it in
TestPointInside()
TestComputeBox()
TestSimulateOnce()
TestSimulateMany()
TestSimulation()
def TestPointDistance():
'run unit testing on point class distance method'
print("FUNCTION NAME: 'distance()' method from class Point")
print("")
help(classEllipse.Point.distance)
print("")
print("Goal is to test that distance calculations are
correct.")
print('')
#arbitrary points
point1 = classEllipse.Point(1,4)
point2 = classEllipse.Point(2,10)
point3 = classEllipse.Point(-2,6)
point4 = classEllipse.Point(0,0)
point5 = classEllipse.Point(-2,-20)
point6 = classEllipse.Point(-4,-1)
print("The inputs are: ", point1, point2, point3, point4, point5,
point6)
print('')
d = point1.distance(point2) #run the function
#results
print("The result for distance between {} and {} is {}. Actual is
{}".format(point1,point2,d,4.49999))
print("")
d = point3.distance(point4)
print("The result for distance between {} and {} is {}. Actual is
{}".format(point3,point4,d,6.324555))
d = point5.distance(point6)
print("")
print("The result for distance between {} and {} is {}. Actual is
{}".format(point5,point6,d,19.104973))
print("*******************************")
print('')
def TestGetSemiMajor():
'run unit testing on Ellipse class method that calculates the
length of the semimajor axis'
print("FUNCTION NAME: 'getSemiMajor()' method from class
Ellipse.")
print("")
help(classEllipse.Ellipse.getSemiMajor)
print("")
print("Goal is to test that we got the semi major axis lenght
correct.")
print("")
#arbitrary Ellipses
e1 = classEllipse.Ellipse(1,4,5,6,9)
e2 = classEllipse.Ellipse(2,-4,5,10,15)
#e3 = classEllipse.Ellipse(-5,10,2,-5,10) #this one should give
error message YOU NEED TO UNCOMMENT THIS TO SEE
e4 = classEllipse.Ellipse(3,9,-4,6,22)
print('')
print("The inputs are: ", e1,e2,e4)
print('')
mj1 = e1.getSemiMajor()
print("The function result for the length of the major axis is {}.
Actual is {}.".format(mj1,6.082763))
print("")
mj2 = e2.getSemiMajor()
print("The function result for the length of the major axis is {}.
Actual is {}.".format(mj2,7.49999))
print("")
#mj3 = e3.getSemiMajor()
#print("The function result for the length of the major axis is {}.
Actual is {}.".format(mj3,00000000))
print("")
mj4 = e4.getSemiMajor()
print("The function result for the length of the major axis is {}.
Actual is {}.".format(mj4,7.50000))
print("**************************************")
print('')
def TestGetSemiMinor():
'run unit testing on Ellipse class method that calculates the
length of the semiminor axis'
print("FUNCTION NAME: 'getSemiMinor() method from class
Ellipse.")
print("")
help(classEllipse.Ellipse.getSemiMinor)
print("")
print("Goal is to test that we got the semi minor axis lenght
correct.")
print("")
e1 = classEllipse.Ellipse(1,4,5,6,9)
e2 = classEllipse.Ellipse(2,-4,5,10,15)
#e3 = classEllipse.Ellipse(-5,10,2,-5,10) #this one should give
error message, YOU MUST UNCOMMENT TO SEE/TEST
e4 = classEllipse.Ellipse(3,9,-4,6,22)
print('')
print("The inputs are: ", e1,e2,e4)
print('')
mn1 = e1.getSemiMinor()
print("The function result for the length of the minor axis is {}.
Actual is {}.".format(mn1,3.905137))
print("")
mn2 = e2.getSemiMinor()
print("The function result for the length of the minor axis is {}.
Actual is {}.".format(mn2,2.236609))
print("")
#mn3 = e3.getSemiMajor()
#print("The function result for the length of the minor axis is {}.
Actual is {}".format(mn3,0000000))
print("")
mn4 = e4.getSemiMinor()
print("The function result for the length of the minor axis is {}.
Actual is {}.".format(mn4,10.31988))
print("********************************")
print('')
def TestgetArea():
'run unit testing on Ellipse class method that calculates the area
of an ellipse'
print("FUNCTION NAME: 'getArea()' method from class
Ellipse.")
print("")
help(classEllipse.Ellipse.getArea)
print("")
print("Goal is to test that we got the area calculation
correct.")
print("")
#using same Ellipses
e1 = classEllipse.Ellipse(1,4,5,6,9)
e2 = classEllipse.Ellipse(2,-4,5,10,15)
#e3 = classEllipse.Ellipse(-5,10,2,-5,10) #bad ellipse
e4 = classEllipse.Ellipse(3,9,-4,6,22)
print('')
print("The inputs are: ", e1,e2,e4)
print('')
area1 = e1.getArea()
print("The function result for the area of the ellipse is {}.
Actual is {}.".format(area1,55.21))
print("")
area2 = e2.getArea()
print("The function result for the area of the ellipse is {}.
Actual is {}.".format(area2,52.69))
print("")
print("")
area4 = e4.getArea()
print("The function result for the area of the ellipse is {}.
Actual is {}.".format(area4,356.63))
print("**********************************************")
print('')
def TestgetCircum():
'run unit testing on Ellipse class method that calculates the
circumference of an ellipse'
print("FUNCTION NAME: 'getCircum()' method from class
Ellipse.")
print("")
help(classEllipse.Ellipse.getCircum)
print("")
print("Goal is to test that we got the circumference estimate
correct.")
print("")
e1 = classEllipse.Ellipse(1,4,5,6,9)
e2 = classEllipse.Ellipse(2,-4,5,10,15)
#e3 = classEllipse.Ellipse(-5,10,2,-5,10)
e4 = classEllipse.Ellipse(3,9,-4,6,22)
print('')
print("The inputs are: ", e1,e2,e4)
print('')
circum1 = e1.getCircum()
print("The function result for the circumference of the ellipse is
{}. Compare to estimate: {}.".format(circum1,26.44))
print("")
circum2 = e2.getCircum()
print("The function result for the circumference of the ellipse is
{}. Compare to estimate: {}.".format(circum2,32.87))
print("")
print("")
circum4 = e4.getCircum()
print("The function result for the circumference of the ellipse is
{}. Compare to estimate: {}.".format(circum4,67.01))
print("*********************************************")
print('')
def TestIsBoundary():
'run unit testing on Ellipse class method that calculates whether a
point is on the boundary'
print("FUNCTION NAME: 'isBoundary()' method from class
Ellipse.")
print("")
help(classEllipse.Ellipse.isBoundary)
print("")
print("Goal is to test whether a point is on the boundary of an
ellipse.")
print("")
e1 = classEllipse.Ellipse(2,0,4,0,4)
p1 = classEllipse.Point(1,0) #on boundary
p2 = classEllipse.Point(5,0) #on boundary
p3 = classEllipse.Point(5,2) # NOT on boundary
p4 = classEllipse.Point(3,0) #NOT on boundary
print('')
print("The inputs are: ", e1,p1,p2,p3,p4)
print('')
b1 = e1.isBoundary(1,0)
print("The result should be 'True'. The actual resulat is
{}.".format(b1))
print("")
b2 = e1.isBoundary(5,0)
print("The result should be 'True'. The actual resulat is
{}.".format(b2))
print("")
b3 = e1.isBoundary(5,2)
print("The result should be 'False'. The actual resulat is
{}.".format(b3))
print("")
b4 = e1.isBoundary(3,0)
print("The result should be 'False'. The actual resulat is
{}.".format(b4))
print("")
print("********************************")
print('')
def TestPointInside():
'run unit testing on Ellipse class method that calculates whether a
point is inside an ellipse'
print("FUNCTION NAME: 'pointInside()' method from class
Ellipse.")
print("")
help(classEllipse.Ellipse.pointInside)
print("")
print("Goal is to test whether a point is inside an
ellipse.")
print("")
e1 = classEllipse.Ellipse(2,0,4,0,4)
p1 = classEllipse.Point(1,0) #on boundary
p2 = classEllipse.Point(5,0) #on boundary
p3 = classEllipse.Point(5,2) # OUTSIDE
p4 = classEllipse.Point(3,0) #INSIDE
print('')
print("The inputs are: ", e1,p1,p2,p3,p4)
print('')
b1 = e1.pointInside(p1)
print("The result should be 'None'. The actual result is
{}.".format(b1))
print("")
b2 = e1.pointInside(p2)
print("The result should be 'None'. The actual result is
{}.".format(b2))
print("")
b3 = e1.pointInside(p3)
print("The result should be 'None'. The actual result is
{}.".format(b3))
print("")
b4 = e1.pointInside(p4)
print("The result should be 'True'. The actual result is
{}.".format(b4))
print("")
print("********************************")
print('')
def TestComputeBox():
'run unit testing on function that creates a box that can enclose 2
ellipses'
print("FUNCTION NAME: 'computeBox()' function from main
program.")
print("")
help(computeBox)
print("")
print("Goal is to test that the box completely surrounds the 2
ellipses.")
print("")
#e1 fits inside e2
e1 = classEllipse.Ellipse(1,4,5,6,9)
e2 = classEllipse.Ellipse(2,-4,5,10,15)
print('')
print("The inputs are: ", e1,e2)
print('')
a,b,c,d,e,f,g,h, = computeBox(e1,e2)
print("Test vs actual values. Actual values in () : {} (12.5), {}
(-11.5), {} (12.5), {} (17.5), {} (-6.5), {} (-11.5), {} (-6.5), {}
(17.5). ".format(a,b,c,d,e,f,g,h))
#No overlap of ellipses
e3 = classEllipse.Ellipse(-2,-2,-4,-4,5)
e4 = classEllipse.Ellipse(2,-2,4,-4,5)
print('')
print("The inputs are: ", e3,e4)
print('')
a,b,c,d,e,f,g,h, = computeBox(e3,e4)
print("Test vs actual values. Actual values in () : {} (6.5), {}
(-6.5), {} (6.5), {} (0.5), {} (-6.5), {} (-6.5), {} (-6.5), {}
(0.5). ".format(a,b,c,d,e,f,g,h))
print("*************************************")
print('')
def TestSimulateOnce():
'run unit testing on function that tests whether a given point is
inside both given ellipses'
print("FUNCTION NAME 'simulateOnce()' function from main
program.")
print("")
help(simulateOnce)
print("")
print("Goal is to test whether the function correctly determines
whether a point is inside both ellipses.")
print("")
print("e1 FITS INSIDE e2 - /////////////////")
e1 = classEllipse.Ellipse(1,4,5,6,9)
e2 = classEllipse.Ellipse(2,-4,5,10,15)
p1 = classEllipse.Point(2,3) #True inside both
p2 = classEllipse.Point(12, -5) #False outside both
print('')
print("The inputs are: ", e1,e2, p1)
print('')
t = simulateOnce(e1, e2, p1)
print('')
print("The inputs are: ", e1,e2, p2)
print('')
t2 = simulateOnce(e1, e2, p2)
print('Test 1, result should be True. Actual was:
{}'.format(t))
print('Test 2, result should be None. Actual was:
{}'.format(t2))
print("THERE IS NO OVERLAP OF ELLIPSES - ///////////////")
e3 = classEllipse.Ellipse(-2,-2,-4,-4,5)
e4 = classEllipse.Ellipse(2,-2,4,-4,5)
p3 = classEllipse.Point(3,-3) #In one but not both
p4 = classEllipse.Point(-3,-3) #in the other but not both
print('')
print("The inputs are: ", e3,e4, p3)
print('')
t3 = simulateOnce(e3, e4, p3)
print('')
print("The inputs are: ", e3,e4, p4)
print('')
t4 = simulateOnce(e3, e4, p4)
print('Test 3, result should be None. Actual was:
{}'.format(t3))
print('Test 4, result should be None. Actual was:
{}'.format(t4))
print("TWO OVERLAPING HORIZONTAL ELLIPSES -
/////////////////////")
e5 = classEllipse.Ellipse(2,0,4,0,4)
e6 = classEllipse.Ellipse(4,0,6,0,4)
p5 = classEllipse.Point(4,0) #In both
p6 = classEllipse.Point(2,0) #in one but not both
p7 = classEllipse.Point(2,5) #in neither
print('')
print("The inputs are: ", e5,e6, p5)
print('')
t5 = simulateOnce(e5, e6, p5)
print('')
print("The inputs are: ", e5,e6, p6)
print('')
t6 = simulateOnce(e5, e6, p6)
print('')
print("The inputs are: ", e5,e6, p7)
print('')
t7 = simulateOnce(e5, e6, p7)
print('Test 5, result should be True. Actual was: {}'.format(t5))
#point is in both
print('Test 6, result should be None. Actual was: {}'.format(t6))
#point is only in one
print('Test 7, result should be None. Actual was: {}'.format(t7))
#point is in neither
print("TWO OVERLAPPING VERTICAL ELLIPSES -
///////////////////")
e7 = classEllipse.Ellipse(0,2,0,4,4)
e8 = classEllipse.Ellipse(0,4,0,6,4)
p8 = classEllipse.Point(0,4) #In both
p9 = classEllipse.Point(0,2) #in one but not both
p10 = classEllipse.Point(5,2) #in neither
print('')
print("The inputs are: ", e7,e8, p8)
print('')
t8 = simulateOnce(e7, e8, p8)
print('')
print("The inputs are: ", e7,e8, p9)
print('')
t9 = simulateOnce(e7, e8, p9)
print('')
print("The inputs are: ", e7,e8, p10)
print('')
t10 = simulateOnce(e7, e8, p10)
print('Test 8, result should be True. Actual was: {}'.format(t8))
#point is in both
print('Test 9, result should be None. Actual was: {}'.format(t9))
#point is only in one
print('Test 10, result should be None. Actual was: {}'.format(t10))
#point is in neither
print('')
print("************************************")
print('')
def TestSimulateMany():
'run unit testing on function that generates random points and
counts how many are in both of 2 given ellipses'
print("FUNCTION NAME: 'simulateMany()' function from main
program.")
print("")
help(simulateMany)
print("")
print("Goal is to test whether the function correctly creates
random point within the box.")
print("Also checking if it correctly runs the simulationOnce to see
if point is in both ellipses.")
print("And check if the function is counting the number of True
occurances returned by simulateOnce.")
print("*****************************************")
print('')
print("e1 FITS INSIDE e2 - /////////////////////")
e1 = classEllipse.Ellipse(1,1,2,2,4)
e2 = classEllipse.Ellipse(-2,-2,4,4,16)
a,b,c,d,e,f,g,h = computeBox(e1,e2)
result1 = simulateMany(e1,e2,a,b,c,d,e,f,g,h)
print('')
print("The ellipse inputs are: ", e1,e2)
print('')
print('The box coordinate inputs are: ', a,b,c,d,e,f,g,h,
sep=",")
print("")
print('Test 1 result was: {}%'.format(result1*100))
print("*****************************")
print('')
print("THERE IS NO OVERLAP BETWEEN THE 2 ELLIPSES -
/////////////////////////")
e3 = classEllipse.Ellipse(-2,-2,-4,-4,5)
e4 = classEllipse.Ellipse(2,-2,4,-4,5)
a,b,c,d,e,f,g,h = computeBox(e3,e4)
result2 = simulateMany(e3,e4,a,b,c,d,e,f,g,h)
print('')
print("The ellipse inputs are: ", e3,e4)
print('')
print('The box coordinate inputs are: ', a,b,c,d,e,f,g,h,
sep=",")
print("")
print('Test 2 result was: {}%. Should be
0%.'.format(result2*100))
print("********************************")
print('')
print("TWO OVERLAPING HORIZONTAL ELLIPSES -
/////////////////////")
e5 = classEllipse.Ellipse(2,0,4,0,4)
e6 = classEllipse.Ellipse(4,0,6,0,4)
a,b,c,d,e,f,g,h = computeBox(e3,e4)
result3 = simulateMany(e5,e6,a,b,c,d,e,f,g,h)
print('')
print("The ellipse inputs are: ", e5,e6)
print('')
print('The box coordinate inputs are: ', a,b,c,d,e,f,g,h,
sep=",")
print("")
print('Test 3 result was: {}%'.format(result3*100))
print("**********************************")
print('')
print("TWO OVERLAPPING VERTICAL ELLIPSES -
///////////////////")
e7 = classEllipse.Ellipse(0,2,0,4,4)
e8 = classEllipse.Ellipse(0,4,0,6,4)
a,b,c,d,e,f,g,h = computeBox(e7,e8)
result4 = simulateMany(e7,e8,a,b,c,d,e,f,g,h)
print('')
print("The ellipse inputs are: ", e7,e8)
print('')
print('The box coordinate inputs are: ', a,b,c,d,e,f,g,h,
sep=",")
print("")
print('Test 4 result was {}%'.format(result4*100))
print("")
print("******************************************************")
print('')
def TestSimulation():
'unit testing on the function that calcuates are of the box, then
calculates area of overlap and returns it'
print("FUNCTION NAME: 'simulation()' function from main
program.")
print("")
help(simulation)
print("")
print("Goal is to test whether function calculates correct area of
overlap.")
print("////////////////////////////////////////////")
print('')
#e1 fits inside e2 *****************************
e1 = classEllipse.Ellipse(1,1,2,2,4)
e2 = classEllipse.Ellipse(-2,-2,4,4,16)
a,b,c,d,e,f,g,h = computeBox(e1,e2)
result1 = simulation(e1,e2,a,b,c,d,e,f,g,h)
print('')
print("The ellipse inputs are: ", e1,e2)
print('')
print('The box coordinate inputs are: ', a,b,c,d,e,f,g,h,
sep=",")
print("")
print('Test 1 result was: {}'.format(result1))
print("///////////////////////////////////////////// ")
print('')
#No overlap of ellipses********************************
e3 = classEllipse.Ellipse(-2,-2,-4,-4,5)
e4 = classEllipse.Ellipse(2,-2,4,-4,5)
a,b,c,d,e,f,g,h = computeBox(e3,e4)
result2 = simulation(e3,e4,a,b,c,d,e,f,g,h)
print('')
print("The ellipse inputs are: ", e3,e4)
print('')
print('The box coordinate inputs are: ', a,b,c,d,e,f,g,h,
sep=",")
print("")
print('Test 2 result was: {}. Should be 0.'.format(result2))
print("/////////////////////////////////////////////")
print('')
#Overlap horizontal***********************************
e5 = classEllipse.Ellipse(2,0,4,0,4)
e6 = classEllipse.Ellipse(4,0,6,0,4)
a,b,c,d,e,f,g,h = computeBox(e3,e4)
result3 = simulation(e5,e6,a,b,c,d,e,f,g,h)
print('')
print("The ellipse inputs are: ", e5,e6)
print('')
print('The box coordinate inputs are: ', a,b,c,d,e,f,g,h,
sep=",")
print("")
print('Test 3 result was: {}'.format(result3))
print("////////////////////////////////////////")
print('')
#Overlap vertical****************************************
e7 = classEllipse.Ellipse(0,2,0,4,4)
e8 = classEllipse.Ellipse(0,4,0,6,4)
a,b,c,d,e,f,g,h = computeBox(e7,e8)
result4 = simulation(e7,e8,a,b,c,d,e,f,g,h)
print('')
print("The ellipse inputs are: ", e7,e8)
print('')
print('The box coordinate inputs are: ', a,b,c,d,e,f,g,h,
sep=",")
print("")
print('Test 4 result was {}'.format(result4))
print("/////////////////////////////////////////")
print('')
print("EXTRA CHECKING *****************")
print("Area overlap of test 1 should equal area of e1.")
print('')
print("Area overlap of test 1 = {}. Area of e1 =
{}.".format(result1, e1.getArea()))
print('')
print("Area overlap of test 2 should equal to 0.")
print('')
print("Area overlap of test 2 is {}".format(result2))
print('')
print("Area overlap of test 3 and test 4 should be almost
equal.")
print('')
print("Overlap test 3 = {}. Overlap test 4 =
{}.".format(result3,result4))
main()
---
ALL THE BEST