FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   Refresh problems
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Refresh problems  (Read 301 times)
st33d

WWW Email
Refresh problems
« on: Jan 17th, 2005, 1:52am »

Any ideas on what might be causing the slow refresh on this spring applet I've just coded? Is there a way to fix it?
 
Code:

spring one;
void setup(){
  size(200,200);
  one = new spring (100,0,100,100);
}
void loop(){
  background(200);
  if (mousePressed && overRect(mouseX,mouseY,one.x2-5,one.y2-5,one.x2+5,one.y2+5)){
    one.move();
  }
  one.updateTop();
  one.d();
}
class spring{
  float x1,y1,x2,y2,r;
  spring (float x1, float y1, float x2, float y2){
    this.x1 = x1;
    this.y1 = y1;
    this.x2 = x2;
    this.y2 = y2;
    r = sqrt(sq(x1-x2)+sq(y1-y2));
  }
  void move(){
    x2 = mouseX;
    y2= mouseY;
  }
  void updateTop(){
    float l = sqrt(sq(x1-x2)+sq(y1-y2));
    float th = atan2 (y1-y2,x1-x2);
    if (int(l) != int(r)){
 l -= impulse (l,r);
 x2 = cos (th) * l;
 y2 = sin (th) * l;
    }
  }
  void d(){
    ellipse (x1-5,y1-5,10,10);
    ellipse (x2-5,y2-5,10,10);
    line(x1,y1,x2,y2);
  }
}
 
float impulse (float span, float rest){
  return float(Math.log(span)) - float(Math.log(rest));
}
 
boolean overRect(float xo, float yo, float x1, float y1, float x2, float y2){
  if (xo >= x1 && xo <= x2 &&
  yo >= y1 && yo <= y2) {
    return true;
  } else {
    return false;
  }
}
 

I could murder a pint.
Pages: 1 

« Previous topic | Next topic »