From 95d8ed3af9f069620896d65ad24f64770611ffb1 Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 11 Mar 2019 21:45:42 +0400 Subject: Initial commit --- .gitignore | 2 + chaos_game/sierpinski_carpet.py | 96 +++++++++++++++++++++++++++++++++++++++ chaos_game/sierpinski_triangle.py | 94 ++++++++++++++++++++++++++++++++++++++ times_table.py | 84 ++++++++++++++++++++++++++++++++++ 4 files changed, 276 insertions(+) create mode 100644 .gitignore create mode 100644 chaos_game/sierpinski_carpet.py create mode 100644 chaos_game/sierpinski_triangle.py create mode 100644 times_table.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6b75623 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +venv \ No newline at end of file diff --git a/chaos_game/sierpinski_carpet.py b/chaos_game/sierpinski_carpet.py new file mode 100644 index 0000000..22deee1 --- /dev/null +++ b/chaos_game/sierpinski_carpet.py @@ -0,0 +1,96 @@ +from pygame.locals import * +from pygame import Vector2, Surface +import pygame + +import random +from sys import argv + + +def convert2int(t): + return list(map(int, t)) + + +def new_point_square(): + attractor_point = random.choice(vertices) + _ = Vector2( + (points[-1].x + 2 * attractor_point.x) / 3, + (points[-1].y + 2 * attractor_point.y) / 3 + ) + points.append(_) + return _ + + +def generate_surface(iterations): + surf = Surface((WIDTH, HEIGHT)) + + for _ in range(iterations): + new_point = new_point_square() + surf.set_at(convert2int(new_point), WHITE) + + return surf + + +pygame.init() + +WIDTH, HEIGHT = SIZE = 1000, 1000 + +screen = pygame.display.set_mode(SIZE) + +BLACK = Color('black') +WHITE = Color('white') +GREEN = Color('green') +font = pygame.font.SysFont('courier', 30) + + +vertices = [ + Vector2(200, 200), Vector2(500, 200), Vector2(800, 200), + Vector2(200, 500), Vector2(800, 500), + Vector2(200, 800), Vector2(500, 800), Vector2(800, 800) +] + +points = [Vector2(201, 200)] + + +iterations = 0 +static = False +surface = None + +try: + iterations = int(argv[1]) + static = True + surface = generate_surface(iterations) + +except (ValueError, IndexError): + pass + + +while True: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pygame.quit() + quit() + + if static: + screen.fill(BLACK) + + screen.blit(surface, (0, 0)) + + for point in vertices: + pygame.draw.circle(screen, GREEN, convert2int(point), 3) + + else: + for point in points: + screen.set_at(convert2int(point), WHITE) + + for point in vertices: + pygame.draw.circle(screen, GREEN, convert2int(point), 3) + + new_point_square() + + iterations += 1 + + text = font.render('interation: {}'.format(iterations), 4, WHITE) + screen.fill(BLACK, text.get_rect(x=10, y=10)) + screen.blit(text, text.get_rect(x=10, y=10)) + + pygame.display.flip() diff --git a/chaos_game/sierpinski_triangle.py b/chaos_game/sierpinski_triangle.py new file mode 100644 index 0000000..4509fac --- /dev/null +++ b/chaos_game/sierpinski_triangle.py @@ -0,0 +1,94 @@ +from pygame.locals import * +from pygame import Vector2, Surface +import pygame + +import random +from sys import argv + + +def convert2int(t): + return list(map(int, t)) + + +def new_point_triangle(): + attractor_point = random.choice(vertices) + path = attractor_point - points[-1] + _ = points[-1] + path.normalize() * path.length() / 2 + points.append(_) + return _ + + +def generate_surface(iterations): + surf = Surface((WIDTH, HEIGHT)) + + for _ in range(iterations): + new_point = new_point_triangle() + surf.set_at(convert2int(new_point), WHITE) + + return surf + + +pygame.init() + +WIDTH, HEIGHT = SIZE = 1000, 1000 + +screen = pygame.display.set_mode(SIZE) + +BLACK = Color('black') +WHITE = Color('white') +GREEN = Color('green') + +font = pygame.font.SysFont('courier', 30) + +vertices = [ + Vector2(200, 200), + Vector2(505, 505), + Vector2(30, 400) +] + + +points = [Vector2(201, 200)] + +iterations = 0 +static = False +surface = None + +try: + iterations = int(argv[1]) + static = True + surface = generate_surface(iterations) + +except (ValueError, IndexError): + pass + + +while True: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pygame.quit() + quit() + + if static: + screen.fill(BLACK) + + screen.blit(surface, (0, 0)) + + for point in vertices: + pygame.draw.circle(screen, GREEN, convert2int(point), 3) + + else: + for point in points: + screen.set_at(convert2int(point), WHITE) + + for point in vertices: + pygame.draw.circle(screen, GREEN, convert2int(point), 3) + + new_point_triangle() + + iterations += 1 + + text = font.render('interation: {}'.format(iterations), 4, WHITE) + screen.fill(BLACK, text.get_rect(x=10, y=10)) + screen.blit(text, text.get_rect(x=10, y=10)) + + pygame.display.flip() diff --git a/times_table.py b/times_table.py new file mode 100644 index 0000000..9def165 --- /dev/null +++ b/times_table.py @@ -0,0 +1,84 @@ +from math import sin, cos, pi + +from pygame.math import Vector2 +import pygame +from pygame.locals import * + +pygame.init() + + +def convert2int(t): + return list(map(int, t)) + + +def up(start_color: list, index: int): + for component in range(256): + new_color = start_color.copy() + new_color[index] = component + yield new_color + + +def down(start_color: list, index: int): + for component in range(255, -1, -1): + new_color = start_color.copy() + new_color[index] = component + yield new_color + + +def color_iterator(): + while True: + yield from up([255, 0, 0], 1) + yield from down([255, 255, 0], 0) + yield from up([0, 255, 0], 2) + yield from down([0, 255, 255], 1) + yield from up([0, 0, 255], 0) + yield from down([255, 0, 255], 2) + + +count = 200 +multiple = 1 +radius = 400 +step = 0.01 +center = Vector2(radius, radius) + +theta = 2 * pi / count + +alpha = lambda n: (theta * n) % count + +f_x = lambda n: cos(alpha(n)) * radius + center.x +f_y = lambda n: sin(alpha(n)) * radius + center.y + +f = lambda n: Vector2(f_x(n), f_y(n)) + + +BLACK = Color('black') +WHITE = Color('white') +RED = Color('red') + +WIDTH, HEIGHT = SIZE = radius * 2, radius * 2 +screen = pygame.display.set_mode(SIZE) + +color = color_iterator() + +clock = pygame.time.Clock() +while True: + clock.tick(60) + for event in pygame.event.get(): + if event.type == QUIT: + pygame.quit() + quit() + + screen.fill(BLACK) + + c = next(color) + + pygame.draw.circle(screen, c, convert2int(center), radius, 3) + + for n in range(count): + p1 = f(n) + p2 = f(multiple * n) + pygame.draw.line(screen, c, convert2int(p1), convert2int(p2), 1) + + multiple += step + + pygame.display.flip() -- cgit v1.2.3