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];
}
}