How does Processing handle methods drawing objects outside of the screen coordinates?

The reason I ask is because my code generates points that travels in a 3d trajectory within and outside of the screen coordinates. (Please note that I’m not using translate()).

Currently, the point() method - that handles the actual drawing to screen - is called regardless of whether the coordinates are within the screen boundaries or not. This had me thinking if my code could be optimized by restricting calling point() only for the points whose coordinates are within the bounds of the screen. Calling point() for coordinates outside of the screen seems superfluous and perhaps even wasteful in terms of processor resources.

Or is this is optimization handled automatically under the hood, so to speak?

1 Like

Welcome to the forum.

You don’t say but I am assuming you are using the default JAVA2D mode and P3D. Both P2D and P3D use OpenGL for rendering graphics. Now OpenGL is a very powerful rendering engine that will handle coordinates outside the display area seamlessly.

The GPU (graphics processing unit) is heavily optimized for handling rendering and will handle coordinates outside the display area much faster than anything we can do to avoid them.

4 Likes

Hello @Erik,

Give it a try!

I like to generate code to test such things under different conditions.

Some code to test performance:

// Performance setting boundaries for shapes
// glv
// 2023-12-31
// v1.0.0

boolean c;
int d, dt, off, mo;

void setup()
  {
  size(500, 500);
  println(cnt);
  d = 40;
  mo = 4; // The 4 was for testing to see boundary conditions!       
  off = 40/2-mo; 
  }
  
void draw()
  {
  surface.setTitle(str(int(frameRate)) + "  " + str(dt));  
  int t0 = millis();  
  for (int i=0; i<5000; i++)
    {
    float x = random(-500, 1000);
    float y = random(-500, 1000);
    
    // Uncomment to change offset with mouse
    //mo = (int) map(mouseX, 0, width, -100, 100);
    //off = 40/2 + mo;
   
    if (cnt == 0)
      {
      fill(255, 255, 0);  
      circle(x, y, d);
      }
    else if (cnt == 1)
      {
      c = x<(0-off) || x>(499+off) || y<(0-off) || y>(499+off);  
      if (c) 
        {
        fill(255, 0, 0);  
        circle(x, y, d);
        }
      }
    else if (cnt == 2)
      {
      c = x>(0+off) && x<(499-off) && y>(0+off) && y<(499-off); 
      if (c) 
        {
        fill(0, 255, 0);  
        circle(x, y, d);
        }
      }    
    }
  int t1 = millis();
  dt = t1-t0;
  //println(t, dt); // Printing to console can slow things down!
  }
  
int cnt;
  
void mousePressed()
  {
  cnt++;
  cnt = cnt%3;
  println(cnt);
  }

Click on mouse to go through the difference conditions.

This seemed to provide the best performance for the code provided:

I wrote this quickly and will revisit another day. Not drawing any conclusions as yet.

Feel free to scrutinize code and make changes.

Have fun!

:)