Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Creative Coding
- * Week 3, 04 - spinning top: curved motion with sin() and cos()
- * by Indae Hwang and Jon McCormack
- * Copyright (c) 2014 Monash University
- *
- * This sketch is the first cut at translating the motion of a spinning top
- * to trace a drawing path. This sketch traces a path using sin() and cos()
- *
- */
- int maxLines = 100; //allocate space for up to 100 lines
- float x[] = new float[maxLines], y[] = new float[maxLines]; // current drawing position
- float dx[] = new float[maxLines], dy[] = new float[maxLines]; // change in position at each frame
- float rad[] = new float[maxLines]; // radius for the moving hand
- int col[] = new int[maxLines];
- float max = 3; //dx/dy max default
- float min = 0.5; //dx/dy min default
- int radiusScale = 300; //Radius scale default value
- int frameCounter= 0, fps = 40; //Default framerate (fps)
- float blendRate = 3; //Rate for image blending
- int numLines = 20; //set default number of lines
- void setup() {
- size(1000, 500);
- frameRate(fps);
- colorMode(HSB, 100);
- for (int i =0; i < numLines; i++) {
- // initial position in the centre of the screen
- x[i] = width/2;
- y[i] = height/2;
- // dx and dy are the small change in position each frame
- dx[i] = random(min, max);
- dy[i] = random(min, max);
- col[i] = (int) random(100);
- }
- background(0);
- }
- void draw() {
- // blend the old frames into the background
- blendMode(BLEND);
- fill(0, blendRate);
- rect(0, 0, width, height);
- for (int i =0; i < numLines; i++) {
- rad[i] = radians(frameCount+(i*PI));
- // calculate new position
- if (mousePressed) {
- x[i] = mouseX > x[i] ? x[i] + dx[i] : x[i] - dx[i];
- y[i] = mouseY > y[i] ? y[i] + dy[i] : y[i] - dy[i];
- } else {
- x[i] += dx[i];
- y[i] += dy[i];
- }
- //When the shape hits the edge of the window, it reverses its direction and changes velocity
- if (x[i] > width-100 || x[i] < 100) {
- dx[i] = dx[i] > 0 ? -random(min, max) : random(min, max);
- }
- if (y[i] > height-100 || y[i] < 100) {
- dy[i] = dy[i] > 0 ? -random(min, max) : random(min, max);
- }
- float bx = x[i] + 100 * sin(rad[i]);
- float by = y[i] + 100 * cos(rad[i]);
- fill(180);
- float radius = radiusScale * sin(rad[i]*0.1);
- float handX = bx + radius * sin(rad[i]*3);
- float handY = by + radius * cos(rad[i]*3);
- stroke(col[i], 100, 100, 50);
- line(bx, by, handX, handY);
- ellipse(handX, handY, 2, 2);
- }
- if (frameCounter > 0) {
- showCommands();
- frameCounter--;
- //if (frameCounter > 0 ) { frameRate(fps); }
- }
- if (keyPressed == true) {
- switch( key ) {
- case 's':
- saveFrame("yourName-###.jpg");
- println("Screenshot saved");
- break;
- case '+':
- max = ((max+1)< (height/2)) ? max+1 : max;
- println("Max dx/dy increased to: " + max);
- break;
- case '-':
- max = ((max-1)>0) ? max-1 : max;
- println("Max dx/dy reduced to: " + max);
- break;
- case 'R':
- radiusScale = ((radiusScale+1)< height) ? radiusScale+1 : radiusScale;
- println("Radius scaling increased to: " + radiusScale);
- break;
- case 'r':
- radiusScale = ((radiusScale-1)>0) ? radiusScale-1 : radiusScale;
- println("Radius scaling reduced to: " + radiusScale);
- break;
- case 'c':
- for (int i=0; i<numLines; i++) {
- col[i] = (int) random(100);
- }
- println("Colours randomised.");
- break;
- case 'x':
- background(0);
- println("Background cleared.");
- break;
- case '?':
- displayCommands();
- break;
- case 'B':
- blendRate = ((blendRate+0.1)< 20) ? blendRate+0.1 : blendRate;
- println("Blend rate increased to: " + blendRate);
- break;
- case 'b':
- blendRate = ((blendRate-0.1)>=0) ? blendRate-0.1 : blendRate;
- println("Blend rate reduced to: " + blendRate);
- break;
- case 'l':
- numLines = (numLines>1) ? numLines-1 : numLines;
- println("Line count reduced to: " + numLines);
- setup();
- break;
- case 'L':
- numLines = (numLines<100) ? numLines+1 : numLines;
- println("Line count increased to: " + numLines);
- setup();
- break;
- }
- }
- }
- void showCommands() {
- float txSize = 20;
- textSize(txSize);
- String helpLines[] = new String[9];
- helpLines[0] = "--Commands--";
- helpLines[1] = "s: Save screenshot";
- helpLines[2] = "x: Clear background";
- helpLines[3] = "?: Show this help";
- helpLines[4] = "c: Randomise colours";
- helpLines[5] = "+/-: +/- dx/dy max";
- helpLines[6] = "R/r: +/- radius";
- helpLines[7] = "L/l: +/- number of lines";
- helpLines[8] = "B/b: +/- blend rate";
- fill(0,10);
- noStroke();
- rect(0,0,width/3,(txSize+2)*helpLines.length);
- fill(100);
- for (int i=0; i< helpLines.length; i++){
- text(helpLines[i], 5, txSize+2+((txSize+2)*i));
- }
- }
- void displayCommands(){
- frameCounter = (int) floor(5*frameRate); // Show help for this number of frames
- showCommands();
- }
Advertisement
Add Comment
Please, Sign In to add comment