Created
January 10, 2017 13:09
-
-
Save flavioamieiro/326acfecf61bda20e2937015f00b658b to your computer and use it in GitHub Desktop.
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 | |
DEBUG = True | |
LOOP = False | |
POINTS_IN_CIRCLE = 1000 | |
NUMBER_OF_CIRCLES = 10 | |
def setup(): | |
size(500, 500) | |
noFill() | |
if LOOP: | |
frameRate(2) | |
else: | |
do_it() | |
def draw(): | |
if LOOP: | |
do_it() | |
def do_it(): | |
background(255) | |
if DEBUG: | |
strokeWeight(20) | |
POINTS_IN_CIRCLE = 360 | |
if DEBUG: | |
circle_1 = Circle(Point(250, 250), 100, color(255, 0, 0)) | |
circle_2 = Circle(Point(200, 200), 100, color(255, 255, 0)) | |
circle_3 = Circle(Point(300, 300), 100, color(255, 0, 255)) | |
circles = [circle_1, circle_2, circle_3] | |
else: | |
circles = [get_random_circle() for x in range(NUMBER_OF_CIRCLES)] | |
for idx, circle in enumerate(circles): | |
print(idx, len(circles) - 1) | |
other_circles = circles[:idx] + circles[idx+1:] | |
for pt in circle.points: | |
circles_pt_is_in = [c for c in other_circles if is_inside_or_on_circle(pt, c)] | |
if circles_pt_is_in: | |
clrs = [pt.clr] + [cir.points[0].clr for cir in circles_pt_is_in] | |
pt.clr = average_color(*clrs) | |
if DEBUG: | |
print(idx, [is_inside_or_on_circle(pt, c) for c in other_circles], get_separate_clr(pt.clr)) | |
for circle in circles: | |
circle.draw() | |
def get_separate_clr(clr): | |
return red(clr), green(clr), blue(clr) | |
def get_concentric_circles(center, start_radius, num_circles=NUMBER_OF_CIRCLES, step=10): | |
radius = start_radius | |
circles = [] | |
for i in range(num_circles): | |
circles.append(Circle(center, radius, random_color())) | |
radius += step | |
return circles | |
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 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 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 = 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment