110,535
社区成员
发帖
与我相关
我的任务
分享
public Point[] GetBezierPoint(Point p1, Point p4)
{
Point[] result = new Point[4];
result[0] = p1;
result[3] = p4;
// TODO...
//我这里需要的是p2,p3的坐标代数式.
//麻烦明示.
//result[1] = ?
//result[2] = ?
return result ;
}
static public PointF[] GetBezier(PointF s, PointF e)
{
const float distance = 3f;
MyPoint p1 = s, p4 = e;
MyPoint normal = (p4 - p1).Pert;
MyPoint t2 = p1 + (p4 - p1) * 0.25f + normal * distance;
MyPoint t3 = p1 + (p4 - p1) * 0.75f - normal * distance;
// t2 = 0.75^3*p1 + 3*0.75^2*0.25*p2 + 3*0.75*0.25^2*p3 + 0.25^3p4
// t3 = 0.25^3*p1 + 3*0.25^2*0.75*p2 + 3*0.25*0.75^2*p3 + 0.75^3p4
float a = 3 * 0.75f * 0.75f * 0.25f;
float b = 3 * 0.25f * 0.25f * 0.75f;
MyPoint m = t2 - p1 * (0.75f * 0.75f * 0.75f) - p4 * (0.25f * 0.25f * 0.25f);
MyPoint n = t3 - p1 * (0.25f * 0.25f * 0.25f) - p4 * (0.75f * 0.75f * 0.75f);
// a*p2 + b*p3 = m
// b*p2 + a*p3 = n
// (a+b)*(p2+p3) = m+n
// (a-b)*(p2-p3) = m-n
// 2*p2 = (m+n)/(a+b) + (m-n)/(a-b);
// 2*p3 = (m+n)/(a+b) - (m-n)/(a-b);
MyPoint p2 = ((m + n) / (a + b) + (m - n) / (a - b)) / 2;
MyPoint p3 = ((m + n) / (a + b) - (m - n) / (a - b)) / 2;
return new PointF[] { p1, p2, p3, p4 };
}
public class MyPoint
{
public float X { get; set; }
public float Y { get; set; }
public float Length { get { return (float)Math.Sqrt(this * this); } }
public MyPoint Pert { get { MyPoint r = this / this.Length; return new MyPoint() { X = r.Y, Y = -r.X }; } }
public static MyPoint operator +(MyPoint p1, MyPoint p2) { return new MyPoint() { X = p1.X + p2.X, Y = p1.Y + p2.Y }; }
public static MyPoint operator -(MyPoint p1, MyPoint p2) { return new MyPoint() { X = p1.X - p2.X, Y = p1.Y - p2.Y }; }
public static MyPoint operator *(MyPoint p1, float f) { return new MyPoint() { X = p1.X * f, Y = p1.Y * f }; }
public static MyPoint operator /(MyPoint p1, float f) { return new MyPoint() { X = p1.X / f, Y = p1.Y / f }; }
public static implicit operator PointF(MyPoint p1) { return new PointF(p1.X, p1.Y); }
public static implicit operator MyPoint(PointF p1) { return new MyPoint() { X = p1.X, Y = p1.Y }; }
public static float operator *(MyPoint p1, MyPoint p2) { return p1.X * p2.X + p1.Y * p2.Y; }