你得到的線是三個不同的貝塞爾曲線的結合 - 每個組的三個點。 (每一個「Bezier段」?)
如果你想要一個平滑的曲線,你需要將你的9點(或更多)點作爲點的單個集合(單個「Bezier段」?),而不是三點組。
編輯︰顯然BezierSegment
只有支持三點,所以難怪這不起作用。即使'PolyBezierSegment'只是給貝塞爾細分的集合,而不是一個光滑的貝塞爾...
因此,既然WPF不給你任何有用的東西,我使用數學here敲了一些東西。這是一個數字解決方案,但它似乎是即使有足夠的積分,好看,光滑漂亮的高性能:
PolyLineSegment GetBezierApproximation(Point[] controlPoints, int outputSegmentCount)
{
Point[] points = new Point[outputSegmentCount + 1];
for (int i = 0; i <= outputSegmentCount; i++)
{
double t = (double)i/outputSegmentCount;
points[i] = GetBezierPoint(t, controlPoints, 0, controlPoints.Length);
}
return new PolyLineSegment(points, true);
}
Point GetBezierPoint(double t, Point[] controlPoints, int index, int count)
{
if (count == 1)
return controlPoints[index];
var P0 = GetBezierPoint(t, controlPoints, index, count - 1);
var P1 = GetBezierPoint(t, controlPoints, index + 1, count - 1);
return new Point((1 - t) * P0.X + t * P1.X, (1 - t) * P0.Y + t * P1.Y);
}
利用這一點,
private void Grid_Loaded(object sender, RoutedEventArgs e)
{
Point[] points = new[] {
new Point(0, 200),
new Point(0, 0),
new Point(300, 0),
new Point(350, 200),
new Point(400, 0)
};
var b = GetBezierApproximation(points, 256);
PathFigure pf = new PathFigure(b.Points[0], new[] { b }, false);
PathFigureCollection pfc = new PathFigureCollection();
pfc.Add(pf);
var pge = new PathGeometry();
pge.Figures = pfc;
Path p = new Path();
p.Data = pge;
p.Stroke = new SolidColorBrush(Color.FromRgb(255, 0, 0));
((Grid)sender).Children.Add(p);
}
給
我看到有這個數字是很自然的,你可以發佈一個具有所需曲線的鏈接嗎? –
或者,也許你想繪製一個單獨的部分與7個控制點? –