Created
August 14, 2017 07:04
-
-
Save zouhir/4b16777a3b4353a3d6462151961d05ba to your computer and use it in GitHub Desktop.
polygons
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
void polygon(int sideCount, float radius) { | |
float theta = 0.0; | |
float x = 0.0; | |
float y = 0.0; | |
float R = random(255); | |
float G = random (255); | |
float B = random (255); | |
beginShape(); | |
stroke(R, G, B); | |
if(sideCount % 3 == 0){ | |
fill(R, G, B); | |
} else { | |
noFill(); | |
} | |
for (int i=0; i<sideCount; i++) { | |
x = cos(theta)*radius; | |
y = sin(theta)*radius; | |
vertex(x, y); | |
theta += TWO_PI/sideCount; | |
} | |
endShape(CLOSE); | |
} // end polygon | |
void setup() { | |
float R = random(255); | |
float G = random (255); | |
float B = random (255); | |
size(800, 800); | |
setGradient(width/2, 0, width/2, height, color(R, G, B), color(R, G, B), 2); | |
int polyCount = 1500; | |
noFill(); | |
int sideCount = 0; | |
float radius = 0.0; | |
float rotation = 0.0; | |
for (int i=0; i<polyCount; i++) { | |
sideCount = int(random(3, 15)); | |
radius = random(2, 20); | |
rotation = random(TWO_PI); | |
pushMatrix(); | |
translate(random(width), random(height)); | |
rotate(rotation); | |
polygon(sideCount, radius); | |
popMatrix(); | |
} | |
} | |
void setGradient(int x, int y, float w, float h, color c1, color c2, int axis ) { | |
noFill(); | |
if (axis == 1) { // Top to bottom gradient | |
for (int i = y; i <= y+h; i++) { | |
float inter = map(i, y, y+h, 0, 1); | |
color c = lerpColor(c1, c2, inter); | |
stroke(c); | |
line(x, i, x+w, i); | |
} | |
} | |
else if (axis == 2) { // Left to right gradient | |
for (int i = x; i <= x+w; i++) { | |
float inter = map(i, x, x+w, 0, 1); | |
color c = lerpColor(c1, c2, inter); | |
stroke(c); | |
line(i, y, i, y+h); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment