1 2 Previous Next 16 Replies Latest reply on Nov 23, 2006 8:10 AM by 807599

    Help with Bezier curve algorithm, n amount of points?

    807599
      Hello,

      I have tried to follow my graphics book but its way to complicated. The book uses openGL. I want to do it in java.

      I need to use n amount of control points. I am going to use an applet, and basically hard code the control points in the program. Nothing fancy.



      I tried to change this c code to java format, but I cant get it to work at all.
      Does any one have an algorithm already written, or help me improve this one.

      thanks.
      /*
      Code to generate a cubic Bezier curve
      */
      
      typedef struct
      {
          float x;
          float y;
      }
      Point2D;
      
      /*
      cp is a 4 element array where:
      cp[0] is the starting point, or P0 in the above diagram
      cp[1] is the first control point, or P1 in the above diagram
      cp[2] is the second control point, or P2 in the above diagram
      cp[3] is the end point, or P3 in the above diagram
      t is the parameter value, 0 <= t <= 1
      */
      
      Point2D PointOnCubicBezier( Point2D* cp, float t )
      {
          float   ax, bx, cx;
          float   ay, by, cy;
          float   tSquared, tCubed;
          Point2D result;
      
          /* calculate the polynomial coefficients */
      
          cx = 3.0 * (cp[1].x - cp[0].x);
          bx = 3.0 * (cp[2].x - cp[1].x) - cx;
          ax = cp[3].x - cp[0].x - cx - bx;
              
          cy = 3.0 * (cp[1].y - cp[0].y);
          by = 3.0 * (cp[2].y - cp[1].y) - cy;
          ay = cp[3].y - cp[0].y - cy - by;
              
          /* calculate the curve point at parameter value t */
              
          tSquared = t * t;
          tCubed = tSquared * t;
              
          result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
          result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
              
          return result;
      }
      
      /*
       ComputeBezier fills an array of Point2D structs with the curve   
       points generated from the control points cp. Caller must 
       allocate sufficient memory for the result, which is 
       <sizeof(Point2D) numberOfPoints>
      */
      
      void ComputeBezier( Point2D* cp, int numberOfPoints, Point2D* curve ) {
          float   dt;
          int   i;
      
          dt = 1.0 / ( numberOfPoints - 1 );
      
          for( i = 0; i < numberOfPoints; i++)
              curve[i] = PointOnCubicBezier( cp, i*dt );
      }
        • 2. Re: Help with Bezier curve algorithm, n amount of points?
          807599
          i bet google knows!

          http://64.233.161.104/search?q=cache:7PVVDJ5XUmQJ:www.people.nnov.ru/fractal/Splines/Bezier.htm+Bezier+curve+algorithm+java&hl=en&gl=us&ct=clnk&cd=1
          • 3. Re: Help with Bezier curve algorithm, n amount of points?
            807599
            I have searched google for days for an easy way that makes sense to me. thats why I posted here. I know how to do it by hand, just cant figure out logically to put it as code.
            • 4. Re: Help with Bezier curve algorithm, n amount of points?
              807599
              i dont know anyhting about the Bezier curve algorithm, but the link i gave you has source code that does it. what is wrong with the source it provides?
              • 5. Re: Help with Bezier curve algorithm, n amount of points?
                807599
                You say you want to do one thing and then ask for help in translating a piece of code which does something different. Do you want to do Bezier curves with n control points, or find n points on a cubic (i.e. 4 control point) curve?
                • 6. Re: Help with Bezier curve algorithm, n amount of points?
                  807599
                  sorry about that. Yeah, I want to write up a hard coded app that can take n amount of control points.

                  just the normal bezier curve. We are supposed to test with 4 control points then 5, 6, 7, 8, 9. I guess he wants us to see how smoother it looks.

                  thanks again.
                  • 7. Re: Help with Bezier curve algorithm, n amount of points?
                    807599
                    Please give your working definition of a control point, because I'm still not certain what you want - it seems that you may be using the term differently from all the literature.
                    • 8. Re: Help with Bezier curve algorithm, n amount of points?
                      807599
                      here is what I need to do..

                      "Use the Bezier curve algortihm to approximate a curve for the control points. Your driver program should draw the curves for many different cases of control points"

                      I am using the term incorrectly--I guess.

                      thanks again.
                      • 9. Re: Help with Bezier curve algorithm, n amount of points?
                        807599
                        Synthesising your various posts then, am I correct in understanding the following to be your task?

                        1. Write code to draw an approximation to a cubic Bezier curve by evaluating the positions of n points equally spaced in parameter space.
                        2. Write test cases demonstrating it for various numbers of interpolated points and various interesting/degenerate curves.

                        If I am, then the code you posted will do half of step 1 with very minor changes. Replace the typedef struct with a class having two fields, the pointers cp with Point2D[]s having 4 elements, the pointer curve with a Point2D[], and the argument numberOfPoints with curve.length. Allocate result a new Point2D() rather than merely declaring it, and I think the rest works.

                        For the rendering, which would be the actual thing you claim in post 1 to be a problem (being the part which OpenGL would have played), a GeneralPath on which you call moveto followed by a series of lineto will be perfectly suited. In fact you could then optimise the code to ditch curve and simply start with a new GeneralPath().
                        • 10. Re: Help with Bezier curve algorithm, n amount of points?
                          807599
                          yes you are correct. I tried to make the struct a class but the interpreter doesn't like the point2d[].

                          I think my problem is, How do I use a class within a class.

                          is there an easier way to do this than from the code above?

                          thanks
                          • 11. Re: Help with Bezier curve algorithm, n amount of points?
                            807599
                            ok, I have this written. I have one error left, that I am not sure how to handle.

                            this is in the method bezier(.....)`
                            variable bezCurvePt might not have been initialized
                            computeBezPt(u,bezCurvePt,nCtrlPts, ctrlPts, c);
                            ^
                            1 error

                            How should I change it?

                            import java.applet.*;
                            import java.awt.*;
                            import java.lang.Math;
                            import java.awt.Point;
                            import java.awt.Graphics;
                            import java.io.*;
                            
                            
                            public class Bezier extends Applet
                            {
                            
                                public class wcPt3D
                                {
                                    double x, y, z;
                            
                            
                                    public wcPt3D(double xIN,double yIN, double zIN)
                                    {
                                        x = xIN;
                                        y=yIN;
                                        z=zIN;
                                    }
                                }
                            
                            
                            
                                public void init()
                                {
                                }
                            
                                public void paint(Graphics g)
                                {
                            
                                    int nCtrlPoints = 4;
                                    int nBezCurvePts = 1000;
                            
                                    //wcPt3D ctrlPts[] = {{-40.0, -40.0, 0.0}, {-10.0, 200.0, 0.0},
                                    //                                   { 10.0, 200.0, 0.0}, { 40.0, 40.0 , 0.0}};
                            
                                    wcPt3D [] ctrlPts = {new wcPt3D(-400.0,-40.0,0.0), new wcPt3D(-100.0, 200.0, 0.0), new wcPt3D( 100.0, 200.0, 0.0),
                                                         new wcPt3D( 400.0, 400.0 , 0.0)};
                            
                            
                                    bezier(ctrlPts, nCtrlPoints, nBezCurvePts, g);
                            
                                }
                            
                            
                              public void binomialCoeffs(int n, int[] c)
                                {
                                    int k, j;
                            
                                    for(k = 0; k<=n; k++)
                                        {
                                            //compute n!/k!(n-k)!)
                            
                                            c[k] = 1;
                                            for(j = n; j>= k+1; j--)
                                                c[k] *=j;
                                            for(j = n -k; j>=2; j--)
                                                c[k] /=j;
                                        }
                                }
                            
                                public void computeBezPt(float u, wcPt3D  bezPt, int nCtrlPts, wcPt3D[]  ctrlPts, int [] c)
                                {
                            
                                    int k;
                                    int n = nCtrlPts - 1;
                                    double bezBlendFcn;
                            
                                    for(k=0; k< nCtrlPts; k++)
                                        {
                                            bezBlendFcn = c[k] * Math.pow(u, k) * Math.pow(1-u, n-k);
                                            bezPt.x += ctrlPts[k].x * bezBlendFcn;
                                            bezPt.y += ctrlPts[k].y * bezBlendFcn;
                                        }
                                }
                            
                                public void bezier(wcPt3D[] ctrlPts, int nCtrlPts, int nBezCurvePts, Graphics g)
                                {
                                    wcPt3D bezCurvePt;
                                    float u;
                                    int [] c;
                                    int  k;
                            
                                    c = new int[nCtrlPts];
                            
                                    binomialCoeffs(nCtrlPts -1, c);
                            
                                    for(k = 0; k<= nBezCurvePts; k++)
                                        {
                                            u = (float)k / (float)nBezCurvePts;
                                            computeBezPt(u,bezCurvePt,nCtrlPts, ctrlPts, c);
                                            g.fillRect((int)bezCurvePt.x,(int)bezCurvePt.y, 1, 1);
                            
                                        }
                            
                            
                            
                                }
                            
                            
                            
                            
                            
                            }//end class applet Bezier
                            • 12. Re: Help with Bezier curve algorithm, n amount of points?
                              807599
                              variable bezCurvePt might not have been initialized
                              computeBezPt(u,bezCurvePt,nCtrlPts, ctrlPts, c);
                              ^
                              1 error
                              You need to construct one with default values before you use it as an
                              argument. Or, alternatively, have computeBezPt() construct one and return it.
                              • 13. Re: Help with Bezier curve algorithm, n amount of points?
                                807599
                                The book has it the exact way I do and somehow it works--the book coded with opengl though.

                                I am not sure what you mean. I gave the class values in the paint method up top.
                                • 14. Re: Help with Bezier curve algorithm, n amount of points?
                                  807599
                                  The book has it the exact way I do and somehow it works--the book coded with opengl though.
                                  1. No it doesn't.
                                  2. OpenGL has nothing to do with it - all that handles is rendering. The issue here is C struct type vs Java reference type.
                                  3. I already gave you a complete list of the changes you need to make to the C code.
                                  1 2 Previous Next