Beispiel #1
0
class TestTriangle:
    def setup_class(self):
        self.triangle = Triangle(7, 10, 5)

    def test_valid(self):
        with pytest.raises(ValueError):
            triangle = Triangle(1, 2, 3)
            assert triangle.angles == 3

    def test_name(self):
        triangle = Triangle(1, 1, 1)
        assert triangle.name == 'Треугольник_3'

    def test_area(self):
        assert self.triangle.area == sqrt(11 * 4 * 1 * 6)

    def test_angles(self):
        assert self.triangle.angles == 3

    def test_perimeter(self):
        assert self.triangle.perimeter == 7 + 10 + 5

    def test_add_square(self):
        triangle = Triangle(1, 1, 1)
        print(triangle.area)
        print(self.triangle.area)
        assert self.triangle.add_square(triangle) == sqrt(
            11 * 4 * 1 * 6) + sqrt(1.5 * 0.5 * 0.5 * 0.5)

    def test_add_square_neg(self):
        not_shape = 3.14
        with pytest.raises(NotShapeException):
            assert self.triangle.add_square(not_shape) == 20
Beispiel #2
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")
Beispiel #3
0
    def testValidConstruction(self):
        p1 = Point(1, 2)
        p2 = Point(1, 1)
        p3 = Point(2, 1)
        t1 = Triangle(p1, p2, p3)

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

        l1 = Line(4.33, 8, -4, 3.67)
        l2 = Line(-4, 3.67, 2.19, -5)
        l3 = Line(2.19, -5, 4.33, 8)
        t2 = Triangle(l1, l2, l3)

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

        t3 = Triangle(1, 2, 5, 1, 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)
Beispiel #4
0
 def setUp(self):
     """
     Create a few test objects.
     """
     self.sidea7sideb8sidec9 = Triangle(7, 8, 9)
     self.sidea3sideb4sidec5 = Triangle(3, 4, 5)
     self.sidea9sideb9sidec9 = Triangle(9, 9, 9)
     self.sidea1sideb100sidec3 = Triangle(1, 100, 3)
     self.sidea0sideb10sidec3 = Triangle(0, 10, 3)
Beispiel #5
0
def test_triangle():
    """
    Defines tests on some specific triangle objects.
    """
    h2b3sA4sB5 = Triangle(2,3,4,5)
    h9b10sA12sB9 = Triangle(9,10,12,9)
    assert h2b3sA4sB5.area() == 3
    assert h2b3sA4sB5.perimeter() == 12
    assert h2b3sA4sB5.righttriangle() == True
    assert h9b10sA12sB9.area() == 45
    assert h9b10sA12sB9.perimeter() == 31
    assert h9b10sA12sB9.righttriangle() == False
Beispiel #6
0
    def testComputeArea(self):
        t1 = Triangle(1, 1, 2, 1, 1, 2)
        self.assertAlmostEqual(.5, t1.computeArea(), places=4)

        t2 = Triangle(1, 2, 5, 1, 3, 3)
        self.assertAlmostEqual(3, t2.computeArea(), places=4)

        t3 = Triangle(-3, 3, 2, 4, 3, -1)
        self.assertAlmostEqual(13, t3.computeArea(), places=4)
Beispiel #7
0
 def __init__(self, menu_items, font_name, font_size=None, x=0, y=0):
     self.x = x
     self.y = y
     self.items = menu_items
     self.labels = [None] * len(self.items)
     for index, item in enumerate(self.items):
         label = Label(item.name, font_name=font_name, font_size=font_size)
         self.labels[index] = label
         # if label not in self.labels:
         #     self.labels.append(label)
     self.set_label_coordinates()
     self.pointer = Triangle(0, 0, 10, 10)
     self.update_pointer()
Beispiel #8
0
    def test_valid(self):
        """
        Confirm or deny if the triangle is a valid configuration.
        """
        with self.assertRaises(ValueError):
            Triangle(5, 1, 1)

        with self.assertRaises(ValueError):
            Triangle(5, 1, 4)

        self.assertTrue(self.equilateral.is_valid())
        self.assertTrue(self.scalene.is_valid())
        self.assertTrue(self.isosceles.is_valid())
        self.assertTrue(self.right.is_valid())
Beispiel #9
0
    def testMove(self):
        t1 = Triangle(1, 2, 5, 1, 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)
Beispiel #10
0
 def setUp(self):
     """
     Create a few test objects.
     """
     self.equilateral = Triangle(7, 7, 7)
     self.isosceles = Triangle(4, 6, 6)
     self.right = Triangle(5, 4, 3)
     self.scalene = Triangle(5, 4, 6)
Beispiel #11
0
def get_triangles(filename):
    """
    Read in from the JSON file supplied and create
    a list of rectangles based on the input data.
    """
    with open(filename, "r") as handle:
        try:
            data = json.load(handle)
        except json.decoder.JSONDecodeError as error:
            print(error)
            raise

    # Manually iterate over the JSON dictionaries in the list
    # of rectangles. Create a new Rectangle object for each one
    # and add it to the list.
    triangle_objects = []
    for tria in data["triangle_list"]:
        lengthA = tria["lengthA"]
        lengthB = tria["lengthB"]
        lengthC = tria["lengthC"]
        height = tria["height"]
        new_triangle = Triangle(lengthA, lengthB, lengthC, height)
        triangle_objects.append(new_triangle)

    # Return the list of Rectangle objects
    return triangle_objects
Beispiel #12
0
 def _init(self):
     self.n = len(self.sides)
     assert (self.n < 4), 'No support for sides > 3'
     if self.n == 3:
         self.shape = Triangle(*self.sides)
     elif self.n == 2:
         self.shape = Rectangle(*self.sides)
Beispiel #13
0
def get_triangles(filename):
    """
    Read in from the JSON file supplied and create
    a list of triangles based on the input data.
    """
    with open(filename, "r") as handle:
        try:
            data = json.load(handle)
        except json.decoder.JSONDecodeError as error:
            print(error)
            raise

    # Manually iterate over the JSON dictionaries in the list
    # of triangles. Create a new triangle object for each one
    # and add it to the list.
    triangle_objects = []
    for tri in data["triangle_list"]:
        a = tri["a"]
        b = tri["b"]
        c = tri["c"]
        new_triangle = Triangle(a, b, c)
        triangle_objects.append(new_triangle)

    # Return the list of triangle objects
    return triangle_objects
Beispiel #14
0
def get_triangles(filename):
    """
    Read in from the JSON file supplied and create
    a list of triangles based on the input data.
    """
    with open(filename, 'r') as handle:
        try:
            data = json.load(handle)
        except json.decoder.JSONDecodeError as error:
            print(error)
            raise

    # Manually iterate over the JSON dictionaries in the list
    # of rectangles. Create a new Rectangle object for each one
    # and add it to the list.
    triangle_objects = []
    for triangle in data['triangle_list']:
        side1 = triangle['side1']
        side2 = triangle['side2']
        side3 = triangle['side3']
        new_triangle = Triangle(side1, side2, side3)
        triangle_objects.append(new_triangle)

    # Return the list of Rectangle objects
    return triangle_objects
Beispiel #15
0
    def testValidateTriangle(self):
        t1 = Triangle(1, 1, -1, -5, -3, -2)
        Validator.validateTriangle(t1, "Triangle unexpectedly invalid")

        self.assertRaises(
            ShapeException, Validator.validateLine, "(1, 1, -1, -5, -3, -2)",
            "String \'(1, 1, -1, -5, -3, -2)\' is not a valid triangle")
        self.assertRaises(ShapeException, Validator.validateLine, Point(1, 1),
                          "Point is not a valid triangle")
Beispiel #16
0
 def get_quadrangle_volume(vertices: Mat) -> float:
     """
     ================================================================================================================
     Method :
     ================================================================================================================
     Computes the surface of a quadrangle.
     ================================================================================================================
     Parameters :
     ================================================================================================================
     - vertices : the matrix containing the coordinates of the vertices of the quadrangle as vectors.
     ================================================================================================================
     Returns :
     ================================================================================================================
     - quadrangle_volume : the surface of the quadrangle.
     """
     t0 = vertices[0, 1, 2]
     t0 = vertices[0, 3, 2]
     v0 = Triangle.get_triangle_volume(t0)
     v1 = Triangle.get_triangle_volume(t1)
     quadrangle_volume = v0 + v1
     return quadrangle_volume
Beispiel #17
0
 def setUp(self):
     """
     Create a few test objects.
     """
     self.side8side10side15 = Triangle(8, 10, 15)
     self.side10side12side15 = Triangle(10, 12, 15)
     self.side5side5side5 = Triangle(5, 5, 5)
 def setUp(self):
     """
     Create a few test objects.
     """
     self.isosceles_triangle = Triangle(5, 5, 5, 7.07)
     self.equilateral_triangle = Triangle(6.93, 8, 8, 8)
     self.triangle = Triangle(24, 50, 30, 40)
Beispiel #19
0
 def __init__(self, vertices: Mat, polynomial_order: int):
     """
     ================================================================================================================
     Class :
     ================================================================================================================
     The Polygon class inherits from the Domain class to specifiy its attributes when the domain is a polygon.
     ================================================================================================================
     Parameters :
     ================================================================================================================
     - vertices : the matrix containing the vertices coordinates as vectors.
     - polynomial_order : the polynomial order of integration over the polygon.
     ================================================================================================================
     Attributes :
     ================================================================================================================
     - centroid : the vector with values containing the center of mass of the polygon.
     - volume : the volume of the polygon.
     - diameter : the diameter of the polygon.
     - quadrature_points : the matrix containing the quadrature points of the polygon.
     - quadrature_weights : the vector containing the quadrature weights of the polygon.
     """
     if not vertices.shape[0] > 4 and not vertices.shape[1] == 2:
         raise TypeError(
             "The domain dimension do not match that of a polygon")
     else:
         barycenter = Domain.get_domain_barycenter_vector(vertices)
         volume = 0.0
         # volume = Polygon.get_polygon_volume(vertices)
         simplicial_sub_domains = Polygon.get_polygon_simplicial_partition(
             vertices, barycenter)
         # sub_domains_centroids = []
         quadrature_points, quadrature_weights = [], []
         for simplicial_sub_domain in simplicial_sub_domains:
             # simplex_centroid = Domain.get_domain_barycenter_vector(simplicial_sub_domain)
             simplex_volume = Triangle.get_triangle_volume(
                 simplicial_sub_domain)
             simplex_quadrature_points, simplex_quadrature_weights = DunavantRule.get_triangle_quadrature(
                 simplicial_sub_domain, simplex_volume, polynomial_order)
             volume += simplex_volume
             quadrature_points.append(simplex_quadrature_points)
             quadrature_weights.append(simplex_quadrature_weights)
             # sub_domains_centroids.append(simplex_centroid)
         # centroid = np.zeros(2,)
         # for sub_domain_centroid in sub_domains_centroids:
         #     centroid += sub_domain_centroid
         # number_of_vertices = vertices.shape[0]
         # centroid = 1.0 / number_of_vertices * centroid
         centroid = Polygon.get_polygon_centroid(vertices, volume)
         diameter = Polygon.get_polygon_diameter(vertices)
         quadrature_points = np.concatenate(quadrature_points, axis=0)
         quadrature_weights = np.concatenate(quadrature_weights, axis=0)
         super().__init__(centroid, volume, diameter, quadrature_points,
                          quadrature_weights)
Beispiel #20
0
class TriangleTest(TestCase):
    """
    Defines a test case for the triangle class.
    """
    def setUp(self):
        """
        Create a few test objects.
        """
        self.length5 = Triangle(5)
        self.length8 = Triangle(8)

    def test_area(self):
        """
        Compare the test triangle area computations to the actual values.
        """
        self.assertEqual(self.length5.area(), 10.83)
        self.assertEqual(self.length8.area(), 27.71)

    def test_perimeter(self):
        """
        Compare the test triangle perimeter computations to the actual values.
        """
        self.assertEqual(self.length5.perimeter(), 15)
        self.assertEqual(self.length8.perimeter(), 24)
def main():
    t1=Triangle() 
    t1.create(3,4,5) # can call using object reference
    test_shape(t1)

    t2=Triangle() #change this
    Triangle.create(t2,3,4,12) # can also call using class reference
    test_shape(t2)

    c=Circle.create(7) # can be called only using class refernce
    test_shape(c)

    c2=Circle.create(-3)
    test_shape(c2)
Beispiel #22
0
class TriangleTest(TestCase):
    """
    Defines a test case for the Triangle class.
    """

    def setUp(self):
        """
        Create a few test objects.
        """
        self.side8side10side15 = Triangle(8, 10, 15)
        self.side10side12side15 = Triangle(10, 12, 15)
        self.side5side5side5 = Triangle(5, 5, 5)

    def test_area(self):
        """
        Compare the test triangle area computations to the actual values.
        """
        self.assertEqual(round(self.side8side10side15.area()), 37) #36.9789)
        self.assertEqual(round(self.side10side12side15.area()), 60) #59.8117)
        self.assertEqual(round(self.side5side5side5.area()), 11) #10.8253)

    def test_perimeter(self):
        """
        Compare the test triangle perimeter computations to the actual values.
        """
        self.assertEqual(self.side8side10side15.perimeter(), 33)
        self.assertEqual(self.side10side12side15.perimeter(), 37)
        self.assertEqual(self.side5side5side5.perimeter(), 15)

    def test_is_equilateral(self):
        """
        Confirm or deny if the triangle is an equilateral.
        """
        self.assertFalse(self.side8side10side15.is_equilateral())
        self.assertFalse(self.side10side12side15.is_equilateral())
        self.assertTrue(self.side5side5side5.is_equilateral())
def get_triangles(filename):
    """
    Read in from the YAML file supplied and create
    a list of triangles based on the input data.
    """
    try:
        handle = open(filename, 'r')
        data = yaml.load(handle)
    except yaml.YAMLError as error:
        print(error)
    finally:
        handle.close()

    # Use a list comprehension to create a new triangle
    # object for each radius integer found in the triangle_list.
    triangle_objects = [Triangle(length) for length in data['triangle_list']]

    # Return the list of triangle objects
    return triangle_objects
def get_triangles(filename):
    """
        Read in from the JSON file supplied and create
        a list of triangles based on the input data.
        """
    with open(filename, "r") as handle:
        try:
            data = json.load(handle)
        except json.decoder.JSONDecodeError as error:
            print(error)
            raise

    # Manually iterate over the JSON dictionaries in the list
    # of triangles. Create a new Triangle object for each one
    # and add it to the list.
    triangle_objects = [
        Triangle(tr["side_a"], tr["side_b"], tr["side_c"]) for
        tr in data["triangle_list"]]

    # Return the list of Triangle objects
    return triangle_objects
Beispiel #25
0
def paint(canvas):
    text = read_file()
    for line in text:
        if "rcl" in line or "va" in line:
            prop = line.split(", ")
            Oval(canvas, prop[1], prop[2], prop[3], prop[4], prop[5], prop[6],
                 prop[7])
        elif "ua" in line or "rect" in line:
            prop = line.split(", ")
            Rectangle(canvas, prop[1], prop[2], prop[3], prop[4], prop[5],
                      prop[6], prop[7])
        elif "tri" in line:
            prop = line.split(", ")
            Triangle(canvas, prop[1], prop[2], prop[3], prop[4], prop[5],
                     prop[6], prop[7], prop[8], prop[9])
        elif "in" in line:
            prop = line.split(", ")
            Line(canvas, prop[1], prop[2], prop[3], prop[4], prop[5], prop[6])
        elif "arc" in line:
            prop = line.split(", ")
            Arc(canvas, prop[1], prop[2], prop[3], prop[4], prop[5], prop[6],
                prop[7], prop[8], prop[9])
Beispiel #26
0
def get_triangles(filename):
    """
    Read in from the JSON file supplied and create
    a list of triangles based on the input data.
    """
    triangle_objects = []
    with open(filename, 'r') as handle:
        try:
            data = json.load(handle)
        except json.decoder.JSONDecodeError as error:
            print(error)
            raise
    # Manually iterate over the JSON dictionaries in the list
    # of triangles. Create a new Triangle object for each one
    # and add it to the list.
    for item in data['triangles']:
        height = item['height']
        base = item['base']
        sideA = item['sideA']
        sideB = item['sideB']
        new_triangle = Triangle(height, base, sideA, sideB)
        triangle_objects.append(new_triangle)

    return triangle_objects
Beispiel #27
0
 def generator(sides: typing.Tuple[int, ...] = ()):
     assert len(sides) == 3
     side_set = [Side(s) for s in sides]
     side_set = (*side_set, )
     return Triangle(side_set)
Beispiel #28
0
 def __init__(self, vertices: Mat, connectivity_matrix: Mat,
              quadrature_order: int):
     """
     ================================================================================================================
     Class :
     ================================================================================================================
     
     ================================================================================================================
     Parameters :
     ================================================================================================================
     
     ================================================================================================================
     Attributes :
     ================================================================================================================
     
     """
     # --------------------------------------------------------------------------------------------------------------
     # Building the cell
     # --------------------------------------------------------------------------------------------------------------
     cell_shape = Cell.get_cell_shape(vertices)
     if cell_shape == "SEGMENT":
         c = Segment(vertices, quadrature_order)
         centroid = c.centroid
         volume = c.volume
         diameter = c.diameter
         quadrature_points = c.quadrature_points
         quadrature_weights = c.quadrature_weights
         del c
     if cell_shape == "TRIANGLE":
         c = Triangle(vertices, quadrature_order)
         centroid = c.centroid
         volume = c.volume
         diameter = c.diameter
         quadrature_points = c.quadrature_points
         quadrature_weights = c.quadrature_weights
         del c
     if cell_shape == "POLYGON":
         c = Polygon(vertices, quadrature_order)
         centroid = c.centroid
         volume = c.volume
         diameter = c.diameter
         quadrature_points = c.quadrature_points
         quadrature_weights = c.quadrature_weights
         del c
     if cell_shape == "TETRAHEDRON":
         c = Tetrahedron(vertices, quadrature_order)
         centroid = c.centroid
         volume = c.volume
         diameter = c.diameter
         quadrature_points = c.quadrature_points
         quadrature_weights = c.quadrature_weights
         del c
     if cell_shape == "POLYHEDRON":
         c = Polyhedron(vertices, connectivity_matrix, quadrature_order)
         centroid = c.centroid
         volume = c.volume
         diameter = c.diameter
         quadrature_points = c.quadrature_points
         quadrature_weights = c.quadrature_weights
         del c
     super().__init__(centroid, volume, diameter, quadrature_points,
                      quadrature_weights)
Beispiel #29
0
def main():
    t1=Triangle()
    print(id(t1))
    is_of_type(t1, Triangle)
    is_of_type(t1, str)
    is_of_type('hello',Triangle)
Beispiel #30
0
def test_squares_equals_area_after_add_area():
    square_1 = Square(4, 2)
    triangle_2 = Triangle(6, 2)
    assert square_1.add_area(triangle_2) == triangle_2.add_area(square_1)