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.
Page Index Toggle Pages: 1
Strange BUG ?!? (Read 427 times)
Strange BUG ?!?
May 3rd, 2007, 10:34am
 
Hi.
I'm sorry to post all this code (pretty useless).
I just made this little program to understand drawing splines and automatically adjust their joint (made them 2-continuous).

If I try to comment the call to grid_lines() in draw() and lauch it after the first clic it frezes and crashes.

To use it simply click and drag to see a preview of the control point, then release. Click again for another spline.
Press any key on the keyboard to try to "optimize" it.
(note: try to make a couple of splines close to continuity and other with cups joints...)

Sorry for the errors, but im so tireeeed !
Re: Strange BUG ?!?
Reply #1 - May 3rd, 2007, 10:36am
 
Code:

/****************** COPYRIGHT MATTIA JAKOB DAL POZZO ******************
[M ~ J]
May 2007 - Simple program to draw continuos bezier curves
mattjakob [at] gmail [dot] com
**********************************************************************/


float[][] p = new float[201][4]; //array containing the points
boolean[] opt = new boolean[201]; //array containing the status (optimized or not) of the 'knots' (a 'knot' is the first starting point of each curve
int gridSize; //size of each grid cell
int curves = 1; //amount of curves
boolean ren; //decides whether to stop updatin the scene (in order to display the points being optimized with different colours)


void setup()
{
size(500, 500);
p[1][1] = 250;
p[1][2] = 250;
smooth();
ren = true;
frameRate(10);
}



void draw()
{

if(ren) //if 'true' updates the scene otherwise maintains the current buffer --> drawing from other functions is possible because there's no more a 'clear display' call
{
background(255);


/************** BUG: try to comment grid_lines() *************/

grid_lines();

/**************************** BUG ****************************/

fill(255);
stroke(0);
ellipse(width/2, height/2, gridSize * 4/10, gridSize * 4/10); //the first point is drawn by default at the center of the screen

//DRAWING GEOMETRY FOR EACH CURVE
for(int c = 1; c <= curves - 1; c++) //straight lines connecting vertices and control points
{
noFill();
stroke(180);
beginShape(LINES);
vertex(p[c*3 - 2][1], p[c*3 - 2][2]); //c*3 - 2 FIRST VERTEX
vertex(p[c*3 - 2 + 1][1], p[c*3 - 2 + 1][2]); //c*3 - 2 + 1 FIRST CONTROL POINT
vertex(p[c*3 - 2 + 1][1], p[c*3 - 2 + 1][2]); //c*3 - 2 + 2 SECOND CONTROL POINT
vertex(p[c*3 - 2 + 2][1], p[c*3 - 2 + 2][2]); //c*3 - 2 + 3 LAST VERTEX
vertex(p[c*3 - 2 + 2][1], p[c*3 - 2 + 2][2]); //the parameter 'c' indicates the current curve
vertex(p[c*3 - 2 + 3][1], p[c*3 - 2 + 3][2]);
endShape();

stroke(90); //bezier drawing
strokeWeight(2);
beginShape();
vertex(p[c*3 - 2][1], p[c*3 - 2][2]);
bezierVertex(p[c*3 - 2 + 1][1], p[c*3 - 2 + 1][2], p[c*3 - 2 + 2][1], p[c*3 - 2 + 2][2], p[c*3 - 2 + 3][1], p[c*3 - 2 + 3][2]);
endShape();
strokeWeight(1);

fill(255); //a contour is drawn for each vertex/control point
for(int i = 0; i <= 3; i++)
{
stroke(i/3f * 255);
fill(255);
ellipse(p[c*3 - 2 + i][1], p[c*3 - 2 + i][2], gridSize * 3/10, gridSize * 3/10);
}

if(! opt[c] && c != 1) //if a 'knot' is marked as 'false' in opt[] it means it has not bee optimized and thus there's no continuiti in that joint. Red marks display this situation
{
noFill();
stroke(255, 0, 0);
ellipse(p[c*3 - 2][1], p[c*3 - 2][2], gridSize * 5/10, gridSize * 5/10);
fill(255);
stroke(0, 0, 0);
ellipse(p[c*3 - 2][1], p[c*3 - 2][2], gridSize * 3/10, gridSize * 3/10);
}

}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
}



//GENERATES A SERIES OF VALID POINT FOR A BEZIER SEGMENT
void genCurve(float xP, float yP, int zP, int xC, int yC)
{
print("CURVE #" + curves + " with center of rotation in quadrant (Y X) ");

fill(255,0,0);
ellipse(xC, yC, 5, 5);
fill(255);

float Dx, Dy, Dz, zC;
Dx = Dy = Dz = zC = 0;
Dx = xP - xC;
Dy = yP - yC;


while(Dz == 0)
{
zC = zP + int(round(random(-gridSize * 2, gridSize * 2)));
Dz = zP - zC;
}

Re: Strange BUG ?!?
Reply #2 - May 3rd, 2007, 10:36am
 
Code:


float Ap1 = atan(Dy/Dx); //Ap1 is the angle made by P1
float A1 = 0; //A1 is a 'world' angle


if(Dy > 0 && Dx > 0) //cases to calculate the correct A1 angle... see the 'quadrant' during runtime in the terminal
{
A1 = Ap1;
println("+ +");
}
if(Dy > 0 && Dx < 0)
{
A1 = radians(180) + Ap1;
println("+ -");
}
if(Dy < 0 && Dx < 0)
{
A1 = radians(180) + Ap1;
println("- -");
}
if(Dy < 0 && Dx > 0)
{
A1 = Ap1;
println("- +");
}


float rad = sqrt(sq(Dx) + sq(Dy));
println("rad: " + rad);
println("ap : " + degrees(Ap1));
println("a : " + degrees(A1));


for(int i = 0; i <= 3; i++)
{
p[(curves*3 - 2) + i][1] = xC + (rad * cos(A1 + (i * Ap1))); //writes found point coordinates in p[][]
p[(curves*3 - 2) + i][2] = yC + (rad * sin(A1 + (i * Ap1)));
p[(curves*3 - 2) + i][3] = 0;

stroke(200);
line(xC, yC, p[(curves*3 - 2) + i][1], p[(curves*3 - 2) + i][2]); //useful when draggin the mouse: due to refresh it allows to have a sort of interactive preview
}



}


void mouseDragged()
{
genCurve(p[curves*3 - 2][1], p[curves*3 - 2][2] , 0, mouseX, mouseY); //when dragging (hold + mmove) the mouse generates parameters for the curve segment
}

void grid_lines()
{
stroke(230);
gridSize = width / 20;
for(int i = 0; i<= width; i = i + gridSize)
{
line(i,0,i,height);
}
for(int i = 0; i<= height; i = i + gridSize)
{
line(0,i,width,i);
}
}


//FIRST OPTIMIZATION FOR NON-CUSP JOINTS
void optim()
{
println();
println();
float p1X, p1Y, p2X, p2Y, cX, cY, d1, d2, dp, ncX, ncY;
ncX = ncY = 0;
for(int i = 2; i <= curves - 1; i++)
{

p1X = p[i*3 - 2 +1][1];
p1Y = p[i*3 - 2 +1][2];
p2X = p[i*3 - 2 -1][1];
p2Y = p[i*3 - 2 -1][2];
cX = p[i*3 - 2][1];
cY = p[i*3 - 2][2];

d1 = dist(p1X,p1Y,cX,cY);
d2 = dist(p2X,p2Y,cX,cY);
dp = dist(p1X,p1Y,p2X,p2Y);

if((d1+d2)/dp * 100 < 140)
{
fill(255,0,0);
ellipse(p1X, p1Y, 5,5);
fill(0,255,0);
ellipse(p2X, p2Y, 5,5);
fill(255,0,255);
ellipse(((p1X + p2X) / 2), ((p1Y + p2Y) / 2), 7,7);

ncX = (p1X + p2X) / 2; //the correct point of joint between the two curves is collinear with the control points (cp)
ncY = (p1Y + p2Y) / 2; //and is placed halfway between cp1 and cp2
p[i*3 - 2][1] = ncX; //updates the array containing the points
p[i*3 - 2][2] = ncY;

opt[i] = true; //since this joint now is continuous it's status is updated

println("CURVE #" + i + "optimized with °1°");
}
}
ren = true;
}


void keyPressed()
{
ren = false; //to activate the First optimization the user has to press a key. 'ren' becomes false so that
optim(); //drawing() does not overwrite the screen with a clean display --> in this way it is possible for optim()
} //to add some drawing

void mouseReleased() //if the mouse is released it means the user has decided a final position for its curve (previsualized while dragging)
{
curves++;
}
Page Index Toggle Pages: 1