示例#1
0
    def project(self, perspective=False):
        p = Polygon()

        for line in self.lines:
            line2d = line.project(perspective)
            p.add_line(line2d)
        return p
class PrimitiveParser(object):
    def __init__(self):
        self.PRIMITIVE_MAP = {
            "Line" : self.make_line,
            "moveto" : self.move_to,
            "lineto" : self.line_to,
            "stroke" : self.stroke,
        }

        self.polygon = Polygon()
        self.pen = None

    def parse(self, string):
        parts = string.split(" ")
        definition = parts[:-1]
        identifier = parts[-1].strip()
        if identifier in self.PRIMITIVE_MAP:
            return self.PRIMITIVE_MAP[identifier](definition)
        else:
            raise RuntimeError("NotImplemented Error: Primitive [%s]" % identifier)

    def make_line(self, parts):
        parts = [int(p) for p in parts[:4]]
        p1 = Point([parts[0], parts[1]])
        p2 = Point([parts[2], parts[3]])
        return Line(p1, p2)

    def move_to(self, parts):
        x, y = [int(c) for c in parts[0:2]]
        self.pen = Point([x, y])
        return None

    def line_to(self, parts):
        assert(self.pen is not None)
        last_point = self.pen
        self.move_to(parts)
        current_point = self.pen
        line = Line(last_point, current_point)
        self.polygon.add_line(line)
        return None

    def stroke(self, parts=None):
        polygon = self.polygon
        self.polygon = Polygon()
        return polygon