mattjakob
YaBB Newbies
Offline
Posts: 22
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++; }