def test_decompose_pentagon(self):
     # Make sure decompose returns correct thing for a pentagon.
     result = decompose_to_triangles(self.pentagon)
     benchmark = array([[[0, 0], [2, 0], [2, 1]],
                        [[0, 0], [2, 1], [1, 2]],
                        [[0, 0], [1, 2], [0, 2]]], dtype='float')
     assert_equal(result, benchmark)
Exemplo n.º 2
0
def find_area_of_polygon(vertices):
    """
    Return area of polygon.

    Parameters
    ----------
    vertices : N x 2 np.ndarray

    Returns
    -------
    area : real number
    """
    decomposition = decompose_to_triangles(vertices)
    area = 0.0
    for triangle in decomposition:
        area += find_area_of_triangle(triangle)
    return area
def center_of_mass(vertices):
    """
    Return center of mass of polygon spanned by vertex list.

    Parameters
    ----------
    vertices : N x 2 np.ndarray

    Returns
    -------
    center : 1 x 2 np.ndarray
    """
    center = np.array([0, 0], dtype='float')
    area = find_area_of_polygon(vertices)
    decomposition = decompose_to_triangles(vertices)

    for triangle in decomposition:
        area_of_triangle = find_area_of_polygon(triangle)
        center_of_triangle = find_center_of_mass_triangle(triangle)
        center += area_of_triangle*center_of_triangle
    
    center = (1/area)*center

    return center
 def test_decompose_trapezoid(self):
     # Make sure decompose returns correct thing for a trapezoid.
     result = decompose_to_triangles(self.trapezoid)
     benchmark = array([[[0, 0], [2, 0], [1, 1]],
                        [[0, 0], [1, 1], [0, 1]]], dtype='float')
     assert_equal(result, benchmark)
 def test_decompose_triangle2(self):
     # Make sure decompose returns same thing if given another triangle.
     result = decompose_to_triangles(self.triangle2)
     benchmark = array([[[0, 0], [2, 0], [0, 1]]], dtype='float')
     assert_equal(result, benchmark)