Hi!
I just discoverd processing and and recently started programming a little "sketch"
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;
}