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 › Getting Perpendicular
Page Index Toggle Pages: 1
Getting Perpendicular (Read 1237 times)
Getting Perpendicular
Apr 17th, 2007, 5:21pm
 
I want to be able to draw a line perpendicular ("normal") to a Bezier curve at a given point along it. The bezierTangent() language example includes a function that seems to do what I want:

Code:
bezier(85, 20, 10, 10, 90, 90, 15, 80);
stroke(255, 102, 0);
int steps = 16;
for (int i = 0; i <= steps; i++) {
float t = i / float(steps);
float x = bezierPoint(85, 10, 90, 15, t);
float y = bezierPoint(20, 10, 90, 80, t);
float tx = bezierTangent(85, 10, 90, 15, t);
float ty = bezierTangent(20, 10, 90, 80, t);
float a = atan2(ty, tx);
a += PI/2.0;
line(x, y, cos(a)*8 + x, sin(a)*8 + y);
}

And it does draw nice perpendicular lines along the specific s-curve created in the example, but when I alter the curve to nearly any other shape the lines are almost never normal. You can play with this in the interactive example at:
http://morrisdesign.com/projects/bezier_normals

Is there a different function I should be using The geometry seems right to me, so I'm hoping someone can straighten me out.
Re: Getting Perpendicular
Reply #1 - Apr 18th, 2007, 8:14pm
 
Funny, I had missed that function even existed in the reference!  I had written something similar to create lathed surfaces around a curve.  I'm sort of pulling it out of context here, but it appears to work, give it a try (as a replacement for bezierTangent()):

Quote:

public float beztan(float a, float b, float c, float d, float t) {
 return 3*t*t * (-a+3*b-3*c+d) +
          6*t * (a-2*b+c) +
            3 * (-a+b);
}



P.S.:  you can flip the tangent into normal without all that atan2/sin/cos.  In your code it might look something like this:

Code:

float len = sqrt(tx*tx+ty*ty);
line(x,y, x+ty/len*8, y-tx/len*8);
Re: Getting Perpendicular
Reply #2 - Apr 19th, 2007, 12:11am
 
Dave, that is *exactly* what I was hoping for. Your custom function computes the x and y tangents in a way that gives the normals I want, and the other code snippet is more elegant. I've updated my example sketch to show both methods side-by-side for comparison: http://morrisdesign.com/projects/bezier_normals. I hope this will be helpful to anyone working the same issue.

Thanks for the excellent help.
Re: Getting Perpendicular
Reply #3 - Jan 22nd, 2008, 9:39am
 
how can I get the Perpendicular of a point on a curve which is generated through the curve() function?
Re: Getting Perpendicular
Reply #4 - Jan 23rd, 2008, 1:56am
 
I just looked at the example you posted. Very nice. I would like to correct a minor error. The fact that the single Bezier spline has two bends in it means it is a cubic, NOT a quadratic spline.
Page Index Toggle Pages: 1