Skip to content
Snippets Groups Projects
Commit 721aa36e authored by W. Spencer Smith's avatar W. Spencer Smith
Browse files

Addition of L11 for Generic MIS

parent bb15a900
No related branches found
No related tags found
No related merge requests found
No preview for this file type
This diff is collapsed.
PY = python
PYFLAGS =
DOC = doxygen
DOCFLAGS =
DOCCONFIG = doxConfig
SRC = src/testCircleDeque.py
.PHONY: all test doc clean
test:
$(PY) $(PYFLAGS) $(SRC)
doc:
$(DOC) $(DOCFLAGS) $(DOCCONFIG)
cd latex && $(MAKE)
all: test doc
clean:
rm -rf html
rm -rf latex
This diff is collapsed.
## @file lineADT.py
# @author Spencer Smith
# @brief Provides the CircleT ADT class for representing a circle
# @date 2 Feb 2017
from math import *
from pointADT import *
from lineADT import *
##@brief An ADT that respresents a circle
class CircleT:
## @brief CircleT constructor
# @details Initializes a CircleT object using a center point and radius
# @param p1 The center point of the circle
# @param p2 The radius of the circle
def __init__(self, cin, rin):
self.c = cin
self.r = rin
## @brief Override default equality so comparing by field values not reference
# @details Equal if the centre and the radius are equal
# @param cin The other circle
def __eq__(self, cin):
return self.c == cin.cen() and self.r == cin.rad()
## @brief Gets the center point of the circle
# @return The center point of circle
def cen(self):
return self.c
# return PointT(self.c.xcrd(), self.c.ycrd()) #to return a new point
## @brief Gets the radius of the circle
# @return The radius circle
def rad(self):
return self.r
## @brief Gets the area of the circle
# @return The area of circle
def area(self):
return pi*(self.rad())**2.0
## @brief Determines if 2 circles intersect
# @details Finds if the 2 circles overlap or touch
# @param cin Second circle
# @return Returns true if 2 circles intersect, false otherwise
def intersect(self, cin):
centerDist = self.connection(cin).len()
rSum = self.r + cin.rad()
return rSum >= centerDist
## @brief Determines a line from the center of 1st circle to the center of the 2nd circle
# @details The length of the line is the distance between the circle center points
# @param cin Second circle
# @return The line between circle center points
def connection(self, cin):
return LineT(self.c, cin.cen())
## @brief Determines the force between 2 circles
# @details Uses the force formula
# @param f function
# @return The force between the 2 circles
def force(self, f):
return lambda c: self.area()*c.area()*f(self.connection(c).len())
## @file deque.py
# @author Spencer Smith
# @brief Implements an abstract object for a deque (double ended queue) of circles
# @date 2/2/2017
from circleADT import *
import copy
## @brief An Abstract Object that represents a sequence
class Deq:
# maximum size of deque is 20
MAX_SIZE = 20
# empty sequence
s = []
## @brief Deque initializer
@staticmethod
def init():
Deq.s = []
## @brief insert element at back
@staticmethod
def pushBack(c):
if len(Deq.s) == Deq.MAX_SIZE:
raise FULL("Maximum size exceeded")
cnew = copy.deepcopy(c)
Deq.s = Deq.s + [cnew]
## @brief insert element at front
@staticmethod
def pushFront(c):
if len(Deq.s) == Deq.MAX_SIZE:
raise FULL("Maximum size exceeded")
cnew = copy.deepcopy(c)
Deq.s = [cnew] + Deq.s
## @brief removes element at back
@staticmethod
def popBack():
size = len(Deq.s)
if size == 0:
raise EMPTY("Deque is empty")
Deq.s = Deq.s[0:(size-1)]
## @brief insert element at front
@staticmethod
def popFront():
size = len(Deq.s)
if size == 0:
raise EMPTY("Deque is empty")
Deq.s = Deq.s[1:size]
## @brief Gets last element
# @return The last element of deque
@staticmethod
def back():
size = len(Deq.s)
if size == 0:
raise EMPTY("Deque is empty")
return Deq.s[size-1]
## @brief Gets first element
# @return The first element of deque
@staticmethod
def front():
size = len(Deq.s)
if size == 0:
raise EMPTY("Deque is empty")
return Deq.s[0]
## @brief Gets length of deque
# @return The last element of deque
@staticmethod
def size():
return len(Deq.s)
## @brief Determines if the sequence of circles are disjoint
# @details Finds if any of the sequence of circles intersect with each other
# @return Returns true if no circles in the deque intersect, false otherwise
@staticmethod
def disjoint():
size = len(Deq.s)
if size == 0:
raise EMPTY("Deque is empty")
djoint = True
for i in range(0, size):
for j in range(0, size):
if i != j:
djoint = djoint and not(Deq.s[i].intersect(Deq.s[j]))
# return djoint #calculated the conventional way
return reduce(lambda x, y: x and y, \
[not(Deq.s[i].intersect(Deq.s[j])) for i in \
range(size) for j in range(size) if i != j],\
True) #calculated with the functional way
## @brief Determines sum of forces in the x component acting on first circle in the sequence
# @return Returns the sum of forces in the x component acting on first circle in the sequence
@staticmethod
def sumFx(f):
size = len(Deq.s)
if size == 0:
raise EMPTY("Deque is empty")
total = 0.0
for i in range(1, size):
total = total + __xcomp__(Deq.s[i].force(f)(Deq.s[0]), Deq.s[i], Deq.s[0])
#return total #calculated the conventional way
return reduce(lambda a, i: a + __xcomp__(Deq.s[i].force(f)(Deq.s[0]), Deq.s[i], Deq.s[0]), range(1, size), 0.0)
#calculated with the functional way
## @brief Determines the x component of Force between 2 circles
# @param F force between circles
# @param ci first circle
# @param cj second circle
# @return Returns the x component of Force between 2 circles
def __xcomp__(F, ci, cj):
return F*(ci.cen().xcrd() - cj.cen().xcrd())/ci.connection(cj).len()
## @brief An exception class for FULL deque
class FULL(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return str(self.value)
## @brief An exception class for EMPTY deque
class EMPTY(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return str(self.value)
from math import *
from pointADT import *
from lineADT import *
from circleADT import *
from deque import *
p1 = PointT(1.0, 2.0)
p2 = PointT(1.0, 10.0)
print p1.dist(p2)
p1.rot(pi)
print p1.xcrd()
print p1.ycrd()
l1 = LineT(p1, p2)
print l1.len()
p3 = l1.mdpt()
print p3.xcrd()
print p3.ycrd()
l1.rot(-pi)
print l1.beg().xcrd()
print l1.beg().ycrd()
c1 = CircleT(p1, 9.0)
print c1.area()
c2 = CircleT(p2, 18.0)
print c1.intersect(c2)
l2 = c1.connection(c2)
print "length =" + str(l1.len())
grav = lambda r: 6e-7*r**(-3)
print (c1.force(grav))(c2)
p1 = PointT(1.0, 2.0)
p2 = PointT(1.0, 10.0)
c1 = CircleT(p1, 3.0)
c2 = CircleT(p2, 2.0)
Deq.init()
Deq.pushBack(c1)
Deq.pushFront(c2)
Deq.popBack()
Deq.popBack()
print Deq.size()
#Deq.popBack()
Deq.pushBack(c1)
Deq.pushFront(c2)
print Deq.back().rad()
print Deq.front().rad()
print Deq.disjoint()
Deq.popFront()
print Deq.disjoint()
c3 = CircleT(p2, 1.0)
Deq.pushFront(c3)
print Deq.disjoint()
p1 = PointT(0.0, 0.0)
p2 = PointT(4.0, 0.0)
c1 = CircleT(p1, 1.0)
c2 = CircleT(p2, 1.0)
Deq.init()
Deq.pushFront(c2)
Deq.pushFront(c1)
f = lambda r: 6.672*r**(-3)
print Deq.sumFx(f)
p3 = PointT(8.0, 0.0)
c3 = CircleT(p3, 1.0)
Deq.pushBack(c3)
print Deq.sumFx(f)
pnew = c3.cen()
print pnew.xcrd()
pnew.rot(3.0)
print pnew.xcrd()
p4 = c3.cen()
print p4.xcrd()
## @file lineADT.py
# @author Spencer Smith
# @brief Provides the LineT ADT class for representing a line in 2D
# @date 2 Feb 2017
from pointADT import *
##@brief An ADT that respresents a line in 2D
class LineT:
## @brief LineT constructor
# @details Initializes a LineT object with 2 points
# @param p1 The start point of line
# @param p2 The end point of line
def __init__(self, p1, p2):
self.b = p1
self.e = p2
## @brief Override default equality so comparing by field values not reference
# @details Equal if the corresponding end points of the two lines are equal
# @param l The other line
def __eq__(self, l):
return self.b == l.beg() and self.e == l.end()
## @brief Gets the beginning of the line
# @return The start point of line
def beg(self):
return self.b
## @brief Gets the ending of the line
# @return The end point of line
def end(self):
return self.e
## @brief Gets the length of the line
# @return The length of line
def len(self):
return self.b.dist(self.e)
## @brief Determines the midpoint of the line
# @return The midpoint point of line
def mdpt(self):
return PointT((self.b.xcrd()+self.e.xcrd())/2.0, (self.b.ycrd()+self.e.ycrd())/2.0)
## @brief Sets the new points of the line after rotation
# @details Rotates the line around the origin by angle of phi radians
# @para phi angle of rotation in radians
def rot(self, phi):
self.b.rot(phi)
self.e.rot(phi)
def __avg__(x1, x2): #not necessary, for illustrative purposes
return (x1+x2)/2.0
## @file pointADT.py
# @author Spencer Smith
# @brief Provides the PointT ADT class for representing 2D points
# @date 2 Feb 2017
from math import *
##@brief An ADT that respresents a 2D point
class PointT:
## @brief PointT constructor
# @details Initializes a PointT object with a cartesian coordinate
# @param x The x coordinate of the point
# @param y The y coordinate of the point
def __init__(self, x, y):
self.xc = x
self.yc = y
## @brief Override default equality so comparing by field values not reference
# @details Equal if the corresponding coordinates of the two points are equal
# @param p The other point
def __eq__(self, p):
return self.xc == p.xcrd() and self.yc == p.ycrd()
## @brief Gets the x coordinate of the point
# @return The x coordinate of the point
def xcrd(self):
return self.xc
## @brief Gets the y coordinate of the point
# @return The y coordinate of the point
def ycrd(self):
return self.yc
## @brief Determines the distance between 2 points
# @details Uses pythagorean theorem
# @param p Another point
# @return The distance between the given points
def dist(self, p):
xDist = self.xc - p.xcrd()
yDist = self.yc - p.ycrd()
return sqrt(xDist ** 2 + yDist ** 2)
## @brief Sets the new coordinates after rotation
# @details Rotates the point around the origin by angle of phi radians
# @para phi angle of rotation in radians
def rot(self, phi):
xc_old = self.xc
self.xc = cos(phi)*self.xc - sin(phi)*self.yc
self.yc = sin(phi)*xc_old + cos(phi)*self.yc
## @file testCircleDeque.py
# @author Gurankash Singh
# @brief Implements 25 Pyunit tests
import unittest
import math
from pointADT import *
from lineADT import *
from circleADT import *
from deque import *
class PointTest(unittest.TestCase):
def setUp(self):
## Declaring Points
self.p1 = PointT(1.5,2)
self.p2 = PointT(-1.5,6)
self.p3 = PointT(-1.5,10)
## Declaring Line
self.l1 = LineT(self.p1,self.p2)
## Declaring Circles
self.c1 = CircleT(self.p1,5)
self.c2 = CircleT(self.p2,7.5)
self.c3 = CircleT(self.p3,3)
self.c4 = CircleT(self.p3,math.sqrt(73)-5)
## Declaring force lambda functions
self.f1 = lambda x: x
self.f2 = lambda x: 5/(x**2)
def tearDown(self):
self.p1 = None
self.p2 = None
self.l1 = None
self.c1 = None
self.c2 = None
self.c3 = None
self.c4 = None
## Point Testing
## @brief Tests xcrd method of PointT class
def test_xcrd(self):
self.assertTrue(self.p1.xcrd() == 1.5)
self.assertTrue(self.p2.xcrd() == -1.5)
## @brief Tests ycrd method of PointT class
def test_ycrd(self):
self.assertTrue(self.p1.ycrd() == 2)
self.assertTrue(self.p2.ycrd() == 6)
## @brief Tests dist method of PointT class
def test_dist(self):
self.assertTrue(self.p1.dist(self.p2) == 5)
## @brief Tests rot method of PointT class
def test_rot1(self):
#90 degrees rotation
self.p1.rot(math.pi/2)
self.assertAlmostEqual(self.p1.xcrd(),-2,None,None,0.0001)
self.assertAlmostEqual(self.p1.ycrd(),1.5,None,None,0.0001)
## @brief Tests rot method of PointT class
def test_rot2(self):
#180 degrees rotation
self.p2.rot(math.pi)
self.assertAlmostEqual(self.p2.xcrd(),1.5,None,None,0.0001)
self.assertAlmostEqual(self.p2.ycrd(),-6,None,None,0.0001)
## Line Testing
## @brief Tests beg method of LineT class
def test_beg(self):
self.assertTrue([self.l1.beg().xcrd(),self.l1.beg().ycrd()] == [1.5,2])
## @brief Tests end method of LineT class
def test_end(self):
self.assertTrue([self.l1.end().xcrd(),self.l1.end().ycrd()] == [-1.5,6])
## @brief Tests len method of LineT class
def test_len(self):
self.assertTrue(self.l1.len() == 5)
## @brief Tests mdpt method of LineT class
def test_mdpt(self):
self.assertTrue(self.l1.mdpt().xcrd() == 0)
self.assertTrue(self.l1.mdpt().ycrd() == 4)
## @brief Tests rot method of LineT class
def test_rot(self):
#180 degrees rotation
self.l1.rot(math.pi)
self.assertAlmostEqual(self.p1.xcrd(),-1.5,None,None,0.0001)
self.assertAlmostEqual(self.p1.ycrd(),-2,None,None,0.0001)
self.assertAlmostEqual(self.p2.xcrd(),1.5,None,None,0.0001)
self.assertAlmostEqual(self.p2.ycrd(),-6,None,None,0.0001)
## Circle Testing
## @brief Tests cen method of CircleT class
def test_cen(self):
self.assertTrue([self.c1.cen().xcrd(),self.c1.cen().ycrd()] == [1.5,2])
self.assertTrue([self.c2.cen().xcrd(),self.c2.cen().ycrd()] == [-1.5,6])
## @brief Tests rad method of CircleT class
def test_rad(self):
self.assertTrue(self.c1.rad() == 5)
self.assertTrue(self.c2.rad() == 7.5)
## @brief Tests area method of CircleT class
def test_area(self):
self.assertAlmostEqual(self.c1.area(),math.pi*5**2,None,None,0.0001)
self.assertAlmostEqual(self.c2.area(),math.pi*7.5**2,None,None,0.0001)
## @brief Tests intersect method of CircleT class
def test_intersect(self):
#Intersecting
self.assertTrue(self.c1.intersect(self.c2) == True)
#Not intersecting
self.assertFalse(self.c1.intersect(self.c3) == True)
#Intersecting at boundary
self.assertTrue(self.c1.intersect(self.c4) == True)
## @brief Tests connection method of CircleT class
def test_connection(self):
self.assertTrue(self.c1.connection(self.c2).beg().xcrd() == 1.5)
self.assertTrue(self.c1.connection(self.c2).beg().ycrd() == 2)
## @brief Tests force method of CircleT class
def test_force(self):
self.assertAlmostEqual(self.c1.force(self.f1)(self.c2),(math.pi**2)*(5**2)*(7.5**2)*(5),None,None,0.0001)
## Deque Testing
## @brief Tests pushFront method of Deq class
def test_pushFront(self):
Deq.init()
Deq.pushFront(self.c1)
self.assertTrue(Deq.front() == self.c1)
self.assertTrue(Deq.front() == Deq.back())
for i in range(0,Deq.MAX_SIZE-1):
Deq.pushFront(self.c2)
self.assertRaises(Exception,Deq.pushFront,self.c3)
## @brief Tests pushBack method of Deq class
def test_pushBack(self):
Deq.init()
Deq.pushBack(self.c1)
self.assertTrue(Deq.back() == self.c1)
self.assertTrue(Deq.back() == Deq.front())
for i in range(0,Deq.MAX_SIZE-1):
Deq.pushBack(self.c2)
self.assertRaises(Exception,Deq.pushBack,self.c3)
## @brief Tests popFront method of Deq class
def test_popFront(self):
Deq.init()
Deq.pushFront(self.c1)
Deq.pushFront(self.c2)
Deq.popFront()
self.assertTrue(Deq.front() == self.c1)
## @brief Tests popBack method of Deq class
def test_popBack(self):
Deq.init()
Deq.pushBack(self.c1)
Deq.pushBack(self.c2)
Deq.popBack()
self.assertTrue(Deq.back() == self.c1)
## @brief Tests front method of Deq class
def test_front(self):
Deq.init()
self.assertRaises(Exception,Deq.front)
## @brief Tests back method of Deq class
def test_back(self):
Deq.init()
self.assertRaises(Exception,Deq.back)
## @brief Tests size method of Deq class
def test_size(self):
Deq.init()
self.assertTrue(Deq.size() == 0)
Deq.pushFront(self.c1)
self.assertTrue(Deq.size() == 1)
Deq.popBack()
self.assertTrue(Deq.size() == 0)
## @brief Tests disjoint method of Deq class
def test_disjoint(self):
Deq.init()
Deq.pushFront(self.c1)
self.assertTrue(Deq.disjoint())
#Intersecting at boundary
Deq.pushBack(self.c4)
self.assertFalse(Deq.disjoint())
Deq.popBack()
#Not intersecting
Deq.pushBack(self.c3)
self.assertTrue(Deq.disjoint())
## @brief Tests sumFx method of Deq class
def test_sumFx(self):
Deq.init()
#Empty Deq
self.assertRaises(Exception, Deq.sumFx, self.f2)
#Push c1
Deq.pushFront(self.c1)
self.assertTrue(Deq.sumFx(self.f2) == 0)
#Push c2
Deq.pushBack(self.c2)
self.assertAlmostEqual(Deq.sumFx(self.f2),-1665.495742,None,None,0.0001)
#Push c3
Deq.pushBack(self.c3)
self.assertAlmostEqual(Deq.sumFx(self.f2),-1718.901641,None,None,0.0001)
#Push c4
Deq.pushBack(self.c4)
self.assertAlmostEqual(Deq.sumFx(self.f2),-1793.432319,None,None,0.0001)
if __name__ == '__main__':
unittest.main()
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