Seeking to improve my code
in
Programming Questions
•
1 year ago
Hello,
I am wondering if I could make my code more efficient. Currently it's quite CPU-hungry.
Any suggestion ?
Thank you in advance.
I am wondering if I could make my code more efficient. Currently it's quite CPU-hungry.
Any suggestion ?
Thank you in advance.
- /*
- ROTATING BARS
- EACH BAR PULSES AT A DIFFERENT RATE AND ROTATES IN 2D AROUND ITS CENTER
- VARYING HSB COLORS
- */
- import processing.opengl.*;
- // VARIABLES
- int numCubes = 1000;
- int maxDistance = 300;
- PVector[] places = new PVector[numCubes]; // stores positions of bars
- PVector[] angles = new PVector[numCubes]; // stores orientations of bars
- PVector[] rotFreq = new PVector[numCubes]; // stores rotation frequencies of bars
- float[] pulsFreq = new float[numCubes]; // stores pulse frequency for alpha
- float phaseX ; // phase for rotation around own center
- float phaseY ;
- int rate = 120; // set frameRate here
- int transparency ; // bar alpha
- float currentPulse; //pulse for HSB color variation
- float currentHue;
- float currentSat;
- float currentBri;
- float myHue1 = 170; // star's start hue 0/255 = red - 127 = green
- float myHue2 = 130; // star's final hue
- float mySat1 = 100; // star's start saturation
- float mySat2 = 0; // star's intermediate saturation
- float mySat3 = 255; // star's final saturation
- float myBri1 = 100; // star's start brilliance
- float myBri2 = 255; // star's intermediate brilliance
- float myBri3 = 240; // star's final brilliance
- float xRotateFrequency = 0.503 ; //frequency for rotation around x axis
- void setup() {
- size (500, 500);
- colorMode(HSB);
- frameRate(rate);
- strokeWeight(3);
- for (int i = 0; i < numCubes; i++) {
- places[i] = new PVector (random(0, maxDistance), random(0, maxDistance)) ;
- angles[i] = new PVector (random(0, TWO_PI), random(0, TWO_PI)) ;
- rotFreq[i] = new PVector (pow(10., random(-1., 0.001)), pow(10., random(-1, 0.001))) ;
- pulsFreq[i] = rotFreq[i].x;
- }
- background(170, 255,80 ,0);
- smooth();
- }
- void draw() {
- translate(width/2, height/2);
- for (int i = 0; i < numCubes; i++) {
- pushMatrix() ;
- // POSITION AROUND CENTER
- smooth();
- rotate(angles[i].x);
- places[i].x = places[i].x+int(random(-2,2));
- places[i].y = places[i].y+int(random(-2,2));
- translate(places[i].x, places[i].y);
- // ORIENTATION
- phaseX = (((rotFreq[i].x * float(frameCount) / float(rate)) * TWO_PI) % TWO_PI);
- phaseY = 0;
- rotate(phaseX);
- // TRANSPARENCY
- transparency = 50 + int(206. * (0.5 + (0.5 * (sin((((pulsFreq[i] * float(frameCount) / float(rate)) * TWO_PI) % TWO_PI))))));
- // COLOR
- currentPulse = unipolarTriangle((pulsFreq[i] * float(frameCount) / float(rate)) % 1.) ;
- // SETS HUE
- if (currentPulse < 0.5 ) {
- currentHue = myHue1 ;
- } else {
- currentHue = myHue2 ;
- }
- // SETS SATURATION
- if (currentPulse < 0.5 ) {
- currentSat = int(((1. - (2. * currentPulse)) * mySat1) + ((2. * currentPulse) * mySat2)) ;
- } else {
- currentSat = int(((1. - (2. * (currentPulse - 0.5))) * mySat2) + ((2. * (currentPulse - 0.5)) * mySat3)) ;
- }
- // SETS BRILLIANCE
- if (currentPulse < 0.5 ) {
- currentBri = int(((1. - (2. * currentPulse)) * myBri1) + ((2. * currentPulse) * myBri2)) ;
- } else {
- currentBri = int(((1. - (2. * (currentPulse - 0.5))) * myBri2) + ((2. * (currentPulse - 0.5)) * myBri3)) ;
- }
- fill(currentHue, currentSat, currentBri, 30);
- stroke(currentHue, currentSat, currentBri, 30);
- scale(1);
- line(-15,-15,15,15);
- popMatrix() ;
- }
- }
- float unipolarTriangle(float phase)
- {
- float amp;
- amp = min(phase, 1.) ;
- if (amp > 0.5 ) {
- amp = 0.5 - (amp - 0.5);
- }
- amp *= 2.;
- return amp;
- }
1