根据两点,获取贝塞尔曲线.

1dit 2011-05-27 11:34:44
小弟数学从来都是59分,所有请各位多多关照....

如图
http://hi.csdn.net/attachment/201105/27/148499_13064671916HT0.png

...全文
363 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
1dit 2011-05-28
  • 打赏
  • 举报
回复
@gomoku
3Q,我知道了.
1dit 2011-05-28
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 gomoku 的回复:]

把图贴出来后才发现是要约束曲线跑在半径为3的通道内:)
1楼的方法不能达成该要求,它只是强迫Bezier经过1/4和3/4处的两点,该两点距离直线的距离为3。
[/Quote]


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

gomoku 2011-05-27
  • 打赏
  • 举报
回复
Bezier曲线为:
B(t) = (1-t)^3*p1 + 3*(1-t)^2*t*p2 + 3*(1-t)*t^2*p3 + p4

假设p1和p4在x轴上,那么对于控制点p2(x2,y),p3(x3,-y),最高最低点将发生在以下函数的拐点上:
f(t) = 3*(1-t)^2*t + 3*(1-t)*t^2

求导:
f'(t) = 6t*t - 6t + 1

解得:
t2 = (6-Sqrt(12)/12
t3 = (6+Sqrt(12)/12

把一楼方法中的0.25(1/4处)和0.75(3/4处)换成t2和t3即可。
gomoku 2011-05-27
  • 打赏
  • 举报
回复
把图贴出来后才发现是要约束曲线跑在半径为3的通道内:)
1楼的方法不能达成该要求,它只是强迫Bezier经过1/4和3/4处的两点,该两点距离直线的距离为3。
gomoku 2011-05-27
  • 打赏
  • 举报
回复

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

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧