Ejemplo n.º 1
0
    def testValidConstruction(self):
        p1 = Point(1, 3)
        p2 = Point(5, 3)
        p3 = Point(5, 1)
        p4 = Point(1, 1)
        r1 = ShapeFactory.build("rectangle", p1, p2, p3, p4)

        self.assertEqual(p1, r1.point1)
        self.assertEqual(p2, r1.point2)
        self.assertEqual(p3, r1.point3)
        self.assertEqual(p4, r1.point4)

        l1 = Line(Point(-3, 1), Point(1, 1))
        l2 = Line(Point(1, 1), Point(1, -5))
        l3 = Line(Point(1, -5), Point(-3, -5))
        l4 = Line(Point(-3, -5), Point(-3, 1))
        r2 = ShapeFactory.build("rectangle", l1.point1, l2.point1, l3.point1,
                                l4.point1)

        self.assertEqual(l1, r2.line1)
        self.assertEqual(l2, r2.line2)
        self.assertEqual(l3, r2.line3)
        self.assertEqual(l4, r2.line4)

        r3 = ShapeFactory.build("rectangle", Point(-.8, .4), Point(0, 2),
                                Point(.8, 1.6), Point(0, 0))
        self.assertEqual(-.8, r3.point1.x)
        self.assertEqual(.4, r3.point1.y)
        self.assertEqual(0, r3.point2.x)
        self.assertEqual(2, r3.point2.y)
        self.assertEqual(.8, r3.point3.x)
        self.assertEqual(1.6, r3.point3.y)
        self.assertEqual(0, r3.point4.x)
        self.assertEqual(0, r3.point4.y)
Ejemplo n.º 2
0
    def testComputeArea(self):
        center = Point(0, 0)
        r1 = ShapeFactory.build("rectangle", Point(1, 1), Point(4, 1),
                                Point(4, 3), Point(1, 3))
        t1 = ShapeFactory.build("triangle", Point(1, 2), Point(5, 1),
                                Point(3, 3))
        c1 = ShapeFactory.build("circle", Point(0, 0), Point(2, 0),
                                Point(0, 2))
        e1 = ShapeFactory.build("ellipse", Point(0, 0), Point(3, 0),
                                Point(0, 2))
        l1 = ShapeFactory.build("line", Point(4, 2), Point(6, 1))
        cm1 = ShapeFactory.build("composite", center, r1)
        expected = r1.computeArea()
        actual = cm1.computeArea()
        self.assertEqual(expected, actual)

        cm2 = ShapeFactory.build("composite", center, t1, c1)
        expected = t1.computeArea() + c1.computeArea()
        actual = cm2.computeArea()
        self.assertEqual(expected, actual)

        cm3 = ShapeFactory.build("composite", center, cm1, e1, cm2)
        expected = cm1.computeArea() + e1.computeArea() + cm2.computeArea()
        actual = cm3.computeArea()
        self.assertEqual(expected, actual)

        cm4 = ShapeFactory.build("composite", center)
        expected = 0
        actual = cm4.computeArea()
        self.assertEqual(expected, actual)

        cm5 = ShapeFactory.build("composite", center, l1, r1)
        expected = r1.computeArea()
        actual = cm5.computeArea()
        self.assertEqual(expected, actual)
Ejemplo n.º 3
0
    def testValidConstruction(self):
        p1 = Point(1, 2)
        p2 = Point(1, 1)
        p3 = Point(2, 1)
        t1 = ShapeFactory.build("triangle", p1, p2, p3)

        self.assertEqual(p1, t1.point1)
        self.assertEqual(p2, t1.point2)
        self.assertEqual(p3, t1.point3)

        l1 = Line(Point(4.33, 8), Point(-4, 3.67))
        l2 = Line(Point(-4, 3.67), Point(2.19, -5))
        l3 = Line(Point(2.19, -5), Point(4.33, 8))
        t2 = ShapeFactory.build("triangle", l1.point1, l2.point1, l3.point1)

        self.assertEqual(l1, t2.line1)
        self.assertEqual(l2, t2.line2)
        self.assertEqual(l3, t2.line3)

        t3 = ShapeFactory.build("triangle", Point(1, 2), Point(5, 1), Point(3, 3))
        self.assertEqual(1, t3.point1.x)
        self.assertEqual(2, t3.point1.y)
        self.assertEqual(5, t3.point2.x)
        self.assertEqual(1, t3.point2.y)
        self.assertEqual(3, t3.point3.x)
        self.assertEqual(3, t3.point3.y)
Ejemplo n.º 4
0
    def testComputeRadius(self):
        p1 = Point(0, 0)
        p2 = Point(2, 0)
        p3 = Point(0, 2)
        c1 = ShapeFactory.build("circle", p1, p2, p3)
        self.assertAlmostEqual(2, c1.computeRadius(), places=3)

        c2 = ShapeFactory.build("circle", Point(1, 1), Point(6, 6), Point(-4, 6))
        self.assertAlmostEqual(7.071, c2.computeRadius(), places=3)
Ejemplo n.º 5
0
    def testComputeLength(self):
        l1 = ShapeFactory.build("line", Point(1, 2), Point(4, 10))
        self.assertAlmostEqual(8.544, l1.computeLength(), places=3)

        l2 = ShapeFactory.build("line", Point(1, 2), Point(1, 3))
        self.assertAlmostEqual(1, l2.computeLength(), places=3)

        l3 = ShapeFactory.build("line", Point(3, -2), Point(-4, 10))
        self.assertAlmostEqual(13.892, l3.computeLength(), places=3)
Ejemplo n.º 6
0
    def testComputeArea(self):
        t1 = ShapeFactory.build("triangle", Point(1, 1), Point(2, 1), Point(1, 2))
        self.assertAlmostEqual(.5, t1.computeArea(), places=4)

        t2 = ShapeFactory.build("triangle", Point(1, 2), Point(5, 1), Point(3, 3))
        self.assertAlmostEqual(3, t2.computeArea(), places=4)

        t3 = ShapeFactory.build("triangle", Point(-3, 3), Point(2, 4), Point(3, -1))
        self.assertAlmostEqual(13, t3.computeArea(), places=4)
Ejemplo n.º 7
0
 def testGetShape(self):
     center = Point(0, 0)
     t1 = ShapeFactory.build("triangle", Point(1, 2), Point(5, 1),
                             Point(3, 3))
     c1 = ShapeFactory.build("circle", Point(0, 0), Point(2, 0),
                             Point(0, 2))
     cm1 = ShapeFactory.build("composite", center, t1, c1)
     self.assertEqual(cm1.getShape(0), t1)
     self.assertEqual(cm1.getShape(1), c1)
     self.assertRaises(ShapeException, cm1.getShape, 2)
Ejemplo n.º 8
0
    def testComputeArea(self):
        e1 = ShapeFactory.build("ellipse", Point(0, 0), Point(2, 0),
                                Point(0, 2))

        self.assertAlmostEqual(12.566, e1.computeArea(), places=3)

        e2 = ShapeFactory.build("ellipse", Point(1, 1), Point(6, 6),
                                Point(-2, 4))

        self.assertAlmostEqual(94.248, e2.computeArea(), places=3)
Ejemplo n.º 9
0
 def testAddShape(self):
     center = Point(0, 0)
     cm1 = ShapeFactory.build("composite", center)
     self.assertEqual(0, len(cm1.shapes))
     r1 = ShapeFactory.build("rectangle", Point(1, 1), Point(4, 1),
                             Point(4, 3), Point(1, 3))
     t1 = ShapeFactory.build("triangle", Point(1, 2), Point(5, 1),
                             Point(3, 3))
     cm1.addShape(r1)
     self.assertEqual(cm1.getShape(0), r1)
     cm1.addShape(t1)
     self.assertEqual(cm1.getShape(1), t1)
Ejemplo n.º 10
0
    def testComputeArea(self):
        r1 = ShapeFactory.build("rectangle", Point(0, 1), Point(1, 1),
                                Point(1, 0), Point(0, 0))
        self.assertAlmostEqual(1, r1.computeArea(), places=4)

        r2 = ShapeFactory.build("rectangle", Point(-2, 2), Point(3, 2),
                                Point(3, 0), Point(-2, 0))
        self.assertAlmostEqual(10, r2.computeArea(), places=4)

        r3 = ShapeFactory.build("rectangle", Point(-.8, .4), Point(0, 2),
                                Point(.8, 1.6), Point(0, 0))
        self.assertAlmostEqual(1.6, r3.computeArea(), places=4)
Ejemplo n.º 11
0
    def testClearShapes(self):
        center = Point(0, 0)
        t1 = ShapeFactory.build("triangle", Point(1, 2), Point(5, 1),
                                Point(3, 3))
        c1 = ShapeFactory.build("circle", Point(0, 0), Point(2, 0),
                                Point(0, 2))
        cm1 = ShapeFactory.build("composite", center, t1, c1)
        self.assertEqual(cm1.getShape(0), t1)
        self.assertEqual(cm1.getShape(1), c1)
        self.assertEqual(len(cm1.shapes), 2)

        cm1.clearShapes()
        self.assertEqual(len(cm1.shapes), 0)
Ejemplo n.º 12
0
    def testValidConstruction(self):
        center = Point(0, 0)
        r1 = ShapeFactory.build("rectangle", Point(1, 1), Point(4, 1),
                                Point(4, 3), Point(1, 3))
        t1 = ShapeFactory.build("triangle", Point(1, 2), Point(5, 1),
                                Point(3, 3))
        c1 = ShapeFactory.build("circle", Point(0, 0), Point(2, 0),
                                Point(0, 2))
        e1 = ShapeFactory.build("ellipse", Point(0, 0), Point(3, 0),
                                Point(0, 2))
        cm1 = ShapeFactory.build("composite", center, r1)
        self.assertEqual(center, cm1.center)
        self.assertEqual(cm1.getShape(0), r1)

        cm2 = ShapeFactory.build("composite", center, t1, c1)
        self.assertEqual(center, cm2.center)
        self.assertEqual(cm2.getShape(0), t1)
        self.assertEqual(cm2.getShape(1), c1)

        cm3 = ShapeFactory.build("composite", center, cm1, e1, cm2)
        self.assertEqual(center, cm3.center)
        self.assertEqual(cm3.getShape(0), cm1)
        self.assertEqual(cm3.getShape(1), e1)
        self.assertEqual(cm3.getShape(2), cm2)

        cm4 = ShapeFactory.build("composite", center)
        self.assertEqual(center, cm4.center)
Ejemplo n.º 13
0
    def testComputeSlope(self):
        l1 = ShapeFactory.build("line", Point(2, 2), Point(4, 10))
        self.assertAlmostEqual(4, l1.computeSlope(), places=3)

        l2 = ShapeFactory.build("line", Point(2, 2), Point(10, 4))
        self.assertAlmostEqual(.25, l2.computeSlope(), places=3)

        l3 = ShapeFactory.build("line", Point(2, 2), Point(4, 2))
        self.assertAlmostEqual(0, l3.computeSlope(), places=3)

        l4 = ShapeFactory.build("line", Point(2, 2), Point(2, 4))
        self.assertAlmostEqual(float('inf'), l4.computeSlope(), places=3)

        l5 = ShapeFactory.build("line", Point(2, 4), Point(2, 2))
        self.assertAlmostEqual(float('-inf'), l5.computeSlope(), places=3)
Ejemplo n.º 14
0
    def testValidateLine(self):
        l1 = ShapeFactory.build("line", Point(1, 1), Point(-1, -5))
        Line.validateLine(l1, "Line unexpectedly invalid")

        self.assertRaises(ShapeException, Line.validateLine, "(1, 1, -1, -5)",
                          "String \'(1, 1, -1, -5)\' is not a valid line")
        self.assertRaises(ShapeException, Line.validateLine, Point(1, 1), "Point is not a valid line")
Ejemplo n.º 15
0
    def testMove(self):
        r1 = ShapeFactory.build("rectangle", Point(-.8, .4), Point(0, 2),
                                Point(.8, 1.6), Point(0, 0))

        r1.move(3, 4)
        self.assertAlmostEqual(2.2, r1.point1.x)
        self.assertAlmostEqual(4.4, r1.point1.y)
        self.assertAlmostEqual(3, r1.point2.x)
        self.assertAlmostEqual(6, r1.point2.y)
        self.assertAlmostEqual(3.8, r1.point3.x)
        self.assertAlmostEqual(5.6, r1.point3.y)
        self.assertAlmostEqual(3, r1.point4.x)
        self.assertAlmostEqual(4, r1.point4.y)

        r1.move(-.234, -1.987)
        self.assertAlmostEqual(1.966, r1.point1.x)
        self.assertAlmostEqual(2.413, r1.point1.y)
        self.assertAlmostEqual(2.766, r1.point2.x)
        self.assertAlmostEqual(4.013, r1.point2.y)
        self.assertAlmostEqual(3.566, r1.point3.x)
        self.assertAlmostEqual(3.613, r1.point3.y)
        self.assertAlmostEqual(2.766, r1.point4.x)
        self.assertAlmostEqual(2.013, r1.point4.y)

        r1.move(.234, 1.987)
        self.assertAlmostEqual(2.2, r1.point1.x)
        self.assertAlmostEqual(4.4, r1.point1.y)
        self.assertAlmostEqual(3, r1.point2.x)
        self.assertAlmostEqual(6, r1.point2.y)
        self.assertAlmostEqual(3.8, r1.point3.x)
        self.assertAlmostEqual(5.6, r1.point3.y)
        self.assertAlmostEqual(3, r1.point4.x)
        self.assertAlmostEqual(4, r1.point4.y)
Ejemplo n.º 16
0
    def testValidateCircle(self):
        c1 = ShapeFactory.build("circle", Point(0, 0), Point(2, 0), Point(0, 2))
        Circle.validateCircle(c1, "Circle unexpectedly invalid")

        self.assertRaises(ShapeException, Circle.validateCircle, "(0, 0, 2, 0, 0, 2)",
                          "String \'(0, 0, 2, 0, 0, 2)\' is not a valid circle")
        self.assertRaises(ShapeException, Circle.validateCircle, Point(1, 1), "Point is not a valid circle")
Ejemplo n.º 17
0
    def testMove(self):
        e1 = ShapeFactory.build("ellipse", Point(1, 1), Point(6, 6),
                                Point(-2, 4))

        e1.move(3, 4)
        self.assertAlmostEqual(4, e1.center.x)
        self.assertAlmostEqual(5, e1.center.y)
        self.assertAlmostEqual(9, e1.point1.x)
        self.assertAlmostEqual(10, e1.point1.y)
        self.assertAlmostEqual(1, e1.point2.x)
        self.assertAlmostEqual(8, e1.point2.y)

        e1.move(-.234, -1.987)
        self.assertAlmostEqual(3.766, e1.center.x)
        self.assertAlmostEqual(3.013, e1.center.y)
        self.assertAlmostEqual(8.766, e1.point1.x)
        self.assertAlmostEqual(8.013, e1.point1.y)
        self.assertAlmostEqual(.766, e1.point2.x)
        self.assertAlmostEqual(6.013, e1.point2.y)

        e1.move(.234, 1.987)
        self.assertAlmostEqual(4, e1.center.x)
        self.assertAlmostEqual(5, e1.center.y)
        self.assertAlmostEqual(9, e1.point1.x)
        self.assertAlmostEqual(10, e1.point1.y)
        self.assertAlmostEqual(1, e1.point2.x)
        self.assertAlmostEqual(8, e1.point2.y)
Ejemplo n.º 18
0
    def testValidateImage(self):
        i1 = ShapeFactory.build("image", Point(1, 1), Point(4, 1), Point(4, 3), Point(1, 3), "images/red_eyed_tree_frog.png")
        Image.validateImage(i1, "Image unexpectedly invalid")

        self.assertRaises(ShapeException, Image.validateImage, "(1, 1, 4, 1, 4, 3, 1, 3, images/red_eyed_tree_frog.png)",
                          "String \'(1, 1, 4, 1, 4, 3, 1, 3, images/red_eyed_tree_frog.png)\' is not a valid embedded image")
        self.assertRaises(ShapeException, Image.validateImage, Point(1, 1), "Point is not a valid embedded image")
Ejemplo n.º 19
0
def drawCircle(graphics):
    p1 = Point(0, 0)
    p2 = Point(2, 0)
    p3 = Point(0, 2)
    c1 = ShapeFactory.build("circle", p1, p2, p3)
    c1.draw(graphics)
    graphics.show()
Ejemplo n.º 20
0
    def testMove(self):
        t1 = ShapeFactory.build("triangle", Point(1, 2), Point(5, 1), Point(3, 3))

        t1.move(3, 4)
        self.assertEqual(4, t1.point1.x)
        self.assertEqual(6, t1.point1.y)
        self.assertEqual(8, t1.point2.x)
        self.assertEqual(5, t1.point2.y)
        self.assertEqual(6, t1.point3.x)
        self.assertEqual(7, t1.point3.y)

        t1.move(.4321, .7654)
        self.assertEqual(4.4321, t1.point1.x)
        self.assertEqual(6.7654, t1.point1.y)
        self.assertEqual(8.4321, t1.point2.x)
        self.assertEqual(5.7654, t1.point2.y)
        self.assertEqual(6.4321, t1.point3.x)
        self.assertEqual(7.7654, t1.point3.y)

        t1.move(-.4321, -.7654)
        self.assertEqual(4, t1.point1.x)
        self.assertEqual(6, t1.point1.y)
        self.assertEqual(8, t1.point2.x)
        self.assertEqual(5, t1.point2.y)
        self.assertEqual(6, t1.point3.x)
        self.assertEqual(7, t1.point3.y)
Ejemplo n.º 21
0
    def testValidateTriangle(self):
        t1 = ShapeFactory.build("triangle", Point(1, 1), Point(-1, -5), Point(-3, -2))
        Triangle.validateTriangle(t1, "Triangle unexpectedly invalid")

        self.assertRaises(ShapeException, Triangle.validateTriangle, "(1, 1, -1, -5, -3, -2)",
                          "String \'(1, 1, -1, -5, -3, -2)\' is not a valid triangle")
        self.assertRaises(ShapeException, Triangle.validateTriangle, Point(1, 1), "Point is not a valid triangle")
Ejemplo n.º 22
0
def drawTriangle(graphics):
    p1 = Point(1, -2)
    p2 = Point(2, -1)
    p3 = Point(0, 0)
    t1 = ShapeFactory.build("triangle", p1, p2, p3)
    t1.draw(graphics)
    graphics.show()
Ejemplo n.º 23
0
def drawEllipse(graphics):
    p1 = Point(0, 0)
    p2 = Point(3, 0)
    p3 = Point(0, 2)
    e1 = ShapeFactory.build("ellipse", p1, p2, p3)
    e1.draw(graphics)
    graphics.show()
Ejemplo n.º 24
0
 def testSaveToString(self):
     r1 = ShapeFactory.build("rectangle", Point(1, 1), Point(4, 1),
                             Point(4, 3), Point(1, 3))
     expected = r1.toString()
     testShapeIO = ShapeIO()
     actual = testShapeIO.saveShape(r1)
     self.assertEqual(expected, actual)
Ejemplo n.º 25
0
 def testInvalidConstruction(self):
     center = Point(0, 0)
     r1 = ShapeFactory.build("rectangle", Point(1, 1), Point(4, 1),
                             Point(4, 3), Point(1, 3))
     self.assertRaises(AttributeError, ShapeFactory.build, "composite",
                       center, r1.toString())
     self.assertRaises(AttributeError, ShapeFactory.build, "composite", r1,
                       r1.toString())
Ejemplo n.º 26
0
 def testLoadSource(self):
     p1 = Point(1, 3)
     p2 = Point(5, 3)
     p3 = Point(5, 1)
     p4 = Point(1, 1)
     s1 = "images/red_eyed_tree_frog.png"
     i1 = ShapeFactory.build("image", p1, p2, p3, p4, s1)
     i1._loadSource()
Ejemplo n.º 27
0
def drawSquare(graphics):
    p1 = Point(1, 3)
    p2 = Point(5, 3)
    p3 = Point(5, -1)
    p4 = Point(1, -1)
    s1 = ShapeFactory.build("square", p1, p2, p3, p4)
    s1.draw(graphics)
    graphics.show()
Ejemplo n.º 28
0
def drawRectangle(graphics):
    p1 = Point(1, 3)
    p2 = Point(5, 3)
    p3 = Point(5, 1)
    p4 = Point(1, 1)
    r1 = ShapeFactory.build("rectangle", p1, p2, p3, p4)
    r1.draw(graphics)
    graphics.show()
Ejemplo n.º 29
0
 def testSaveImage(self):
     s1 = "images/red_eyed_tree_frog.png"
     i1 = ShapeFactory.build("image", Point(1, 3), Point(5, 3), Point(5, 1),
                             Point(1, 1), s1)
     expected = i1.toString()
     testShapeIO = ShapeIO()
     actual = testShapeIO.saveShape(i1)
     self.assertEqual(expected, actual)
Ejemplo n.º 30
0
    def testValidConstruction(self):
        p1 = Point(0, 0)
        p2 = Point(3, 0)
        p3 = Point(0, 3)
        c1 = ShapeFactory.build("circle", p1, p2, p3)

        self.assertEqual(p1, c1.center)
        self.assertEqual(p2, c1.point1)
        self.assertEqual(p3, c1.point2)

        c2 = ShapeFactory.build("circle", Point(1, 1), Point(6, 6), Point(-4, 6))
        self.assertEqual(1, c2.center.x)
        self.assertEqual(1, c2.center.y)
        self.assertEqual(6, c2.point1.x)
        self.assertEqual(6, c2.point1.y)
        self.assertEqual(-4, c2.point2.x)
        self.assertEqual(6, c2.point2.y)