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 › blinking while mouseMoved()
Page Index Toggle Pages: 1
blinking while mouseMoved() (Read 720 times)
blinking while mouseMoved()
Feb 3rd, 2010, 3:47pm
 
Hi!
I just discoverd processing  and and recently started programming a little "sketch"  Roll Eyes

I was trying to programming a fraktal - Pythagoras tree (search in wikipedia for Pythagoras_tree). It works quite well. But there is kind of a problem. I extend my sketch by the function mouseMoved(). it calculates the angle(Thales)  in percent (from 0 to 1) every time when the mousex-Value are changing. But i reconized that the window is starting to blinking/flashing in random way every time when i move the mousecursor over the screen. so its kind of annoying. I wonder if there is someone who can help me fix that problem.

Code:

float k=0.5;
void setup()
{  
   noLoop();
   smooth();
   size(screen.width,700);
   background(0);
}
void draw()
{
PVector [] trunk = new PVector[2];
trunk[0]=new PVector(width/2-25,height-40);
trunk[1]=new PVector(width/2+25,height-40);

drawPythagorasTress(trunk,k);
 
}
void mouseMoved()
{

 float c=mouseX;
 if(((c/1000)>0.1)&&((c/1000)<0.85))
 {
 k=(c/1000);
 }
 println(k);
 
 
 background(0);
 redraw();

}

void drawPythagorasTress(PVector [] AB, float k)
{
 
 if(AB[0].dist(AB[1])>=1)
 {
 drawRect(AB);
 PVector [] CD = new PVector[2];
   CD = calcRect(AB);
 PVector []  ThalesPoint = new PVector[1];
   ThalesPoint = ThalesPoint(AB, k);
 PVector [] newPoint1 = new PVector[2];
   newPoint1[0] = new PVector(CD[0].x,CD[0].y);
   newPoint1[1] = new PVector(ThalesPoint[0].x,ThalesPoint[0].y);
 PVector [] newPoint2 = new PVector[2];
   newPoint2[0] = new PVector(ThalesPoint[0].x,ThalesPoint[0].y);
   newPoint2[1] = new PVector(CD[1].x,CD[1].y);
 drawPythagorasTress(newPoint1,k);
 drawPythagorasTress(newPoint2,k);
 }


}

void drawRect(PVector [] AB)
{
   PVector [] CD = new PVector[4];
   CD = calcRect(AB);
   
   stroke(255);
   line(AB[0].x,AB[0].y,AB[1].x,AB[1].y);
   stroke(255);
   line(AB[1].x,AB[1].y,CD[1].x,CD[1].y);
   line(CD[1].x,CD[1].y,CD[0].x,CD[0].y);
   line(AB[0].x,AB[0].y,CD[0].x,CD[0].y);
}

PVector [] calcRect (PVector [] L)
{
 float dX=(L[1].x-L[0].x);
 float dY=(L[1].y-L[0].y);
 float cx,cy,dx,dy;
 
   PVector [] CD = new PVector[2];
   CD[0] = new PVector(L[0].x+dY,L[0].y-dX); // C
   CD[1] = new PVector(L[1].x+dY,L[1].y-dX); // D
   return CD;
}

PVector [] ThalesPoint(PVector[] L, float k)
{
 //Calculate angles
 float dX=L[1].x-L[0].x;
 float dY=L[1].y-L[0].y;
 float Xn= k*L[0].x + (1-k)*L[1].x - (1+sqrt(k*(1-k))) * (-dY);
 float Yn= k*L[0].y + (1-k)*L[1].y - (1+sqrt(k*(1-k))) * (dX);
 PVector [] ThalesPoint= new PVector[1];
 ThalesPoint[0] = new PVector(Xn,Yn);
return  ThalesPoint;
}
Re: blinking while mouseMoved()
Reply #1 - Feb 3rd, 2010, 4:10pm
 
You're fighting with the draw loop!  You use noLoop(), but IIRC mouse events are only registered at the end of draw.  draw stops == no more mouse events...  I see very little when I run it.  Take out noLoop and redraw and move background(0) to draw() and it looks better to me, though a little slow (but also rather nice plant-like shapes):

Code:
float k=0.5; 
void setup()
{  
   //noLoop();
   smooth();
   size(screen.width,700);
   background(0);
}
void draw()
{
 background(0);
PVector [] trunk = new PVector[2];
trunk[0]=new PVector(width/2-25,height-40);
trunk[1]=new PVector(width/2+25,height-40);

drawPythagorasTress(trunk,k);
 
}
void mouseMoved()
{

 float c=mouseX;
 if(((c/1000)>0.1)&&((c/1000)<0.85))
 {
 k=(c/1000);
 }
 println(k);
 
 
 
 //redraw();

}

void drawPythagorasTress(PVector [] AB, float k)
{
 
 if(AB[0].dist(AB[1])>=1)
 {
 drawRect(AB);
 PVector [] CD = new PVector[2];
   CD = calcRect(AB);
 PVector []  ThalesPoint = new PVector[1];
   ThalesPoint = ThalesPoint(AB, k);
 PVector [] newPoint1 = new PVector[2];
   newPoint1[0] = new PVector(CD[0].x,CD[0].y);
   newPoint1[1] = new PVector(ThalesPoint[0].x,ThalesPoint[0].y);
 PVector [] newPoint2 = new PVector[2];
   newPoint2[0] = new PVector(ThalesPoint[0].x,ThalesPoint[0].y);
   newPoint2[1] = new PVector(CD[1].x,CD[1].y);
 drawPythagorasTress(newPoint1,k);
 drawPythagorasTress(newPoint2,k);
 }


}

void drawRect(PVector [] AB)
{
   PVector [] CD = new PVector[4];
   CD = calcRect(AB);
   
   stroke(255);
   line(AB[0].x,AB[0].y,AB[1].x,AB[1].y);
   stroke(255);
   line(AB[1].x,AB[1].y,CD[1].x,CD[1].y);
   line(CD[1].x,CD[1].y,CD[0].x,CD[0].y);
   line(AB[0].x,AB[0].y,CD[0].x,CD[0].y);
}

PVector [] calcRect (PVector [] L)
{
 float dX=(L[1].x-L[0].x);
 float dY=(L[1].y-L[0].y);
 float cx,cy,dx,dy;
 
   PVector [] CD = new PVector[2];
   CD[0] = new PVector(L[0].x+dY,L[0].y-dX); // C
   CD[1] = new PVector(L[1].x+dY,L[1].y-dX); // D
   return CD;
}

PVector [] ThalesPoint(PVector[] L, float k)
{
 //Calculate angles
 float dX=L[1].x-L[0].x;
 float dY=L[1].y-L[0].y;
 float Xn= k*L[0].x + (1-k)*L[1].x - (1+sqrt(k*(1-k))) * (-dY);
 float Yn= k*L[0].y + (1-k)*L[1].y - (1+sqrt(k*(1-k))) * (dX);
 PVector [] ThalesPoint= new PVector[1];
 ThalesPoint[0] = new PVector(Xn,Yn);
return  ThalesPoint;
}


Re: blinking while mouseMoved()
Reply #2 - Feb 3rd, 2010, 4:27pm
 
THANKS!  Cheesy solves my problem completly!

[Moderator's note: I removed the quote. No need to quote a whole answer, including code, since you are on the same thread. Bad habit from e-mail (where it is still a bad habit! Smiley)]
Page Index Toggle Pages: 1