We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Can you help me optimize this simple code
Page Index Toggle Pages: 1
Can you help me optimize this simple code ? (Read 1168 times)
Can you help me optimize this simple code ?
May 9th, 2010, 3:31pm
 
Hi , i got this sketch.

http://openprocessing.org/visuals/?visualID=9579



It has 4 very similar functions. they just draw an Image or rects , and make them grow as you hoover them.


But when i run my sketch , i can see in the  activity monitor , my cpu gets a lotta work , for this simple sketch.


i think the problem is that it is re drawing the image over and over, and this might be slowing it down , is there a more optimal way to do this

so any ideas on how to optimize this

thanx !

Code:
int borde = 20;
int hoover = 5;
int borde_hoover;
void setup (){
 
 size (600,600);
 smooth();
 background (255);
}

void draw (){
   background (255);
 drawR_1 (width/2-borde,width/4,height/4);
   drawR_2 (width/2-borde,width-width/4,height/4);
drawR_3 (width/2-borde,width/4,height-height/4);
 drawR_4 (width/2-borde, width-width/4,height-height/4);
 
}


// FUNCTIONS


void drawR_1 ( int tamanio, int rx_pos, int yx_pos ){
 fill (0);
imageMode(CENTER);
 noStroke ();
   if (mouseX > borde_hoover && mouseX < width/2-borde_hoover && mouseY > borde && mouseY < height/2-borde_hoover ){

    tamanio += hoover ;
  }
    else{
tamanio -=hoover ;

  }
  PImage b;

b = loadImage("javi_img_01.jpg");

   
   image (b , rx_pos, yx_pos , tamanio, tamanio);
}


void drawR_2 ( int tamanio, int rx_pos, int yx_pos ){
 fill (0);
 rectMode ( CENTER);
 noStroke ();
   if (mouseX > width/2+borde_hoover && mouseX < width-borde_hoover && mouseY > borde && mouseY < height/2-borde_hoover ){

    tamanio += hoover ;
  }
    else{
tamanio -=hoover ;

  }
   
   rect ( rx_pos, yx_pos , tamanio, tamanio);
}

void drawR_3 ( int tamanio, int rx_pos, int yx_pos ){
 fill (0);
 rectMode ( CENTER);
 noStroke ();
   if (mouseX > borde && mouseX < width/2-borde_hoover && mouseY > height/2 && mouseY < height-borde_hoover ){

    tamanio += hoover ;
  }
    else{
tamanio -=hoover ;

  }
   
   rect ( rx_pos, yx_pos , tamanio, tamanio);
}

void drawR_4 ( int tamanio, int rx_pos, int yx_pos ){
 fill (0);
 rectMode ( CENTER);
 noStroke ();
   if (mouseX > width/2+borde_hoover && mouseX < width- borde && mouseY > height/2+borde_hoover && mouseY < height-borde_hoover ){

    tamanio += hoover ;
  }
    else{
tamanio -=hoover ;

  }
   
   rect ( rx_pos, yx_pos , tamanio, tamanio);
}
Re: Can you help me optimize this simple code ?
Reply #1 - May 9th, 2010, 3:50pm
 
This'll help!!!

Quote:
int borde = 20;
int hoover = 5;
int borde_hoover;
PImage b;


void setup(){
  size(600,600);
  smooth();
  background(255);
  b = loadImage("javi_img_01.jpg");
}

void draw(){
  background(255);
  drawR_1(width/2-borde,width/4,height/4);
  drawR_2(width/2-borde,width-width/4,height/4);
  drawR_3(width/2-borde,width/4,height-height/4);
  drawR_4(width/2-borde, width-width/4,height-height/4);
}

void drawR_1( int tamanio, int rx_pos, int yx_pos ){
  fill (0);
  imageMode(CENTER);
  noStroke ();
  if(mouseX > borde_hoover && mouseX < width/2-borde_hoover && mouseY > borde && mouseY < height/2-borde_hoover ){
    tamanio += hoover ;
  } else {
    tamanio -=hoover ;
  }
  image (b , rx_pos, yx_pos , tamanio, tamanio);
}


void drawR_2 ( int tamanio, int rx_pos, int yx_pos ){
  fill (0);
  rectMode ( CENTER);
  noStroke ();
  if (mouseX > width/2+borde_hoover && mouseX < width-borde_hoover && mouseY > borde && mouseY < height/2-borde_hoover ){

    tamanio += hoover ;
  }
  else{
    tamanio -=hoover ;

  }

  rect ( rx_pos, yx_pos , tamanio, tamanio);
}

void drawR_3 ( int tamanio, int rx_pos, int yx_pos ){
  fill (0);
  rectMode ( CENTER);
  noStroke ();
  if (mouseX > borde && mouseX < width/2-borde_hoover && mouseY > height/2 && mouseY < height-borde_hoover ){

    tamanio += hoover ;
  }
  else{
    tamanio -=hoover ;

  }

  rect ( rx_pos, yx_pos , tamanio, tamanio);
}

void drawR_4 ( int tamanio, int rx_pos, int yx_pos ){
  fill (0);
  rectMode ( CENTER);
  noStroke ();
  if (mouseX > width/2+borde_hoover && mouseX < width- borde && mouseY > height/2+borde_hoover && mouseY < height-borde_hoover ){

    tamanio += hoover ;
  }
  else{
    tamanio -=hoover ;

  }

  rect ( rx_pos, yx_pos , tamanio, tamanio);
}



There's no need to load the same image every time the screen is drawn! Just once!
Re: Can you help me optimize this simple code ?
Reply #2 - May 10th, 2010, 3:13am
 
great thanx !




one more question,

i have thought of this,  draw once the images or rects in setup and then in draw , just when hoovering draw a bigger rect or image, and then when we are not hoovering anymore , refresh the background and redraw the normal , non scaled image.



or something like , this , do you think this can be more efficient ?



One last question how do you made your code colored ?



Thanx !!!

Wink Wink Wink Wink Wink Wink Wink
Re: Can you help me optimize this simple code ?
Reply #3 - May 12th, 2010, 5:16am
 
any thoughs on this last thought of optimizing my code ?


thanx !!
Re: Can you help me optimize this simple code ?
Reply #4 - May 12th, 2010, 6:16am
 
TweakingKnobs wrote on May 10th, 2010, 3:13am:
do you think this can be more efficient

The best way to know... is to try!

Quote:
One last question how do you made your code colored

PDE window > Edit > Copy for Discourse

Mmm, let me test my custom colors:
Ah, interesting, a bug:
Quote:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 9
     at processing.app.tools.DiscourseFormat.appendFormattedLine(DiscourseFormat.java:16
9)
     at processing.app.tools.DiscourseFormat.show(DiscourseFormat.java:97)
     at processing.app.Editor$34.actionPerformed(Editor.java:982)


It works with another, saved sketch.
Quote:
static final int D_TRIANGLE = 1;
static final int D_TEXT = 2;

int m1, m2;

void setup()
{
  size(800, 500);

  pgMask = createGraphics(width, height, JAVA2D);
  // We cannot use mask() with a JAVA2D graphics...
  pgShape = createGraphics(width, height, P2D);
  font = loadFont("AmericanTypewriter-24.vlw");

  // Make gradient
  pgGradient = CreateGradient(width / 3, height / 3, 200, #00FF00, #0088FF);
Re: Can you help me optimize this simple code ?
Reply #5 - May 12th, 2010, 8:44am
 
Thanx philho , ill give it some other shots , adn ill come back with the results  SmileyGrin

Page Index Toggle Pages: 1