Can I solve for Y with bezier or cubic interpolation?

Started by
0 comments, last by Zipster 17 years, 1 month ago
I'm trying to draw 2D terrain using Bezier (and Cubic) interpolation and I'd like to find out the terrain height (eg. the curve's Y value) at each X point. Right now, I'm using this equation to generate my curve points:

function BezierInterpolate(const p1, p2, p3, p4: TPoint2; Step, Steps: Single): TPoint2; overload;
begin
  Result.x := Power(1 - Step / Steps, 3) * p1.X + 3 * Step / Steps *
              Power(1 - Step / Steps, 2) * p2.X + 3 *
              Power(Step / Steps, 2) * (1 - Step / Steps) * p3.X +
              Power(Step / Steps, 3) * p4.X;
  Result.y := Power(1 - Step / Steps, 3) * p1.Y + 3 * Step / Steps *
              Power(1 - Step / Steps, 2) * p2.Y + 3 *
              Power(Step / Steps, 2) * (1 - Step / Steps) * p3.Y +
              Power(Step / Steps, 3) * p4.Y;
end;
As you can see, you pass in four points (two "normal" points and two control points), the current step (eg. 20) and the total number of steps (eg. 200). What I'd like to do is have a function that lets me pass in an X coordinate and be returned a Y coordinate. After doing this for every X coordinate (eg. 0 -> 640) I'd have then is a list of all points on my curve. Also, every X coordinate would be an integer unlike the X value generated when using multiple steps (eg. 0 to 200 steps in-between points generates fractional X values). Would integer X values be OK or should I use 0.5 X increments? Thanks for any help!!
Advertisement
You have both X and Y as a function of the parameter t, i.e. X(t) and Y(t). What you want is Y(X). This can be done by inverting X(t) to get t(X), and plugging that into Y(t), which gives you Y(t(X)) = Y(X).

What I would do first, is find the intersection between X(t) (which is just a polynomial) and the horizontal line X = X', where X' is the input into the method. This is the same as finding the roots of X(t) - X' = 0. This will give you anywhere from 1 to 3 solutions for t, which you plug into Y(t). Make sure that the solutions for t are within the [0,1] range.

This topic is closed to new replies.

Advertisement