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 › Creating the sweep function
Page Index Toggle Pages: 1
Creating the sweep function (Read 261 times)
Creating the sweep function
Feb 25th, 2009, 3:21pm
 
Hi guys & gals,

I am trying to create the sweep function that is generally found in all 3d rendering applications. I would like to sweep/extrude a 2d object, such as circle, along a predefined 2d path hence creating a 3d shape. I want to rotate this 2d shape so that it becomes perpendicular to the 2d path. Here is the code i'm currently working on, where vertices is the 2d shape and path is the 2d path. I'm calculating the gradient of any two points along the path and use it to calculate the angle of rotation so that is perpendicular to the path. Then using a rotational matrix i'm rotating the 2d object according the angle. However, I'm not achieving the desired results. Anyone can help me about this please?!

//This is the rendering of the 2D object swept along the path
 beginShape(QUAD_STRIP);
 for(int k = 0; k < path.length-1; k++){
   for(int i = 0; i < vertices.length; i++){
     float m1 = (path[k+1].y-path[k].y)/(path[k+1].x-path[k].x);
     float angle = (atan(m1));
     vertex((vertices[i].x*cos(angle) - vertices[i].y*sin(angle)) + path[k].x, (vertices[i].x*sin(angle) + vertices[i].y*cos(angle)) + path[k].y, path[k].x);
     vertex((vertices[i].x*cos(angle) - vertices[i].y*sin(angle)) + path[k].x, (vertices[i].x*sin(angle) + vertices[i].y*cos(angle)) + path[k].y, path[k+1].x);
   }
 }
 endShape();
Re: Creating the sweep function
Reply #1 - Mar 3rd, 2009, 1:46pm
 
Generally your calculation looks quite good to me. But I've seen one thing that might help. Try calculating the angle that way:

float angle = atan2((path[k+1].y-path[k].y), (path[k+1].x-path[k].x));

The atan2 function is the better way to get an angle, because it delivers all angles from -PI to PI, whereas atan only gives you angles between -PI/2 and PI/2. Therefore the result of atan is not unambiguous.

If it is still not working it would help to get a piece of code, that is runnable.
Page Index Toggle Pages: 1