绘制贝塞而曲线
辅助函数
private Pen redPen = new Pen(Color.Red, 2); public static PointF Multi(Point p, float fac) { var pf = new PointF(); pf.X = p.X * fac; pf.Y = p.Y * fac; return pf; } public static PointF Adds(params PointF[] ps) { var pf = new PointF(); foreach (var it in ps) { pf.X += it.X; pf.Y += it.Y; } return pf; } public static float Pow(double v, double n) { return (float)Math.Pow(v,n); }
按参数方程计算点
var p0 = new Point(180, 10); var p1 = new Point(140, 30); var p2 = new Point(180, 360); var p3 = new Point(150, 400); //dc.DrawBezier(redPen, p0,p1,p2,p3); var points4Bezier = new PointF[100]; for (int i = 0; i < 100; i++) { var t=0.01 * i; var p =Adds( Multi(p0, Pow((1 - t), 3)) , Multi(p1, 3f * (float)t * Pow((1 - t),2) ) , Multi(p2, Pow(t, 2) * 3f * (float)(1 - t)) , Multi(p3, Pow(t, 3)) ); points4Bezier[i] = p; } dc.DrawCurve(redPen, points4Bezier);