Exemplo n.º 1
0
def recursively_subdivide_triangle(strategy,
                                   total_iterations=8,
                                   width=1080,
                                   height=1080,
                                   resize_size=None):
    """
    Generates the same output as the methods above (but the colors are set by the default settings),
    but all the logic compressed to a few lines, and it seems to be slightly faster.
    """
    def subdivide_and_draw(draw, vertices, i):
        if i < total_iterations:
            subdivide_vertex = vertices.pop(strategy[i % len(strategy)])
            midpoint = ((vertices[0][0] + vertices[1][0]) / 2,
                        (vertices[0][1] + vertices[1][1]) / 2)
            for vertex_index in range(2):
                subdivide_and_draw(draw,
                                   vertices=[
                                       subdivide_vertex, midpoint,
                                       vertices[vertex_index]
                                   ],
                                   i=i + 1)
        else:
            draw.polygon(xy=vertices)

    image, draw = new_image(size=(width, height))
    subdivide_and_draw(draw=draw,
                       vertices=[(width / 2, 0), (0, height), (width, height)],
                       i=0)
    save_image(image=image,
               file_name='Subdivide_i{}_{}'.format(
                   total_iterations, ''.join(str(i) for i in strategy)),
               resize_size=resize_size)
Exemplo n.º 2
0
def draw_chaos_game(image_width=1080, image_height=1080, polygon_points=None, iterations=10 ** 5, color=Colors.white):
    """

    :param image_width: the image width in pixels
    :param image_height: the image height in pixels
    :param polygon_points: a list of polygon points, where each point is a tuple of two numbers.
        In each iteration of the Chaos Game, one of these points will be chosen at random.
    :param iterations: the number of times a new point will be drawn halfway the previous and the random polygon point
    :param color: The color of the point to draw. Should be a tuple with rgb values, like (230, 25, 75)

    :return: The file path of the image that was created
    """
    if polygon_points is None:
        # default will be 3 (triangle) points: bottom left, bottom right, center top,
        polygon_points = [(0, image_height), (image_width, image_height), (image_width // 2, 0)]

    image, draw = new_image(size=(image_width, image_height))

    # this is the random_start_point, but I call it previous_point, since a new point will be calculated based on this
    previous_point = (random.randint(0, image_width), random.randint(0, image_height))
    for i in range(iterations):
        # choose one of the 'polygon_points' at random, then calculate the point halfway in that direction
        random_polygon_point = random.choice(polygon_points)
        previous_point = (
            ((random_polygon_point[0] + previous_point[0]) / 2),
            ((random_polygon_point[1] + previous_point[1]) / 2),
        )
        draw.point(xy=previous_point, fill=color)

    new_image_path = save_image(image=image, file_name='chaos_game')
    return new_image_path
Exemplo n.º 3
0
def draw_random_pursuit_curve(n=4,
                              image_width=1080,
                              image_height=1080,
                              iterations=30,
                              percentage=Decimal('0.05')):
    image, draw = new_image(size=(image_width, image_height))

    n_gon_points = [(random.randint(0, image_width),
                     random.randint(0, image_height)) for _ in range(n)]
    for i in range(iterations):
        draw.polygon(xy=n_gon_points)
        n_gon_points = get_next_polygon_points(polygon_points=n_gon_points,
                                               percentage=percentage)

    save_image(image=image,
               file_name='pursuit_curve_random',
               resize_size=(1080, 1080))
Exemplo n.º 4
0
def draw_pursuit_curve(image_width=1080,
                       image_height=1080,
                       iterations=30,
                       percentage=Decimal('0.5'),
                       margin=20):
    image, draw = new_image(size=(image_width, image_height))

    # The four corner points of a square, by taking the image dimensions and margin into account
    n_gon_points = [(margin, margin), (image_width - margin, margin),
                    (image_width - margin, image_height - margin),
                    (margin, image_height - margin)]

    for i in range(iterations):
        draw.polygon(xy=n_gon_points)
        n_gon_points = get_next_polygon_points(polygon_points=n_gon_points,
                                               percentage=percentage)

    save_image(image=image, file_name='pursuit_curve')
Exemplo n.º 5
0
def subdivide_triangle(strategy,
                       iterations=8,
                       width=1080,
                       height=1080,
                       background_color=Colors.white,
                       line_color=Colors.black,
                       resize_size=None):
    triangle = [
        (width / 2, 0),  # vertex A
        (0, height),  # vertex B
        (width, height),  # vertex C
    ]

    image, draw = new_image(size=(width, height), color=background_color)

    previous_generation_triangles = [
        triangle
    ]  # start with a list that consist of the starting triangle only
    for iteration in range(iterations):
        new_triangles = []
        for triangle in previous_generation_triangles:
            # for each polygon in the previous generation, subdivide that polygon into two new polygons,
            # extend the list of new triangles with the two subdivided triangles
            new_triangles += divide(triangle_vertices=triangle,
                                    iteration=iteration,
                                    strategy=strategy)

        # override the previous triangles with the new one, we only need to remember the last iteration
        previous_generation_triangles = new_triangles

    # previous_generation_triangles contains all triangles calculated in the last iteration, draw all of them!
    for triangle in previous_generation_triangles:
        draw.polygon(xy=triangle, outline=line_color)

    save_image(image=image,
               file_name='Subdivide_i{}_{}'.format(
                   iterations, ''.join(str(i) for i in strategy)),
               resize_size=resize_size)