Trouble understanding pushMatrix and transform
in
Programming Questions
•
3 months ago
I'm having trouble understanding how push/popMatrix and transforms work together. In the below sketch, i'm generating a pseudo random number every half second and drawing a line the height of that number in the bottom right corner. I'd like it to "transform" the line 5 pixels to the left before drawing the next line, then move those lines 5px to the left before drawing the next, etc. This way the right most line is always the current number and the history of the lines trails off to the left.
I thought I could accomplish this by pushing the matrix, transforming everything -5,0, poping the matrix, and then drawing the new line, but it doesnt seem to be working properly, it just draws the line over the old one.
I considered storing the values in an array and drawing them anew with an offset each time, but that seems like unnecessary work, and I don;t care to save the past values, I just want to see a history of the lines which still fit on the screen.
I would appreciate any assistance to _not_ include a copy/paste solution. Trying to learn by doing, ya know? ;)
- import controlP5.*;
- ControlP5 cp5;
- float startTime, currentTime, hitTime;
- float probabilityPower;
- int posOffset;
- float prNumber;
- void setup(){
- size(480,320);
- background(0);
- hitTime = 500;
- startTime = millis();
- cp5 = new ControlP5(this);
- cp5.addSlider("probabilityPower")
- .setPosition(10,10)
- .setSize(100,20)
- .setRange(0.0,10.0)
- .setValue(1.0)
- ;
- cp5.getController("probabilityPower")
- .getCaptionLabel()
- .align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE)
- .setPaddingX(0);
- }//setup
- void draw(){
- currentTime = millis() - startTime;
- if (currentTime >= hitTime){
- startTime = millis();
- prNumber = PseudoRandomNumber(0, 100, probabilityPower);
- cp5.addNumberbox("prNumber")
- .setPosition(150,10)
- .setSize(40,20)
- .lock()
- .setLabel("Result")
- ;
- int lineHeight = int(height-20-prNumber);
- pushMatrix();
- translate(-5,0);
- popMatrix();
- stroke(255);
- line(width-20, height-20, width-20,lineHeight);
- }//if currentTime
- }//draw
- float PseudoRandomNumber(int _min, int _max, float _probabilityPower){
- float _randomFloat = random(1);
- float result = round(_min + (_max-_min) * (pow(_randomFloat, _probabilityPower)));
- return result;
- }//PseudoRandomNumber
1