C # realizes Bezier three times curve __c#

Source: Internet
Author: User

Now learning graphics, using C # to achieve the Bezier three times curve;

Finally took time to tidy up the code, now put him out

------------------------------------

  public class Drawengecur
 {
       /*funtion: Draw Bezier Curves
         *input: (1) G:graphics objects for drawing concrete graphics; (2) Controlpoints: Single precision point type, control point coordinates
         * (3) PenColor, drawing the color of the graphic; (4) degree The number of times the Bezier Curve, (5) n the precision drawn, that is, the number of segments to be segmented
         *output:void
          * Call funtion: function getnextpoint to produce the coordinates of the next point
          */
        public  static void Bézier (Graphics g, Pointf[] controlpoints, Color pencolor, int degree, int n)
  {
      & nbsp;    
           //Paint the color of the brush
            Pen drawpen = new Pen (pencolor);

Place the horizontal and vertical coordinates of the control points into a one-dimensional array
int controlpointsnum = Controlpoints.length;
float[] coeff_x = new Float[controlpointsnum];
float[] coeff_y = new Float[controlpointsnum];
for (int i=0;i<controlpointsnum;i++)
{
Coeff_x[i] = controlpoints[i]. X
Coeff_y[i] = controlpoints[i]. Y
}


The value of t is between [0,1]
Double T = 0;
The value of the delt change
float delt = (float) (1.0/n);

           //First segment line start point is first control point
             PointF pointpre = controlpoints[0];
           //define current point
             PointF pointcur = new PointF ();

   for (int i=1;i <= n; i++)
   {
    t = t + delt;
               //Find the trailing coordinates of the current segmented line
                pointcur.x = Getnextpoint (degree,coeff_x,t);
    pointcur.y = Getnextpoint (degree,coeff_y,t);
    
                //Draw the line
                 G.drawline (drawpen,pointpre,pointcur);
    
                //Set the starting point for the next segment, which is the end point of the current segmented line
                 pointpre = pointcur;

}

}


/*funtion: The coordinates of the next point in the segment, and the horizontal and ordinate methods are the same.
*input: (1) degree indicates the number of Bezier curves (2) Coeff the value of all horizontal or ordinate points (3) T: T in the expression, the current segment value
*output: The tail coordinate of the current segmented line
* Called funtion: Called by Bézier
*/
private static float getnextpoint (int degree, float[] Coeff, double t)
{
The parameter Coeff is copied to the coeffxory because the Coeff parameter changes
int coeffnumb = Coeff. Length;
float[] coeffxory = new Float[coeffnumb];
for (int i=0;i<coeffnumb;i++)
{
Coeffxory[i] = Coeff[i];
}


Define the difference of T
float Treverse = (float) (1.0-T);
Iterative evaluation based on de Casteljau algorithm
for (int r=1;r<=degree;r++)
{
for (int i =0;i<=degree-r;i++)
{
Coeffxory[i] = (float) (treverse*coeffxory[i]+t*coeffxory[i+1]);
}
}

The first parameter is the value we require.
That is: C (t) = (1-T) ^3*p0 + 3t (1-t) ^2*p1 + 3t^2 (1-t) *P2 + t^3*p3
return coeffxory[0];

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.