コード例 #1
0
class DecoratedPolygon:
    def __init__(self, vlist):
        corrected_vlist = self.__correct(vlist)
        self.polygon = Polygon(corrected_vlist)

    @property
    def vlist(self):
        return self.polygon.vlist

    def area(self):
        return self.polygon.area()

    def set_areaAlg(self, alg):
        self.polygon.set_areaAlg(alg)

    def __correct(self, vlist):
        new_vlist = vlist
        if new_vlist[-1] != new_vlist[0]:
            new_vlist.append(new_vlist[0])
        return new_vlist
コード例 #2
0
        vertices = self._polygon.vlist
        area = 0.0
        for i in range(0, len(vertices) - 3):
            area += self.__calc_triang_area(vertices[i], vertices[i + 1],
                                            vertices[i + 2])
        return fabs(area)

    def __calc_triang_area(self, v1, v2, v3):
        l1 = (v2[0] - v1[0], v2[1] - v1[1])
        l2 = (v3[0] - v1[0], v3[1] - v1[1])
        cross = (0, 0, l1[0] * l2[1] - l1[1] * l2[0])
        area = sqrt(cross[0]**2 + cross[1]**2 + cross[2]**2) / 2
        return area


if __name__ == "__main__":
    # Making us some pretty triangle
    poly = Polygon([(0, 0), (0.866, 0.5), (1, 0)])

    # Wanna know its area. Bridge will call specific area function
    poly.set_areaAlg(OnlyTriangleAreaAlg(poly))
    print(poly.area())

    # Now it's some messed up non-convex polygon. Ouch
    poly = Polygon([(0, 0), (1, 0), (2, 1), (3, 4), (3, 5), (2, 2), (1, 2),
                    (0, 0)])

    # Need new algorithm for that
    poly.set_areaAlg(GeneralAreaAlg(poly))
    print(poly.area())