Looking for an explanation regarding Radians, Two_Pi, and the length of an array
in
Programming Questions
•
1 year ago
Hi everyone!
Im nearly done with a project I'm making on processing. 100 arcs are scaling from the center while spinning around the canvas, in sequential order. I've received help in the form of code that does
exactly what I'm looking for, though I forgot to ask the person who helped me out explain a few things. I can almost understand what's going on, but there are a few concepts that I can't grasp just yet (which is why I'm here).
Here is the code:
- //----Segment class----
- class Segment
- {
- int myColorIndex;
- float myStart;
- float myStop;
- float myScale;
- Segment( int _colorIndex, float _start, float _stop, float _scale )
- {
- myColorIndex = _colorIndex;
- myStart = _start;
- myStop = _stop;
- myScale = _scale;
- }
- void draw()
- {
- pushMatrix();
- fill( c[myColorIndex] );
- scale(myScale);
- arc(0, 0, 600, 600, myStart, myStop);
- popMatrix();
- }
- void simulate()
- {
- myScale = constrain( myScale + random(0.1), 0, 1);
- }
- }
- //----Segment class----
- //----Variables----
- float angle = 0;
- float f = 0;
- float r = random(100);
- color c[] =
- {
- #D6D315, #BA3FB2, #C45B31, #FFE296, #B4BA6C, #7D2A35, #E6CCCE, #663C20
- };
- Segment segments[];
- //----Variables----
- //-----Setup------
- void setup()
- {
- size(400, 400);
- smooth();
- noStroke();
- segments = new Segment[100];
- for ( int i=0; i<segments.length; i++)
- {
- segments[i] = new Segment( getColor(), float(i)*TWO_PI/float(segments.length),
- float(i+1)*TWO_PI/float(segments.length), 0 );
- }
- }
- int getColor()
- {
- float r = random(100);
- if( r < 0.5 ) return(0);
- if( r < 2 ) return(1);
- if( r < 5 ) return(2);
- if( r < 9 ) return(3);
- if( r < 14 ) return(4);
- if( r < 33 ) return(5);
- if( r < 63 ) return(6);
- return(7);
- }
- //------Setup------
- //------Draw------
- void draw ()
- {
- background(#FAD7E9);
- angle = angle + .1;
- translate(width/2, height/2);
- rotate(radians(angle));
- for ( int i = 0; i < segments.length; i++ )
- {
- if( i == update )
- {
- segments[i].simulate();
- }
- if( update < segments.length && segments[update].myScale == 1 )
- {
- update++;
- }
- segments[i].draw();
- }
- }
- int update = 0;
- //-----Draw-----
Lines 24-27
"void simulate"? I checked the processing language section and couldn't find anything pertaining to the word "simulate." I understand that "void" denotes that no value will be returned, but I've got no idea what simulate means.
Lines 48-53
If I'm not mistaken, this is the equation that produces each arc. I understand how a "for" iteration works (initialize, test, update). However I don't get how (i)(TWO_PI) / (length of array) creates 100 matching arcs. If someone could explain this formula, and how it works, I'd greatly appreciate it.
Lastly I tried writing out the formula and doing the math by hand to see what I came up with, but I cant determine what the length of the array is so I dont have all the variables I need to figure it out. How do you determine where the length of the array is at, at any given moment?
Lines 76-89
I don't understand any of this last part :) though I realize that "update" is an int. The fact that it's name is update throws me off, as if it is an inherent function of processing, but I just can't wrap my head around whats happening in these last lines.
Anything you guys [and gals] have to offer in terms of help or perspective would be great!
Thanks again!!
1