Advertisement
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 num = 5; // the number of spinning tops
- float[] x = new float [num];
- float[] y = new float [num]; // current drawing position
- float[] dx = new float [num];
- float[] dy = new float [num];
- float rad; // radius for the moving hand
- void setup() {
- size(500, 500);
- background(0);
- for (int i=0; i<num; i++) {
- float val = 2; // value for the max/min value of the increments
- x[i]= random(width); // initial horizontal starting point
- y[i]= random(height); // initial vertical starting point
- dx[i] = random(-val, val); // x-increments
- dy[i] = random(-val, val); // y-increments
- }
- }
- void draw() {
- // blendMode(BLEND); .-> not needed, it's the default
- fill(0, 20); // I would increase the alpha to 15 in order not to have 'smears', except if that's what you want ;)
- rect(0, 0, width, height);
- rad = radians(frameCount);
- for (int i=0; i<num; i++) {
- spinningTop(i, x[i], y[i], dx[i], dx[i]);
- }
- }
- void spinningTop(int i, float _x, float _y, float _dx, float _dy) {
- // calculate new position
- x[i] += dx[i];
- y[i] += dy[i];
- float max = 1.5;
- float min = 0.5;
- //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);
- float by = y[i] + 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);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement