Created
November 3, 2016 03:06
-
-
Save SamGRosen/ee7b32f0a5441cb11db6ec8e7d346245 to your computer and use it in GitHub Desktop.
This file contains 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
from __future__ import division | |
import math | |
last_points = [] | |
sides = 6 | |
m = None | |
def setup(): | |
global m | |
strokeWeight(2) | |
size(650,650) | |
background(0) | |
colorMode(HSB, 360, 100, 100) | |
m = Mandala(25) | |
def draw(): | |
if mousePressed: | |
m.touched_down = True; | |
m.create(mouseX, mouseY) | |
else: | |
m.no_last_points() | |
if keyPressed: | |
clear() | |
class Mandala: | |
def __init__(self, sides): | |
self.sides = sides | |
self.center = (width//2, height//2) | |
self.lastPoints = [None for _ in range(sides)] | |
self.sat = 50 | |
self.bright = 50 | |
self.h = 100 | |
self.touched_down = False | |
def no_last_points(self): | |
self.lastPoints = [None for _ in range(self.sides)] | |
self.touched_down = False | |
def create(self, x, y): | |
print(x,y) | |
points = self.get_points(x , y) | |
stroke(self.h, self.sat, self.bright) | |
for i in range(self.sides): | |
mx = points[i][0] | |
my = points[i][1] | |
if self.touched_down: | |
#point(mx, my) | |
if self.lastPoints[i]: | |
lastPoint = self.lastPoints[i] | |
if(lastPoint != self.center and points[i] != self.center): | |
line(mx, my, lastPoint[0], lastPoint[1]) | |
self.lastPoints = points | |
self.update_color() | |
def get_points(self, x , y): | |
points = [] | |
midX = self.center[0] | |
midY = self.center[1] | |
radius = self.distance(x,y) | |
theta_offset = self.get_theta(x,y) | |
for i in range(self.sides): | |
theta = (i*(2*math.pi)) / self.sides | |
new_x = int( (math.cos(theta + theta_offset) * radius + midX )) | |
new_y = int( (math.sin(theta + theta_offset) * radius + midY )) | |
points.append((new_x, new_y)) | |
return points | |
def get_theta(self, x , y): | |
theta = 0; | |
midX = self.center[0] | |
midY = self.center[1] | |
if abs(x - midX) > 0: | |
slope = float((y - midY)) / (x - midX) | |
if x > midX and slope >= 0: | |
theta = math.atan(slope) | |
elif x < midX: | |
theta = math.pi + math.atan(slope) | |
elif x > midX and slope < 0: | |
theta = 2*math.pi + math.atan(slope) | |
else: | |
if y > midY: | |
print(y) | |
theta = math.pi / 2 | |
pass | |
elif y < midY: | |
print(y) | |
theta = 3 *math.pi / 2 | |
pass | |
return theta | |
def update_color(self): | |
self.h += 1 | |
#self.sat += 1 | |
self.bright += 1 | |
if self.h > 360: | |
self.h = 1 | |
if self.sat > 100: | |
self.sat = 1 | |
if self.bright > 100: | |
self.bright = 0 | |
def distance(self, x,y): | |
return math.sqrt((self.center[0] - x)**2 + (self.center[1] - y)**2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment