Last active
May 26, 2019 22:26
-
-
Save villares/eb4d6fc7fc13fcca0823fde4bc819dfc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Alexandre B A Villares - https://abav.lugaralgum.com/sketch-a-day | |
| # More explorations of lines in grids | |
| from pyp5js import * | |
| W = None | |
| H = None | |
| num = None | |
| line_combos = None | |
| from random import shuffle | |
| from itertools import product, combinations, permutations, combinations_with_replacement | |
| space = 15 | |
| position = 0 # initial position | |
| def setup(): | |
| global line_combos, W, H, position, num | |
| createCanvas(1045, 700) | |
| frameRate(5) | |
| strokeWeight(2) | |
| grid = product(range(-1, 2), repeat=2) # 3X3 | |
| # all possible lines | |
| lines = combinations(grid, 2) | |
| # colect only short lines | |
| short_lines = [] | |
| for l in lines: | |
| (x0, y0), (x1, y1) = l[0], l[1] | |
| if dist(x0, y0, x1, y1) < 3: # short as defined here... | |
| short_lines.append(l) | |
| num_short_lines = len(short_lines) | |
| print("Number of possible lines: {}".format(num_short_lines)) | |
| # main stuff | |
| line_combos = list(combinations(short_lines, 4)) | |
| shuffle(line_combos) | |
| num = len(line_combos) | |
| print(num) | |
| W, H = (width - space) / space, (height - space) / space | |
| print((W, H, W * H)) | |
| def draw(): | |
| global position | |
| background(240) | |
| i = position | |
| for y in range(H): | |
| for x in range(W): | |
| if i < len(line_combos): | |
| push() | |
| translate(space + space * x, space + space * y) | |
| draw_combo(i) | |
| pop() | |
| i += 1 | |
| if i < len(line_combos): | |
| position += W | |
| def draw_combo(n): | |
| siz = space / 3. | |
| for i, sl in enumerate(line_combos[n]): | |
| colorMode(HSB) | |
| stroke(i * 64, 160, 160) | |
| (x0, y0), (x1, y1) = sl[0], sl[1] | |
| line(x0 * siz, y0 * siz, x1 * siz, y1 * siz) | |
| def keyPressed(): | |
| if key == "s": | |
| saveFrame("####.png") | |
| # ==== This is required by pyp5js to work | |
| # Register your events functions here | |
| event_functions = {'keyPressed': 'keyPressed'} | |
| start_p5(setup, draw, event_functions) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Loading module from “http://localhost:8000/target/itertools.js” was blocked because of a disallowed MIME type (“text/html”). | |
| localhost:8000 | |
| Loading module from “http://localhost:8000/target/random.js” was blocked because of a disallowed MIME type (“text/html”). | |
| localhost:8000 | |
| Loading failed for the module with source “http://localhost:8000/target/org.transcrypt.__runtime__.js”. localhost:8000:13:1 | |
| Loading failed for the module with source “http://localhost:8000/target/itertools.js”. localhost:8000:13:1 | |
| Loading failed for the module with source “http://localhost:8000/target/random.js”. localhost:8000:13:1 | |
| Loading failed for the module with source “http://localhost:8000/target/pyp5js.js”. localhost:8000:13:1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment