def feigenbaum(): y_range = input( "Zadej y rozsah oddelene - od nuly do 1. Napriklad 0.25-0.75: ").split( "-") x_range = input( "Zadej x rozsah oddelene - od nuly do 1. Napriklad 0.25-0.75: ").split( "-") y_range[0] = float(y_range[0]) y_range[1] = float(y_range[1]) x_range[0] = float(x_range[0]) x_range[1] = float(x_range[1]) points = int(200 * abs(1 / abs(y_range[0] - y_range[1]))) start = 0.5 min = 2 max = 4 y = start diference = int(200 * abs(1 / abs(y_range[0] - y_range[1]))) zofka = SvgTurtle(0, 0) for x in range(min * diference, max * diference): x = x / diference for point in range(points): y = x * y * (1.0 - y) if y_range[0] < y < y_range[1] and x_range[0] < ( x - 2) / 2 < x_range[1]: zofka.set_pos(((x - 2) * 500 - x_range[0] * 1000) * (1 / abs(x_range[0] - x_range[1])), (y * 1000 - y_range[0] * 1000) * (1 / abs(y_range[0] - y_range[1]))) zofka.self_point() zofka.save("feigenbaum.svg")
def n_square(n): zofka = SvgTurtle(500, 500) angle = (1 - 2 / n) * 180 for x in range(n): zofka.forward(50) zofka.left(180 - angle) zofka.save("{}_square.svg".format(n))
def createOutputImage(filename, size, screen): drawing = svgwrite.Drawing(filename, size=size) drawing.add(drawing.rect(fill='white', size=("50%", "50%"))) t = SvgTurtle(drawing) mainProgram(t) screen.update() drawing.save()
def write_file(draw_func, filename, size): drawing = svgwrite.Drawing(filename, size=size) drawing.add(drawing.rect(fill='white', size=("100%", "100%"))) t = SvgTurtle(drawing) Turtle._screen = t.screen Turtle._pen = t draw_func() drawing.save()
def my_picture(): zofka = SvgTurtle(500, 500) angle = 1 for x in range(10000): zofka.forward(40) zofka.left(angle) angle += 5 if x % 3 == 0: angle -= 5 zofka.save("my_awsome_svg.svg")
def write_file(draw_func, filename, size): # Uses svgwrite to export the turtle drawing to the out directory. drawing = svgwrite.Drawing("out/" + filename, size=size) drawing.add(drawing.rect(fill='white', size=("100%", "100%"))) t = SvgTurtle(drawing) Turtle._screen = t.screen Turtle._pen = t draw_func() drawing.save()
def n_square_points(n): zofka = SvgTurtle(550, 850) points = [] angle = (1 - 2 / n) * 180 for x in range(n): zofka.forward(400) zofka.left(180 - angle) points.append(list(zofka.position())) return points
def main(): drawing = svgwrite.Drawing("test_koch_knot.svg", size=("1000px", "1000px")) t = SvgTurtle(drawing) Turtle._screen = t.screen Turtle._pen = t speed(0) koch_knot(1000, 0, 5) drawing.save()
def write_file(draw_func, filename, size): """ Write turtle drawing to file """ drawing = svgwrite.Drawing(filename, size=size) drawing.add(drawing.rect(fill="white", size=("100%", "100%"))) t = SvgTurtle(drawing) Turtle._screen = t.screen Turtle._pen = t draw_func() drawing.save()
def ring_with_lines(): zofka = SvgTurtle(500, 500) for x in range(-200, 201, 25): value = sqrt(200**2 - x**2) y = x + 500 zofka.line(y, 500 + value, y, 500 - value) zofka.line(500 + value, y, 500 - value, y) zofka.save("ring_lines.svg")
def MRCM(points, transformations, iterations, name): points = [points] for x in range(iterations): new_points = [] for point in points: for tr in transformations: new_points.append(affine_transformation(point, **tr)) points = new_points set_of_points = points x = 500 y = 500 turtle = SvgTurtle(x, y) for points in set_of_points: turtle.set_pos(points[0][0] + x, points[0][1] + y) for point in points + [points[0]]: turtle.set_pos(point[0] + x, point[1] + y, write=True) turtle.save(name)
def convex_hull(points): points = sorted(points, key=itemgetter(0)) angles = [] for point in points[1:]: c = calculate_length(points[0], point) b = points[0][1] - point[1] angle = degrees(acos(b / c)) angles.append(angle) points_angles = [] for x in range(len(angles)): points_angles.append((points[x + 1], angles[x])) points_angles = [[points[0], 0]] + sorted(points_angles, key=itemgetter(1)) points = [x[0] for x in points_angles] zofka = SvgTurtle(1000, 1000) for x, y in points: zofka.point(x, y) count = 1 while count != 0: count = 0 lines = [points[0] + points[1]] for x in range(1, len(points)): try: delete = True while delete: if not is_convex(points[x] + points[x + 1], lines): print(x) points.remove(points[x]) delete = True count += 1 else: delete = False lines.append(points[x - 1] + points[x]) except IndexError: break for x in range(len(points)): zofka.line(points[x][0], points[x][1], points[x - 1][0], points[x - 1][1]) zofka.save("convex_hull.svg")
def create_maze(n): walls = [] points = [] for x in range(1, n): for y in range(1, n): points.append((x, y)) for x in range(n): walls.append(((0, x), (0, x + 1))) walls.append(((x, n), (x + 1, n))) walls.append(((n, x), (n, x + 1))) walls.append(((x, 0), (x + 1, 0))) while len(points): point = choice(points) x = point[0] y = point[1] c = 0 while point in points: wall = create_wall(x, y) if in_walls(wall, walls): continue if is_still_braid(walls, wall): walls.append(wall) points.remove(point) try: points.remove(wall[1]) except: pass c = c + 1 if c > n * 3: return False last_len = len(walls) while True: for x in range(n): for y in range(n): wall = create_wall(x, y) if in_walls(wall, walls): continue if is_still_braid(walls, wall): walls.append(wall) if last_len == len(walls): break last_len = len(walls) zofka = SvgTurtle(0, 0) for d in walls: zofka.line(d[0][0] * 100, d[0][1] * 100, d[1][0] * 100, d[1][1] * 100) zofka.save("p.svg") return True
def triangulation(n): points = generate_points(n) lines = [] for ia, a in enumerate(points): for ib, b in enumerate(points): if ib > ia: lines.append((a, b, calculate_length(a, b))) lines = sorted(lines, key=itemgetter(2)) zofka = SvgTurtle(1000, 1000) printed_lines = [] for line in lines: if not is_there_intersection( (line[0][0], line[0][1], line[1][0], line[1][1]), printed_lines): zofka.line(line[0][0], line[0][1], line[1][0], line[1][1]) printed_lines.append( (line[0][0], line[0][1], line[1][0], line[1][1])) zofka.save("triangulation.svg")
def affine_transformation(points, iteration=1, translation=False, rotation=False, scaling=False, write=False, own=False): set_of_points = [points] for x in range(iteration): transformed_points = set_of_points[-1] if translation: transformed_points = ATr(transformed_points).translation(translation[0], translation[1]) if rotation: transformed_points = ATr(transformed_points).rotation(rotation) if scaling: transformed_points = ATr(transformed_points).scaling(scaling[0], scaling[1]) if own: transformed_points = ATr(transformed_points).own(own[0], own[1], own[2], own[3], own[4], own[5]) set_of_points.append(transformed_points) if write: x = 500 y = 500 turtle = SvgTurtle(x, y) for points in set_of_points: turtle.set_pos(points[0][0] + x, points[0][1] + y) for point in points + [points[0]]: turtle.set_pos(point[0] + x, point[1] + y, write=True) turtle.save("picture.svg") else: return transformed_points
def write_file(filename): t = SvgTurtle.create(500, 500) draw_spiral(t) t.save_as(filename)
def blow(): zofka = SvgTurtle(500, 500) for x in range(12): zofka.left(360/12) n_square(zofka, 12) zofka.save("blow.svg")
def svg_star(lines, tops, radius, name): # save svg file with star turtle = SvgTurtle(400, 400) angle = 180 - (1 - (2 / (lines * tops))) * 180 distance = radius * 2 * sin(pi / (lines * tops)) # needed calculations for star for top in range(tops): for x in [-1, 1]: turtle.set_pos(400, 400) turtle.left(360 / tops * top) turtle.forward(radius) turtle.left(x * 180 - x * angle / 2) end_of_line = SvgTurtle(400, 400) end_of_line.right(360 / tops * (x - top)) for line in range(lines): turtle.left(x * angle) turtle.forward(distance) end_of_line.forward(radius / lines) turtle.connector(end_of_line) turtle.save(name)
from turtle import * import svgwrite from svg_turtle import SvgTurtle # Prepare SVG drawing = svgwrite.Drawing("primes.svg", size=("10000px", "15000px")) drawing.add(drawing.rect(fill='white', size=("100%", "100%"))) t = SvgTurtle(drawing) Turtle._screen = t.screen Turtle._pen = t # Open the file with sorted prime numbers on each line file = open("primes.txt", "r") primes = file.readlines() # Draw right(90) prev = int(primes[0]) for i in range(1, len(primes)): # for i in range(1, 5000): new = int(primes[i]) forward(new - prev) right(90) prev = new # Save drawing.save()
def star(n): zofka = SvgTurtle(500, 500) for x in range(2*n): zofka.forward(150) zofka.left(180 - 180/n) zofka.save("star.svg")
def triangles(): zofka = SvgTurtle(500, 500) length = 30 zofka.left(90) for x in range(15): positions = [] for x in range(3): zofka.forward(length, write=False) positions.append(zofka.position()) zofka.left(180) zofka.forward(length, write=False) zofka.right(60) positions.append(positions[0]) for x in range(3): zofka.line(positions[x][0], positions[x][1], positions[x+1][0], positions[x+1][1]) length += 30 zofka.save("triangles.svg")
def squares(): zofka = SvgTurtle(500, 500) length = 400 cor_length = sqrt(0.75**2 + 0.25**2) cor_angle = degrees(asin(0.25/cor_length)) for y in range(50): for x in range(4): zofka.forward(length) zofka.right(90) zofka.forward(length*0.25) zofka.right(cor_angle) length = length * cor_length zofka.save("squares_in_squares.svg")
def chaos_game(n, iterations, r, name, random=False): zofka = SvgTurtle(500, 800) points = n_square_points(n) if random: numbers = [x-100 for x in range(200)] for point in points: point[0] += choice(numbers) point[1] += choice(numbers) for point in range(len(points)): zofka.line(points[point][0], points[point][1], points[point-1][0], points[point-1][1]) for x in range(iterations): point = choice(points) t = SvgTurtle(point[0], point[1]) position = zofka.position() if position[0] > point[0]: posx = point[0] + abs(position[0] - point[0]) * r else: posx = position[0] + abs(position[0] - point[0]) * (1-r) if position[1] > point[1]: posy = point[1] + abs(position[1] - point[1]) * r else: posy = position[1] + abs(position[1] - point[1]) * (1-r) zofka.set_pos(posx, posy) zofka.self_point() zofka.save(name+".svg")
def pentagram_relative(): zofka = SvgTurtle(500, 500) for x in range(5): zofka.forward(200) zofka.left(180 - 36) zofka.right(36) length = 100/sin(radians(54)) angle = (1 - 2 / 5) * 180 for x in range(5): zofka.forward(length) zofka.left(180 - angle) zofka.save("relative_pentagram.svg")
sys.path.insert( 0, '/home/dargen3/python/matematika_a_programovani/1-lesson/graphic/') from svg_turtle import SvgTurtle def sierpinski_penta(depth, base, zofka): if depth == 0: for i in range(5): zofka.forward(base) zofka.left(72) else: for x in range(5): sierpinski_penta(depth - 1, base, zofka) zofka.forward(base * 2.62**depth, write=False) zofka.left(72) for x in range(2): zofka.forward(base * 2.62**(depth - 1)) zofka.left(72) zofka.right(108) sierpinski_penta(depth - 1, base, zofka) zofka.right(144) for x in range(2): zofka.forward(base * 2.62**(depth - 1)) zofka.right(72) zofka.left(180 + 72) zofka.save("sierpinski_penta.svg") zofka = SvgTurtle(1000, 1000) sierpinski_penta(3, 10, zofka)
def pentagram_absolute(): zofka = SvgTurtle(500, 500) angle = (1 - 2 / 5) * 180 positions = [] for x in range(5): zofka.forward(200) zofka.left(180-108) positions.append(zofka.position()) for x in range(5): zofka.line(positions[x][0], positions[x][1], positions[x-1][0], positions[x-1][1]) for x in range(5): zofka.line(positions[x][0], positions[x][1], positions[x - 2][0], positions[x - 2][1]) zofka.save("absolute_pentragram.svg")