Created
May 13, 2015 17:02
-
-
Save ron4stoppable/3bc8bf93b3acb7999ca7 to your computer and use it in GitHub Desktop.
TriangleFlower by Radimir Bobev
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
float tri_angle; | |
float radius = 150; | |
int max_tris = 90; | |
color tri_colors[] = new color[max_tris]; | |
float lower_threshold = 0.2; | |
float upper_threshold = 0.8; | |
void setup() { | |
size( 400, 400, P2D ); | |
smooth(8); | |
} | |
void draw() { | |
Fill_Colors(); | |
colorMode( RGB, 255 ); | |
background( 255 ); | |
strokeWeight( 1 ); | |
float start_tris = 5; | |
float start_angle = 360/start_tris; | |
float cur_offset = map( t, 0, lower_threshold, 0, 360-start_angle ); | |
// build triangles | |
if ( t <= lower_threshold ) { | |
for ( int i=0; i<start_tris; i++ ) { | |
if ( t >= lower_threshold/start_tris*i ) { | |
pushMatrix(); | |
translate( width/2, height/2 ); | |
int index = floor(map( i, 0, start_tris, 0, max_tris )); | |
color c = tri_colors[index]; | |
Section( radius, cur_offset - i*start_angle, start_angle, c ); | |
popMatrix(); | |
} | |
} | |
} | |
// spin wheel | |
else { | |
int sides = 5; | |
if ( t <= upper_threshold ) { | |
sides = min( max_tris, floor( map( t, lower_threshold, 0.45, 5, max_tris/2 ) ) ); | |
if ( t > 0.45 ) sides = min( max_tris, floor( map( t, 0.45, 0.5, max_tris/2, max_tris ) ) ); | |
if ( t > 0.5 ) sides = min( max_tris, floor( map( t, 0.5, 0.55, max_tris, max_tris/2 ) ) ); | |
if ( t > 0.55 ) sides = min( max_tris, floor( map( t, 0.55, upper_threshold, max_tris/2, 5 ) ) ); | |
pushMatrix(); | |
translate( width/2, height/2 ); | |
rotate( radians( (t-lower_threshold)*360 ) ); | |
Full_Circle( sides ); | |
popMatrix(); | |
} | |
// deconstruct triangles | |
else { | |
Fill_Colors(); | |
cur_offset = map( t, upper_threshold, 1, 0, 360 ); | |
int end_tris = floor( map( t, upper_threshold, 1, 5, 1 ) ); | |
for ( int i=0; i<end_tris; i++ ) { | |
pushMatrix(); | |
translate( width/2, height/2 ); | |
int index = ceil(map( i, 0, start_tris, 0, max_tris-1 )); | |
color c = tri_colors[index]; | |
Section( radius, cur_offset - i*start_angle, start_angle, c ); | |
popMatrix(); | |
} | |
} | |
} | |
} | |
void Section( float r, float offset, float angle, color c ) { | |
fill( c ); | |
stroke( 0 ); | |
pushMatrix(); | |
//translate( width/2, height/2 ); | |
rotate( radians(offset) ); | |
float outer_side = tan(radians(angle/2)) * r; | |
triangle( 0,0, -outer_side,-r, outer_side,-r ); | |
popMatrix(); | |
} | |
void Full_Circle( int sides ) { | |
float a = 360f/sides; | |
for ( int i=0; i<sides; i++ ) { | |
int index = floor(map( i, 0, sides, 0, max_tris )); | |
int shift = min( max_tris, floor( map( t, lower_threshold, 1, 0, 200 ) ) ); | |
Shift_Colors( shift ); | |
color c = tri_colors[index]; | |
Section( radius, i*a, a, c ); | |
} | |
} | |
void Fill_Colors() { | |
for ( int i=0; i<max_tris; i++ ) { | |
colorMode( HSB, 1 ); | |
tri_colors[i] = color( map( i, 0, max_tris, 0, 1f ), 1, 1 ); | |
} | |
} | |
void Shift_Colors( int dist ) { | |
Fill_Colors(); | |
color[] temp = new color[max_tris]; | |
for ( int i=dist; i<max_tris; i++ ) { | |
temp[i-dist] = tri_colors[i]; | |
} | |
int d = max_tris - dist; | |
for ( int i=0; i<dist; i++ ) { | |
temp[i+d] = tri_colors[i]; | |
} | |
tri_colors = temp; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment