Thread: Bezier Curves where y = f(x)

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    204

    Question Bezier Curves where y = f(x)

    It seems that Bezier curves are usually defined parametrically where

    y = f1(t)
    x = f2(t)

    I'm trying to code non-parametric Bezier curves. I would like to have y explicitly in terms of x. Something like

    y = f(x)

    Has anyone seen anything on Bezier curves written in this form?

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    If you plan on writing it in 3D you need to have at least 2 variables that the function depends on. f(x,t) = y or something of the like. You can't draw a 3D graph with no concept of time, unless maybe you shift everything with respect to the same time, but even then it is technically having 2 variables. I didn't make bezier curves exactly, but I made a parametric equation plotter. If you want to take a look at it the link is in my signature.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    Thanks, but I'm just trying to create 2D curves.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps not the easiest reading ever, but:
    http://en.wikipedia.org/wiki/Bezier_curves

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    I've as far as the Wikipedia page. I have an idea of how to do this, but I have to find the roots of a cubic equation, which is something I'm not looking forwards to doing.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You can't do y = f(x) for bezier curves because for a given x value you can have multiple y values.

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    There are several types of bezier curves.

    After examining this on wikipedia and seeing the 'classroom' formula there is actually a simpler solution when it comes to programming them.

    If you notice a bezier curve with 3 control points is quite simple. Take arbitrary control points A and C and an arbitrary control point B. The curve is created by first finding a point between A and B and finding a point between B and C using linear interpolation. Now draw a line between those points and interpolate on it the same distance as you did to find the two intermediate points. You need some type of time delta here though in order to draw the final curve.

    Code:
    D3DXVECTOR2 A;
    D3DXVECTOR2 B;
    D3DXVECTOR2 C;
    
    D3DXVECTOR2 betweenAB;
    D3DXVECTOR2 betweenBC;
    
    float lerp = 0.0f;
    
    while (lerp <= 1.0f)
    {
      D3DXVec2Lerp(&betweenAB,A,B,lerp);
      D3DXVec2Lerp(&betweenBC,B,C,lerp);
    
      D3DXVec2Lerp(&curvePoint,&betweenAB,&betweenBC,lerp);
    
      lerp += 0.2f;
    }
    A 1D linear interpolation is a simple function:

    Code:
    float lerp(float start,float end,float lerp)
    {
       return start + lerp * (end - start);
    }
    I used D3DX here because it has overloaded the various operators so they work on 2D, 3D, and 4D vectors.
    The bezier curve will trace out over time as the line between the computed points at time T between A and B and B and C changes.
    You can extend this to cubic bezier curves as well and pretty much any order of curve and it just requires more linear interpolations.

    I have never tried the 'school room' equations for the curves in code so I have no data as to whether or not the tried and true formulas are slower or faster in code. I would venture to guess the series of linear interpolations would win in the end. I use this in one version of my camera system that computes a new 'control point' between two camera positions. I then use this to run the camera's path along the bezier curve created by the 3 points.

    But as has been said already without time there are no Bezier curves. There is no way to trace a curve from A to B without time and in fact there is no way to trace a line from A to B without time. A line is a simple bilinear interpolation between A and B and a curve with 3 points is 3 bilinear interpolations along 3 lines using the same coefficient for interpolation.
    Last edited by VirtualAce; 12-26-2008 at 12:35 PM.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Here is a page that describes basically what Bubba said: http://graphics.cs.ucdavis.edu/educa...ier-Curve.html

    The page does it for the t=0.5 condition but it can be done for at 0<= t <= 1.


    When I did Bezier curves I found the equations much easier to deal with. But of course for the programs I just used the OpenGL function to create them for me.

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Interesting. I did not realize that my approach was an actual approach used elsewhere in graphics. I derived my approach from looking at the animations on wikipedia which led me to the overall idea since I am extremely familiar with interpolations.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by thetinman View Post
    It seems that Bezier curves are usually defined parametrically where

    y = f1(t)
    x = f2(t)

    I'm trying to code non-parametric Bezier curves. I would like to have y explicitly in terms of x. Something like

    y = f(x)

    Has anyone seen anything on Bezier curves written in this form?
    Sure.. think about what happens if f2(t) = t.

    You can rotate the parametric form until f2(t) = a*t+b, but this is not always possible because the curve might become multi-valued for some values of x, which is pretty much why these things are specified parametrically in the first place.

    EDIT: More generally, you can solve x = f2(t) for t in terms of x, then substitute this into f1(t). However your result will include roots which might be multi-valued.
    Last edited by brewbuck; 12-26-2008 at 11:58 PM.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    The curve I need won't be multi-valued for a given value of x. I will need to have at least a cubic Bezier curve though, which means that f2(t) != t.
    Last edited by thetinman; 12-27-2008 at 10:18 AM. Reason: spelling

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    Quote Originally Posted by Bubba View Post
    There are several types of bezier curves.
    Do you mean different types aside from quadratic, cubic, quartic, etc? Do they have names?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Are B-splines composed of Bezier curves?
    By thetinman in forum Game Programming
    Replies: 4
    Last Post: 01-28-2009, 11:21 AM
  2. What's the purpose of Bezier Curves?
    By thetinman in forum Game Programming
    Replies: 5
    Last Post: 10-06-2008, 08:38 PM
  3. Collision detection between bezier and ball.
    By CornedBee in forum Game Programming
    Replies: 1
    Last Post: 04-27-2006, 12:49 PM
  4. bezier curves
    By linuxdude in forum Game Programming
    Replies: 9
    Last Post: 12-23-2004, 02:53 AM
  5. My view on the GeForce FX
    By frenchfry164 in forum Tech Board
    Replies: 10
    Last Post: 05-13-2003, 06:23 PM