Last active
January 10, 2017 14:16
-
-
Save flavioamieiro/13c8c3f33e55fa0500166ac09afe2afb to your computer and use it in GitHub Desktop.
Another python processing sketch
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
import math | |
import itertools | |
DEBUG = True | |
LOOP = False | |
PROGRESS = True | |
RATIO_OF_POINTS_IN_CIRCLE = 6 | |
START_RADIUS = 5 | |
RADIUS_STEP = 10 | |
NUMBER_OF_WAVES = 10 | |
NUMBER_OF_CRESTS = 11 | |
def setup(): | |
size(500, 500) | |
noFill() | |
if LOOP: | |
frameRate(1) | |
else: | |
do_it() | |
def draw(): | |
if LOOP: | |
do_it() | |
def do_it(): | |
background(255) | |
if DEBUG: | |
wave_1 = Wave(Point(250, 250), clr=color(255, 0, 0)) | |
wave_2 = Wave(Point(150, 150), clr=color(200, 100, 0)) | |
wave_3 = Wave(Point(350, 350), clr=color(0, 0, 255)) | |
wave_4 = Wave(Point(350, 150), clr=color(255, 0, 255)) | |
wave_5 = Wave(Point(150, 350), clr=color(0, 255, 255)) | |
waves = [wave_1, wave_2, wave_3, wave_4, wave_5] | |
else: | |
waves = [get_random_wave() for x in range(NUMBER_OF_WAVES)] | |
for idx, wave in enumerate(waves): | |
if PROGRESS: | |
print(idx, len(waves) - 1) | |
other_waves = waves[:idx] + waves[idx+1:] | |
for pt in wave.points: | |
waves_pt_is_in = [w for w in other_waves if w.contains_point(pt)] | |
if waves_pt_is_in: | |
waves_to_avg = [pt.clr] + [wv.clr for wv in waves_pt_is_in] | |
pt.clr = average_color(*waves_to_avg) | |
for wave in waves: | |
wave.draw() | |
def get_separate_clr(clr): | |
return red(clr), green(clr), blue(clr) | |
def random_color(): | |
return color(int(random(255)), int(random(255)), int(random(255))) | |
def get_random_circle(): | |
random_center = Point(int(random(500)), int(random(500))) | |
return Circle(random_center, clr=random_color()) | |
def get_random_wave(): | |
random_center = Point(int(random(500)), int(random(500))) | |
return Wave(random_center, clr=random_color()) | |
def average_color(*clrs): | |
n = float(len(clrs)) | |
reds = [red(c) for c in clrs] | |
greens = [green(c) for c in clrs] | |
blues = [blue(c) for c in clrs] | |
new_r = int(sum(reds) / n) | |
new_g = int(sum(greens) / n) | |
new_b = int(sum(blues) / n) | |
return color(new_r, new_g, new_b) | |
def is_inside_or_on_circle(pt, circle): | |
distance_squared = (pt.x - circle.center.x)**2 + (pt.y - circle.center.y) ** 2 | |
return distance_squared <= circle.radius ** 2 | |
class Wave(object): | |
def __init__(self, center, start_radius=START_RADIUS, step=RADIUS_STEP, num_crests=NUMBER_OF_CRESTS, clr=None): | |
self.center = center | |
self.start_radius = start_radius | |
self.step = step | |
self.num_crests = num_crests | |
self.clr = color(0, 0, 0) if clr is None else clr | |
self.circles = self.build_circles() | |
def build_circles(self): | |
current_radius = self.start_radius | |
circles = [] | |
for i in range(self.num_crests): | |
circles.append(Circle(self.center, current_radius, self.clr)) | |
current_radius += self.step | |
return circles | |
@property | |
def points(self): | |
lists_of_points = [c.points for c in self.circles] | |
return list(itertools.chain(*lists_of_points)) | |
@property | |
def biggest_circle(self): | |
return self.circles[-1] | |
def contains_point(self, pt): | |
return is_inside_or_on_circle(pt, self.biggest_circle) | |
def draw(self): | |
for p in self.points: | |
p.draw() | |
class Circle(object): | |
def __init__(self, center, radius=100, clr=None): | |
self.center = center | |
self.radius = radius | |
self.clr = color(0, 0, 0) if clr is None else clr | |
self.points = [] | |
self.number_of_points = radius * RATIO_OF_POINTS_IN_CIRCLE | |
self.slice = 2 * math.pi / self.number_of_points | |
for i in range(0, self.number_of_points): | |
angle = self.slice * i | |
new_x = int(center.x + radius * math.cos(angle)) | |
new_y = int(center.y + radius * math.sin(angle)) | |
self.points.append(Point(new_x, new_y, self.clr)) | |
def draw(self): | |
[p.draw() for p in self.points] | |
def __repr__(self): | |
return 'c({}, {}), r={}'.format(self.center.x, self.center.y, self.radius) | |
class Point(object): | |
def __init__(self, x, y, clr=None): | |
self.x, self.y = x, y | |
if clr is None: | |
self.clr = color(0, 0, 0) | |
else: | |
self.clr = clr | |
def draw(self): | |
stroke(self.clr) | |
point(self.x, self.y) | |
def __repr__(self): | |
return 'Point({}, {}, clr=color({}, {}, {})'.format( | |
self.x, self.y, red(self.clr), green(self.clr), blue(self.clr)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment