Jump to content

Calculating coordinates of peaks of cubic Bézier curves?


THX-1138

Recommended Posts

I never studied the curve algorithms; I just use 'em. So I really have no idea how to answer this question.

 

Once again working in SVG. When using the cubic Bézier curve commands, I want to find out the coordinates of the actual peaks. For instance,

c 3.75,20.0 5.00,-20.00 8.75,0.00

approximates (!) a one-cycle sine curve. I want to be able to figure out the curve's minimum and maximum (y-axis) values.

 

How can I do this?

 

Thanks!

Link to comment
Share on other sites

  • 2 weeks later...

Since it relates explicitly to SVG, I also asked this question i the SVG developers group on Yahoo!. Someone replied there, and I'm copying the response here for completeness and eddy-fication of anyone wandering by this topic in the future.

 

From Frank Bruder:

 

That's quite simple. You've got a cubic Bezier curve

c x1,y1 x2,y2 x3,y3

 

This defines a Mapping

t -> (x(t),y(t)), 0 <= t <= 1

where

x(t) = 3*x1*t*(1-t)^2 + 3*x2*t^2*(1-t) + x3*t^3

y(t) = 3*y1*t*(1-t)^2 + 3*y2*t^2*(1-t) + y3*t^3

 

For a value t which corresponds to a peak of the curve the derivative

y'(t) must be zero.

 

y'(t) = 3*(y1*(1-t)^2 + 2*(y2-y1)*t*(1-t) + (y3-y2)*t^2)

 

To find the zeros of this function first let

 

c := 3*y1 -3*y2 +y3.

 

There are two cases to consider.

 

I: c = 0

If c = 0 then the function y is quadratic and has not more than one

local extremum. In the example you gave this is clearly not the case,

but let's go for a general solution.

If in addition to c = 0 also

y2 = 2*y1

then y is in fact linear and has _no local extrema_.

Otherwise y has it's extremum in

 

t = y1 / (2*y1-y2) / 2,

 

for which you can compute (x(t),y(t)) from the formulas above.

If y2 < 2*y1 this is a maximum, if y2 > 2*y1 it is a minimum.

But only if

0 <= t <= 1

the point is on the displayed curve. Otherwise it's on the polynomial

continuation of the curve.

 

II: c != 0

For non-zero c let

 

b = 2*y1 - y2.

 

Now there are three cases to consider.

 

II.1: b^2 < y1*c

In this case y has _no local extrema_, (or, to be precise, none in

the real domain.)

 

II.2: b^2 = y1*c

In this case y has one saddle point at t = b/c but _no local extrema_.

 

II.3: b^2 > y1*c

In this case y has exactly two local extrema, one of which is a

maximum and one of which is a minimum. They are at

 

t1 = (b + sqrt(b^2-y1*c))/c

and

t2 = (b - sqrt(b^2-y1*c))/c.

 

For these you can compute the coordinates (x(t),y(t)) from the

formulas given above. To determine which one's the minimum and which

the maximum compare y(t1) to y(t2).

Again, only t-values 0 <= t <= 1 lie on the actual curve.

 

Of course, the coordinates x, y are to be considered relative to the

start point of the given path segment.

 

 

I didn't take the time to calculate any examples, so I don't claim

that no error has slipped in.

 

Cheers

Frank

 

I'm not sure I agree that it's "quite simple," but there it is.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.