Last week, I got an itch, as I often do when checking the emails sent from Pinterest. There was one pin of an excellent animation of rings comprised of dots rotating around a centre, and, on each segment of the rotation, the dot would arrive back at its starting point - that's a terrible explanation. What I mean is, should the ring be comprised of 6 dots, then for every sixth of the whole rotation, the original dots location would match the location of the following dot; a ring consisting of 12 dots would be similar - in the time that the first ring took for the original dots location to be matched by the subsequent dots location, the original dots location would be matched by the subsequent dots location. I'm not explaining it well, sorry. It inspired me to replicate it in p5js, so that's what I did, though with some measure of trepidation, as I was sure it would require some level of trigonometry.
Now, I took GCSE maths, but trigonometry has always been something I was utterly terrified of. I blame it on slide rules that my Dad had and being completely unable to get it clear in my head—I've even taken courses on Khan Academy, but it's still something I can't seem to get straight in my head (geddit?).
Anyway, I figured out how to do the math to do it eventually (after significant research) but then got stuck trying to rotate the dots: interestingly, the number of dots increased by six on each ring, so 6, 12, 18, 24, 30, 36.
After pestering Oliwia, who was stuck in a meeting, I called my Dad and explained my situation. After he started talking about trigonometry for a few minutes, I remembered why it wouldn't stick. But, in the process of explaining it, I clocked that the rotation was based on an arbitrary figure within a loop and that by incrementing that figure, I could alter the placement of the dots:
draw() { this.p5.fill(this.fill); this.p5.noStroke(); for ( let j = 0 + this.increment; j < this.p5.TWO_PI + this.increment; j += this.step ) { this.p5.circle( this.x + (Math.cos(j) * this.diameter) / 2, this.y + (Math.sin(j) * this.diameter) / 2, this.dotWidth, ); } const incrementValue = 360 / this.number / 3000; this.increment = this.increment + incrementValue >= this.step ? 0 : this.increment + incrementValue; }
After clocking that, I realised I didn't need to keep on incrementing the increment but could set it to zero once it had reached the step value, which was derived from:
this.step = p5.TWO_PI / this.number;
This means that each time the subsequent dot reached the position of the original dot, the animation could be reset! Instead of each ring rotating for the entire three hundred and sixty degrees, the inner ring rotated sixty degrees and returned to its original position. The next outer ring rotated twenty-one and two-thirds degrees, and so on. Neat eh? The original and my animation are here and here.
No comments:
Post a Comment