# Can I solve for Y with bezier or cubic interpolation?

This topic is 3958 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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!!