Why is this sketch so slow? Get() makes it slow

edited January 2017 in Questions about Code

Hi

Can anyone tell me why this sketch runs so slow? The line with the "Get()" is the problem...

I tried different renderer (P2D, P3D...) but without success..

float  Angle = 0;

void setup() 
{
  size(600, 600,P3D ); 
  noSmooth();
}

void draw() {
  background(0);

  Angle += 0.8;

  // Draw something in the upper left corner (
  colorMode(HSB, 100);  
  pushMatrix();
  translate(7, 7);
  rotate(radians(Angle*34));
  Star5(0, 0, 9.5, 9.5, color(0, 100, 100), color(20, 100, 100), color(40, 100, 100), color(60, 100, 100), color(80, 100, 100), 100);
  popMatrix();


  colorMode(RGB, 255);
  noFill();

  // draw a rect (Pixel scan area)
  strokeWeight(1);
  stroke(255, 255);
  rect(-1, -1, 15, 15);



  int OffsetX = 50;
  int OffsetY = 50;
  int CellSize = 30;
  int GridWidth = 33;
  int CellRadius = 2;

  noStroke();

  // Scan Pixels in the upper left corner and draw matrix
  for (int yy = 0; yy < 13; yy++)
  {
    for (int xx = 0; xx < 13; xx++)
    {      
      color c = get(xx, yy); // THIS LINE MAKES THE WHOLE SKETCH VERY SLOW 
      //color c = color(xx*20,yy*20,0); // TO SEE THE DIFFERENCE... USE THIS LINE INSTEAD
      fill(c);
      rect(OffsetX + (xx * GridWidth), OffsetY + (yy * GridWidth),CellSize,CellSize,CellRadius);
    }
  } 
}


void Star5(float x1, float y1, float r1, float r2, color c1, color c2, color c3, color c4, color c5, int alpha) 
{
  // Draws a star
  noStroke();
  beginShape();
  fill(c1, alpha);
  vertex(x1+cos(radians(36*1))*r2, y1+sin(radians(36*1))*r2);
  vertex(x1+cos(radians(36*2))*r1, y1+sin(radians(36*2))*r1);
  fill(c2, alpha);
  vertex(x1+cos(radians(36*3))*r2, y1+sin(radians(36*3))*r2);
  vertex(x1+cos(radians(36*4))*r1, y1+sin(radians(36*4))*r1);
  fill(c3, alpha);
  vertex(x1+cos(radians(36*5))*r2, y1+sin(radians(36*5))*r2);
  vertex(x1+cos(radians(36*6))*r1, y1+sin(radians(36*6))*r1);
  fill(c4, alpha);
  vertex(x1+cos(radians(36*7))*r2, y1+sin(radians(36*7))*r2);
  vertex(x1+cos(radians(36*8))*r1, y1+sin(radians(36*8))*r1);
  fill(c5, alpha);
  vertex(x1+cos(radians(36*9))*r2, y1+sin(radians(36*9))*r2);
  vertex(x1+cos(radians(36*10))*r1, y1+sin(radians(36*10))*r1);
  endShape();
}

Answers

  • edited January 2017 Answer ✓

    Renderers P2D & P3D are OpenGL-based. Access to a specific pixel is slow for OpenGL AFAIK. 3:-O

    Also, you should favor pixels[] over both get() & set() when possible. *-:)

  • Thank you so much!!

    Heres my much faster sketch:

    float  Angle = 0;
    
    void setup() 
    {
      size(600, 600,P3D ); 
      noSmooth();
    }
    
    void draw() {
      background(0);
    
      Angle += 0.8;
    
      // Draw something in the upper left corner (
      colorMode(HSB, 100);  
      pushMatrix();
      translate(7, 7);
      rotate(radians(Angle*34));
      Star5(0, 0, 9.5, 9.5, color(0, 100, 100), color(20, 100, 100), color(40, 100, 100), color(60, 100, 100), color(80, 100, 100), 100);
      popMatrix();
    
    
      colorMode(RGB, 255);
      noFill();
    
      // draw a rect (Pixel scan area)
      strokeWeight(1);
      stroke(255, 255);
      rect(-1, -1, 15, 15);
    
    
    
      int OffsetX = 50;
      int OffsetY = 50;
      int CellSize = 30;
      int GridWidth = 33;
      int CellRadius = 2;
    
      noStroke();
    
      // Scan Pixels in the upper left corner and draw matrix
      loadPixels();
      for (int yy = 0; yy < 13; yy++)
      {
        for (int xx = 0; xx < 13; xx++)
        {      
          color c = pixels[xx+(yy*height)];
          fill(c);
          rect(OffsetX + (xx * GridWidth), OffsetY + (yy * GridWidth),CellSize,CellSize,CellRadius);
        }
      } 
    }
    
    
    void Star5(float x1, float y1, float r1, float r2, color c1, color c2, color c3, color c4, color c5, int alpha) 
    {
      // Draws a star
      noStroke();
      beginShape();
      fill(c1, alpha);
      vertex(x1+cos(radians(36*1))*r2, y1+sin(radians(36*1))*r2);
      vertex(x1+cos(radians(36*2))*r1, y1+sin(radians(36*2))*r1);
      fill(c2, alpha);
      vertex(x1+cos(radians(36*3))*r2, y1+sin(radians(36*3))*r2);
      vertex(x1+cos(radians(36*4))*r1, y1+sin(radians(36*4))*r1);
      fill(c3, alpha);
      vertex(x1+cos(radians(36*5))*r2, y1+sin(radians(36*5))*r2);
      vertex(x1+cos(radians(36*6))*r1, y1+sin(radians(36*6))*r1);
      fill(c4, alpha);
      vertex(x1+cos(radians(36*7))*r2, y1+sin(radians(36*7))*r2);
      vertex(x1+cos(radians(36*8))*r1, y1+sin(radians(36*8))*r1);
      fill(c5, alpha);
      vertex(x1+cos(radians(36*9))*r2, y1+sin(radians(36*9))*r2);
      vertex(x1+cos(radians(36*10))*r1, y1+sin(radians(36*10))*r1);
      endShape();
    }
    
Sign In or Register to comment.