Skip to content
Snippets Groups Projects
Commit 7d167ebc authored by Owen Huyn's avatar Owen Huyn
Browse files

Added tutorial 5 files.

parent bae736a6
No related branches found
No related tags found
No related merge requests found
File added
File added
## @file CircleADT.py
# @author Steven Palmer
# @brief Provides the CircleT ADT class for representing circles
# @date 1/12/2017
from math import pi, sqrt
## @brief An ADT that represents a circle
class CircleT:
## @brief CircleT constructor
# @details Initializes a CircleT object with a cartesian coordinate center
# and a radius
# @param x The x coordinate of the circle center
# @param y The y coordinate of the circle center
# @param r The radius of the circle
# @exception ValueError Throws if supplied radius is negative
def __init__(self, x, y, r):
if(r < 0):
raise ValueError("Radius cannot be negative")
self.__x = x
self.__y = y
self.__r = r
## @brief Gets the x coordinate of the circle center
# @return The x coordinate of the circle center
def xcoord(self):
return self.__x
## @brief Gets the y coordinate of the circle center
# @return The y coordinate of the circle center
def ycoord(self):
return self.__y
## @brief Gets the radius of the circle
# @return The radius of the circle
def radius(self):
return self.__r
## @brief Calculates the area of the circle
# @return The area of the circle
def area(self):
return pi * self.__r ** 2
## @brief Calculates the circumference of the circle
# @return The circumference of the circle
def circumference(self):
return 2 * pi * self.__r
## @brief Determines if circle is contained within a box
# @details The circles edges may overlap with
# the bounds of the box and still be considered contained.
# @param x x coordinate of top left corner of box
# @param y y coordinate of top left corner of box
# @param w Width of box
# @param h Height of box
# @return Returns true if the circle is contained within the box; false if not
def insideBox(self, x, y, w, h):
left = x <= (self.__x - self.__r)
right = (x + w) >= (self.__x + self.__r)
top = y >= (self.__y + self.__r)
bot = (y - h) <= (self.__y - self.__r)
return left and right and top and bot
## @brief Determines whether the circle intersects another circle
# @details This function treats circles as filled objects: circles completely
# inside other circles are considered as intersecting, even though
# their edges do not cross. The set of points in each circle
# includes the boundary (closed sets).
# @param c Circle to test intersection with
# @return Returns true if the circles intersect; false if not
def intersect(self, c):
xDist = self.__x - c.xcoord()
yDist = self.__y - c.ycoord()
centerDist = sqrt(xDist ** 2 + yDist ** 2)
rSum = self.__r + c.radius()
return rSum >= centerDist
## @brief Scales the radius of the circle
# @param k Scaling factor
def scale(self, k):
self.__r *= k
## @brief Translates the center of the circle
# @param dx Distance to translate the center x coordinate
# @param dy Distance to translate the center y coordinate
def translate(self, dx, dy):
self.__x += dx
self.__y += dy
## @file Statistics.py
# @author Steven Palmer
# @brief Provides statistics functions for the CircleT class
# @date 1/12/2017
import numpy
from CircleADT import *
## @brief Calculates the average radius of a list of circles
# @param circles List of CircleT
# @return Average radius of the list of CircleT
def average(circles):
radii = [c.radius() for c in circles]
return numpy.average(radii)
## @brief Calculates the standard deviation of the radii of a list of circles
# @param circles List of CircleT
# @return Standard deviation of the radii of the list of CircleT
def stdDev(circles):
radii = [c.radius() for c in circles]
return numpy.std(radii)
## @brief Ranks a list of circles by radius
# @details Given a list of n circles, this function provides a list of integer
# rankings 1 through n that rank the circles by radius (descending).
# The positions of the ranks in the returned list correspond to the
# positions of the circles in the list of circles. In the case of ties,
# the dense ranking scheme (as defined at https://en.wikipedia.org/wiki/Ranking)
# is used.
# @param circles List of CircleT
# @return List of rankings
def rank(circles):
radii = [c.radius() for c in circles]
vsmall = float("-inf")
ranking = [0 for x in radii]
rank_curr = 0
for i in range(len(radii)):
m_curr = max(radii)
j = radii.index(m_curr)
if rank_curr == 0 or m_curr != m_prev:
rank_curr += 1
ranking[j] = rank_curr
radii[j] = vsmall
m_prev = m_curr
return ranking
import unittest
from CircleADT import *
class CirclesTests(unittest.TestCase):
def setUp(self):
self.circle = CircleT(1,2,3)
def tearDown(self):
self.circle = None
def test_xcoord_are_equal(self):
self.assertTrue(circle.xcoord() == 1)
def test_xcoord_are_not_equal(self):
self.assertFalse(circle.xcoord() != 1)
if __name__ == '__main__':
unittest.main()
\ No newline at end of file
import unittest
from Statistics import *
class StatisticsTests(unittest.TestCase):
def setUp(self):
c1 = CircleT(1,0,2)
c2 = CircleT(-3,-5,5.25)
c3 = CircleT(-2,5,10)
c4 = CircleT(1e-10,1e-10,1e-10)
c5 = CircleT(1e10,1e10,1e10)
self.circles = [c1,c2,c3,c4,c5]
def tearDown(self):
self.circle = None
def testAverageOfCircles(self):
self.assertAlmostEqual(average(self.circles), 2000000003.45, None, None, 0.1)
def test_rank(self):
self.assertTrue(rank(self.circles) == [4,3,2,5,1])
def test_stdDev(self):
self.assertAlmostEqual(stdDev(self.circles), 3999999998.275, None, None, 0.1)
if __name__ == '__main__':
unittest.main()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment