Creative Coding Week 3: Exercise 3 - Spinning Tops
The spinning top displays lines that rotate around a point as it moves around the display.
> View source code for preceding sketch.
/*
* 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()
*
*/
float x, y; // current drawing position
float dx, dy; // change in position at each frame
float rad; // radius for the moving hand
void setup() {
size(500, 500);
// initial position in the centre of the screen
x = width/2;
y = height/2;
// dx and dy are the small change in position each frame
dx = random(-1, 1);
dy = random(-1, 1);
background(0);
}
void draw() {
// blend the old frames into the background
blendMode(BLEND);
fill(0, 5);
rect(0, 0, width, height);
rad = radians(frameCount);
// calculate new position
x += dx;
y += dy;
float max = 1;
float min = 0.5;
//When the shape hits the edge of the window, it reverses its direction and changes velocity
if (x > width-100 || x < 100) {
dx = dx > 0 ? -random(min, max) : random(min, max);
}
if (y > height-100 || y < 100) {
dy = dy > 0 ? -random(min, max) : random(min, max);
}
float bx = x + 100 * sin(rad);
float by = y + 100 * cos(rad);
fill(180);
float radius = 100 * sin(rad*0.1);
float handX = bx + radius * sin(rad*3);
float handY = by + radius * cos(rad*3);
stroke(255, 50);
line(bx, by, handX, handY);
ellipse(handX, handY, 2, 2);
}
My sketch added extra spinning tops and colour.
> View source code for preceding sketch.
/*
* 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()
*
* Amended by Gerard Holden to draw more than one top in different colours.
*/
int num = 3; // mumber of tracings
float[] x; // x-position of tracing
float[] y; // y-position of tracing
float[] dx; // Change in x-position at each frame
float[] dy; // change in y-position at each frame
float[] rad; // radius for the moving hand
color[] colour; // colour of the moving hand
void setup() {
size(500, 500);
x = new float[num];
y = new float[num];
dx = new float[num];
dy = new float[num];
rad = new float[num];
colour = new color[num];
for (int i = 0; i < num; i++) {
// a random starting point for each tracing
x[i] = random(110, width-110);
y[i] = random(110, height-110);
// dx and dy are the small change in position each frame
dx[i] = random(-1, 1);
dy[i] = random(-1, 1);
rad[i] = radians(frameCount);
colour[i] = color(random(256), random(256), random(256));
}
background(0);
}
void draw() {
// blend the old frames into the background
blendMode(BLEND);
fill(0, 5);
rect(0, 0, width, height);
float max = 1;
float min = 0;
for (int i = 0; i < num; i++) {
rad[i] = radians(frameCount) * (i+11)/(num+10);
// Calculate new position
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 = 100 * sin(rad[i]*0.1);
float handX = bx + radius * sin(rad[i]*3);
float handY = by + radius * cos(rad[i]*3);
stroke(colour[i], 50);
line(bx, by, handX, handY);
ellipse(handX, handY, 2, 2);
}
}