Вопрос

The Question

Okay so basically what I'm trying to do, is calculating the Y location on a Cubic Curve / Bezier Curve / Spline when the X location is given.

I've searched everywhere on Stack Overflow and Google and I could find anything that actually worked!

The Curve Points

x1 = 50d;
y1 = 400d / 2d + 100d;

x2 = 400d;
y2 = 400d / 2d + 100d;

x3 = 600d - 400d;
y3 = 400d / 2d - 100d;

x4 = 600d - 50d;
y4 = 400d / 2d - 100d;

The reason that I calculate "600 - 400" and I don't just write "200" is because in my code "600" is actually the width of the window, which the Cubic Curve is rendered in. Thereby it actually says "width - 400" in my code.

So the following code after, can calculate the X & Y on a Cubic Curve when T is given!

t = 0.5d;

cx = 3d * (x2 - x1);
cy = 3d * (y2 - y1);

bx = 3d * (x3 - x2) - cx;
by = 3d * (y3 - y2) - cy;

ax = x4 - x1 - cx - bx;
ay = y4 - y1 - cy - by;

point_x = ax * (t * t * t) + bx * (t * t) + cx * t + x1;
point_y = ay * (t * t * t) + by * (t * t) + cy * t + y1;

So again, what I'm trying to calculate is the Y location of a Curve when you know an X location. But the only thing that I'm able to calculate is both an X & Y location on the Curve when T is given.

This is my first post, so if something isn't written 100% correctly, I apologize!

Это было полезно?

Решение

A cubic curve can have multiple 'y' values for an 'x' value, so you're going to have to perform root finding, after rotating the cubic curve so that it's aligned with the x/y axes. http://pomax.github.io/bezierinfo/#intersections covers this concept, but the idea is this:

  1. take your curve, defined by points {p1,p2,p3,p4}, and take your line at x=X, defined by points {p5,p6} (where p5 is some coordinate (x,...) and p6 is another coordinate(x,...). The important part is that it's a vertical line, so both points have the same x value).

  2. translate and rotate your cubic curve and your line, together, so that your line becomes horizontal, at new height y = 0.

  3. you can now perform cubic root finding on the curve's y function. This may generate 0, 1, 2, or 3 distinct t values.

  4. for each of these t values: in your normal, unrotated curve, compute the (x,y) coordinate given that t value. You will now have all the y values for a given x (all points will have the same x value, too).

In the article linked, have a look at the source for the cubic curve/line example, and to see how the rotation/rootfinding works, see the BezierCurve align(Point start, Point end) {... and float[] findAllRoots(int derivative, float[] values) {... functions. (especially note that after rotation, we're only finding the roots for the y-function! the x-function has become irrelevant for what we want to do)

Другие советы

If your formulas are correct, theoretically one might use Formula for cubic function roots to express t dependency from x. Then from x you find t, from t you find y.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top