Esempio n. 1
0
def copy(shape,
         copies,
         transform_order='tsr',
         translate=Point.ZERO,
         rotate=0,
         scale=Point.ZERO):
    """Create multiple copies of a shape."""
    if shape is None: return None
    if isinstance(shape, Path):
        shape = shape.asGeometry()
    g = Geometry()
    tx = ty = r = 0.0
    sx = sy = 1.0
    for i in xrange(copies):
        t = Transform()
        # Each letter of the order describes an operation.
        for op in transform_order:
            if op == 't':
                t.translate(tx, ty)
            elif op == 'r':
                t.rotate(r)
            elif op == 's':
                t.scale(sx, sy)
        g.extend(t.map(shape))
        tx += translate.x
        ty += translate.y
        r += rotate
        sx += scale.x / 100.0
        sy += scale.y / 100.0
    return g
Esempio n. 2
0
def fit(shape, position, width, height, keep_proportions):
    """Fit a shape within bounds."""
    if shape is None: return None

    px, py, pw, ph = list(shape.bounds)

    # Make sure pw and ph aren't infinitely small numbers.
    # This will lead to incorrect transformations with for examples lines.
    if 0 < pw <= 0.000000000001: pw = 0
    if 0 < ph <= 0.000000000001: ph = 0

    t = Transform()
    t.translate(position.x, position.y)
    if keep_proportions:
        # Don't scale widths or heights that are equal to zero.
        w = pw and width / pw or float("inf")
        h = ph and height / ph or float("inf")
        w = h = min(w, h)
    else:
        # Don't scale widths or heights that are equal to zero.
        w = pw and width / pw or 1
        h = ph and height / ph or 1
    t.scale(w, h)
    t.translate(-pw / 2 - px, -ph / 2 - py)

    return t.map(shape)
Esempio n. 3
0
def fit(shape, position, width, height, keep_proportions):
    """Fit a shape within bounds."""
    if shape is None: return None

    px, py, pw, ph = list(shape.bounds)

    # Make sure pw and ph aren't infinitely small numbers.
    # This will lead to incorrect transformations with for examples lines.
    if 0 < pw <= 0.000000000001: pw = 0
    if 0 < ph <= 0.000000000001: ph = 0

    t = Transform()
    t.translate(position.x, position.y)
    if keep_proportions:
        # Don't scale widths or heights that are equal to zero.
        w = pw and width / pw or float("inf")
        h = ph and height / ph or float("inf")
        w = h = min(w, h)
    else:
        # Don't scale widths or heights that are equal to zero.
        w = pw and width / pw or 1
        h = ph and height / ph or 1
    t.scale(w, h)
    t.translate(-pw / 2 - px, -ph / 2 - py)

    return t.map(shape)
Esempio n. 4
0
def copy(shape, copies, transform_order='tsr', translate=Point.ZERO, rotate=0, scale=Point.ZERO):
    """Create multiple copies of a shape."""
    if shape is None: return None
    if isinstance(shape, Path):
        shape = shape.asGeometry()
    g = Geometry()
    tx = ty = r = 0.0
    sx = sy = 1.0
    for i in xrange(copies):
        t = Transform()
        # Each letter of the order describes an operation.
        for op in transform_order:
            if op == 't':
                t.translate(tx, ty)
            elif op == 'r':
                t.rotate(r)
            elif op == 's':
                t.scale(sx, sy)
        g.extend(t.map(shape))
        tx += translate.x
        ty += translate.y
        r += rotate
        sx += scale.x / 100.0
        sy += scale.y / 100.0
    return g
Esempio n. 5
0
def scale(shape, scale, origin=Point.ZERO):
    """Scale the given shape."""
    if shape is None: return None
    t = Transform()
    t.translate(origin)
    t.scale(scale.x / 100.0, scale.y / 100.0)
    t.translate(Point(-origin.x, -origin.y))
    return t.map(shape)
Esempio n. 6
0
def scale(shape, scale, origin=Point.ZERO):
    """Scale the given shape."""
    if shape is None: return None
    t = Transform()
    t.translate(origin)
    t.scale(scale.x / 100.0, scale.y / 100.0)
    t.translate(Point(-origin.x, -origin.y))
    return t.map(shape)
Esempio n. 7
0
def lpath(x, y, angle, angleScale, length, thicknessScale, lengthScale,
          full_rule):

    p = Path()
    p.rect(0, -length / 2, 2, length)
    segment = p.asGeometry()

    # Now run the simulation
    g = Geometry()
    stack = []
    angleStack = []
    t = Transform()
    t.translate(x, y)
    for letter in full_rule:
        if re.search('[a-zA-Z]', letter):  # Move forward and draw
            newShape = t.map(segment)
            g.extend(newShape)
            t.translate(0, -length)
        elif letter == '+':  # Rotate right
            t.rotate(angle)
        elif letter == '-':  # Rotate left
            t.rotate(-angle)
        elif letter == '[':  # Push state (start branch)
            stack.append(Transform(t))
            angleStack.append(angle)
        elif letter == ']':  # Pop state (end branch)
            t = stack.pop()
            angle = angleStack.pop()
        elif letter == '"':  # Multiply length
            t.scale(1.0, lengthScale / 100.0)
        elif letter == '!':  # Multiply thickness
            t.scale(thicknessScale / 100.0, 1.0)
        elif letter == ';':  # Multiply angle
            angle *= angleScale / 100.0
        elif letter == '_':  # Divide length
            t.scale(1.0, 1.0 / (lengthScale / 100.0))
        elif letter == '?':  # Divide thickness
            t.scale(1.0 / (thicknessScale / 100.0), 1.0)
        elif letter == '@':  # Divide angle
            angle /= angleScale / 100.0
    return g
Esempio n. 8
0
def cook(generations,x,y,angle,angleScale,length,thicknessScale,lengthScale,premise,rule1,rule2,rule3):
    #segment = self.segment
    #if segment is None:
    p = Path()
    p.rect(0, -length/2, 2, length)
    segment = p.asGeometry()
    # Parse all rules
    ruleArgs = [rule1,rule2,rule3]
    rules = {}
    #rulenum = 1
    #while hasattr(cook,"rule%i" % rulenum):
    for full_rule in ruleArgs:
        #full_rule = getattr("rule%i" % rulenum)
        if len(full_rule) > 0:
            if len(full_rule) < 3 or full_rule[1] != '=':
                raise ValueError("Rule %s should be in the format A=FFF" % full_rule)
            rule_key = full_rule[0]
            rule_value = full_rule[2:]
            rules[rule_key] = rule_value
        #rulenum += 1
    # Expand the rules up to the number of generations
    full_rule = premise
    for gen in xrange(int(round(generations))):
        tmp_rule = ""
        for letter in full_rule:
            if letter in rules:
                tmp_rule += rules[letter]
            else:
                tmp_rule += letter
        full_rule = tmp_rule
    # Now run the simulation
    g = Geometry()
    stack = []
    angleStack = []
    t = Transform()
    t.translate(x, y)
    angle = angle
    for letter in full_rule:
        if re.search('[a-zA-Z]',letter): # Move forward and draw
            newShape = t.map(segment)
            g.extend(newShape)
            t.translate(0, -length)
        elif letter == '+': # Rotate right
            t.rotate(angle)
        elif letter == '-': # Rotate left
            t.rotate(-angle)
        elif letter == '[': # Push state (start branch)
            stack.append(Transform(t))
            angleStack.append(angle)
        elif letter == ']': # Pop state (end branch)
            t = stack.pop()
            angle = angleStack.pop()
        elif letter == '"': # Multiply length
            t.scale(1.0, lengthScale/100.0)
        elif letter == '!': # Multiply thickness
            t.scale(thicknessScale/100.0, 1.0)
        elif letter == ';': # Multiply angle
            angle *= angleScale/100.0
        elif letter == '_': # Divide length
            t.scale(1.0, 1.0/(lengthScale/100.0))
        elif letter == '?': # Divide thickness
            t.scale(1.0/(thicknessScale/100.0), 1.0)
        elif letter == '@': # Divide angle
            angle /= angleScale/100.0
    return g
Esempio n. 9
0
def l_system(shape, position, generations, length, length_scale, angle, angle_scale, thickness_scale, premise, *rules):
    if shape is None:
        p = Path()
        p.rect(0, -length/2, 2, length)
        shape = p.asGeometry()
    # Parse all rules
    rule_map = {}
    for rule_index, full_rule in enumerate(rules):
        if len(full_rule) > 0:
            if len(full_rule) < 3 or full_rule[1] != '=':
                raise ValueError("Rule %s should be in the format A=FFF" % (rule_index + 1))
            rule_key = full_rule[0]
            rule_value = full_rule[2:]
            rule_map[rule_key] = rule_value
    # Expand the rules up to the number of generations
    full_rule = premise
    for gen in xrange(int(round(generations))):
        tmp_rule = ""
        for letter in full_rule:
            if letter in rule_map:
                tmp_rule += rule_map[letter]
            else:
                tmp_rule += letter
        full_rule = tmp_rule
    # Now run the simulation
    g = Geometry()
    stack = []
    angleStack = []
    t = Transform()
    t.translate(position.x, position.y)
    angle = angle
    for letter in full_rule:
        if letter == 'F': # Move forward and draw
            transformed_shape = t.map(shape)
            if isinstance(transformed_shape, Geometry):
                g.extend(transformed_shape)
            elif isinstance(transformed_shape, Path):
                g.add(transformed_shape)
            t.translate(0, -length)
        elif letter == '+': # Rotate right
            t.rotate(angle)
        elif letter == '-': # Rotate left
            t.rotate(-angle)
        elif letter == '[': # Push state (start branch)
            stack.append(Transform(t))
            angleStack.append(angle)
        elif letter == ']': # Pop state (end branch)
            t = stack.pop()
            angle = angleStack.pop()
        elif letter == '"': # Multiply length
            t.scale(1.0, length_scale / 100.0)
        elif letter == '!': # Multiply thickness
            t.scale(thickness_scale / 100.0, 1.0)
        elif letter == ';': # Multiply angle
            angle *= angle_scale / 100.0
        elif letter == '_': # Divide length
            t.scale(1.0, 1.0/(length_scale / 100.0))
        elif letter == '?': # Divide thickness
            t.scale(1.0/(thickness_scale / 100.0), 1.0)
        elif letter == '@': # Divide angle
            angle /= angle_scale / 100.0
    return g