Skip to content

Instantly share code, notes, and snippets.

@dokluch
Last active August 29, 2015 13:56
Show Gist options
  • Save dokluch/8866359 to your computer and use it in GitHub Desktop.
Save dokluch/8866359 to your computer and use it in GitHub Desktop.
The First Assignment in "Introduction to Computational Arts: Processing" on coursera.org
/*
Instead of creating 20 shapes on 2 types, I have created 20 distinctive
random dynamic shapes to show the beauty and diversity of randomness
Each circle contains a shape with random amount of vertices
generated on the circle's edge in random places and moving with
random speed in random direction.
Click anywhere to get completely different picture
Main idea is redrawing all circles on each step
through the .update() chain
circle.update() draws circle and calls for embedded someshape.update()
which in turn calls for each of inside pointOnCircle.update()
Nik Ska, 2014
CC-BY
*/
color sFill = #F3F3F3; //fill color for circles
color lStrokeBegin = #409DAE; //starting stroke color
color lStrokeEnd = #E85F00; //ending stroke color
color bgColor = #494949;
int spacing = 50;
int rad = 100; //circle radius
int sW = 4;
color newStroke;
//defining array for shapes
circle[] circles = new circle[20];
void setup(){
size(750, 600);
background(bgColor);
frameRate(25);
smooth();
initCircles(); //initialize all circles
}
void draw(){
background(bgColor);
//updating all objects
for(int k = 0; k < circles.length; k = k+1){
circles[k].update();
}
//noLoop();
}
void initCircles(){
//this function creates an array of 20 objects
//for furtehr use. calling it again will reload all circles
for(int i = 0; i < 5; i = i+1){
for(int j = 0; j < 4; j = j+1){
newStroke = lerpColor(lStrokeBegin, lStrokeEnd, float(4*i + j+1)/20);
circles[4*i + j] = new circle(rad,i*rad+(i+1.5)*spacing,j*rad+(j+1.5)*spacing, sFill, newStroke, sW);
}
}
}
void mouseReleased(){
//re-initialize on click
initCircles();
}
class pointOnCircle{
//point oject
float radius, speed, angle, xPos, yPos, strokeW;
int direction;
pointOnCircle(float r, float x, float y, float s){
radius = r;
speed = random(-4,4); //speed of point motion
angle = random(0, TWO_PI); //random starting angle
strokeW = s;
xPos = x;
yPos = y;
}
float[] update(){
//on update we add speed to the angle
//of the point and then calculate
//new point position
angle = angle + radians(speed);
float[] point = {0.5*(radius-strokeW)*cos(angle)+xPos, 0.5*(radius-strokeW)*sin(angle)+yPos};
return(point);
}
}
class someShape{
//this is a shape drawn inside the circle
float xPos, yPos, radius, strokeW;
int vertexNum;
color strokeColor;
//crucial part - ArrayList here
//we need this because we don't know
//how much points we're going to have
//prior to the object initialization
ArrayList<pointOnCircle> points;
someShape(float x, float y, float r, color sC, float sW){
xPos = x;
yPos = y;
radius = r;
strokeW = sW;
strokeColor = sC;
vertexNum = floor(random(3, 8));
//define points array
points = new ArrayList<pointOnCircle>();
//initialize
for(int i = 0; i < vertexNum; i = i+1){
points.add(new pointOnCircle(radius, xPos, yPos, strokeW));
}
}
void update(){
//This method will loop through
//"points" array and update each point
//which will return its new position
//that is going to be used as a new vertex
noFill();
stroke(strokeColor);
strokeWeight(strokeW);
strokeJoin(ROUND);
beginShape();
//println(points.size);
for (int i = 0; i < points.size(); i = i+1) {
pointOnCircle p = points.get(i);
float[] newP = p.update();
vertex(newP[0],newP[1]);
}
endShape(CLOSE);
}
}
class circle {
//this class defines circle and contains a shape
float radius, xPos, yPos, strokeW;
color fillColor, strokeColor;
someShape sh;
circle(float r, float x, float y, color fC, color sC, float sW){
radius = r;
xPos = x;
yPos = y;
fillColor = fC;
strokeColor = sC;
strokeW = sW;
//inintialize shape
sh = new someShape(xPos, yPos, radius, strokeColor, strokeW);
}
void update(){
//this method will draw a new ellipse
//and update inner shape object
//.update() method will draw the figure based on points
//inside the shape
smooth();
noStroke();
fill(fillColor);
ellipse(xPos, yPos, radius, radius);
sh.update();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment