gpt4 book ai didi

wpf - 如何在 WPF 的 StreamGeometry 中绘制完整的椭圆?

转载 作者:行者123 更新时间:2023-12-04 13:03:21 35 4
gpt4 key购买 nike

StreamGeometryContext 中唯一的方法似乎与椭圆有关的是 ArcTo方法。不幸的是,它非常适合连接线而不是绘制椭圆。

特别地,弧的位置由起点和终点确定。对于完整的椭圆,两者显然重合,确切的方向变得不确定。

到目前为止,我发现绘制以 100,100 为中心、大小为 10,10 的椭圆的最佳方法是这样的:

using (var ctx = geometry.Open())
{
ctx.BeginFigure(new Point(100+5, 100), isFilled: true, isClosed: true);
ctx.ArcTo(
new Point(100 + 5*Math.Cos(0.01), 100 + 5*Math.Sin(0.01)), // need a small angle but large enough that the ellipse is positioned accurately
new Size(10/2, 10/2), // docs say it should be 10,10 but in practice it appears that this should be half the desired width/height...
0, true, SweepDirection.Counterclockwise, true, true);
}

这非常难看,并且还留下了一个小的“平坦”区域(尽管在正常缩放级别下不可见)。

我还能如何使用 StreamGeometryContext 绘制完整的椭圆?

最佳答案

正如您所指出的,ArcTo 无法绘制完整的椭圆。事实上,当您试图减少“平坦”区域时,它在数值上变得不稳定。另一个考虑因素是在现代硬件上绘制圆弧比绘制贝塞尔曲线要慢。这个最现代的系统使用四个贝塞尔曲线来近似椭圆而不是绘制真正的椭圆。

您可以看到 WPF 的 EllipseGeometry 通过执行以下代码、中断 DrawBezierFigure 方法调用并检查调试器中的 PathFigure 来执行此操作:

using(var ctx = geometry.Open())
{
var ellipse = new EllipseGeometry(new Point(100,100), 10, 10);
var figure = PathGeometry.CreateFromGeometry(ellipse).Figures[0];
DrawBezierFigure(ctx, figure);
}

void DrawBezierFigure(StreamGeometryContext ctx, PathFigure figure)
{
ctx.BeginFigure(figure.StartPoint, figure.IsFilled, figure.IsClosed);
foreach(var segment in figure.Segments.OfType<BezierSegment>())
ctx.BezierTo(segment.Point1, segment.Point2, segment.Point3, segment.IsStroked, segment.IsSmoothJoin);
}

上面的代码是一种将高效椭圆绘制到 StreamGeometry 中的简单方法,但是是非常特殊的代码。在实际实践中,我使用了几种通用的扩展方法来将任意几何图形绘制到 StreamGeometryContext 中,因此我可以简单地编写:
using(var ctx = geometry.Open())
{
ctx.DrawGeometry(new EllipseGeometry(new Point(100,100), 10, 10));
}

下面是 DrawGeometry 扩展方法的实现:
public static class GeometryExtensions
{
public static void DrawGeometry(this StreamGeometryContext ctx, Geometry geo)
{
var pathGeometry = geo as PathGeometry ?? PathGeometry.CreateFromGeometry(geo);
foreach(var figure in pathGeometry.Figures)
ctx.DrawFigure(figure);
}

public static void DrawFigure(this StreamGeometryContext ctx, PathFigure figure)
{
ctx.BeginFigure(figure.StartPoint, figure.IsFilled, figure.IsClosed);
foreach(var segment in figure.Segments)
{
var lineSegment = segment as LineSegment;
if(lineSegment!=null) { ctx.LineTo(lineSegment.Point, lineSegment.IsStroked, lineSegment.IsSmoothJoin); continue; }

var bezierSegment = segment as BezierSegment;
if(bezierSegment!=null) { ctx.BezierTo(bezierSegment.Point1, bezierSegment.Point2, bezierSegment.Point3, bezierSegment.IsStroked, bezierSegment.IsSmoothJoin); continue; }

var quadraticSegment = segment as QuadraticBezierSegment;
if(quadraticSegment!=null) { ctx.QuadraticBezierTo(quadraticSegment.Point1, quadraticSegment.Point2, quadraticSegment.IsStroked, quadraticSegment.IsSmoothJoin); continue; }

var polyLineSegment = segment as PolyLineSegment;
if(polyLineSegment!=null) { ctx.PolyLineTo(polyLineSegment.Points, polyLineSegment.IsStroked, polyLineSegment.IsSmoothJoin); continue; }

var polyBezierSegment = segment as PolyBezierSegment;
if(polyBezierSegment!=null) { ctx.PolyBezierTo(polyBezierSegment.Points, polyBezierSegment.IsStroked, polyBezierSegment.IsSmoothJoin); continue; }

var polyQuadraticSegment = segment as PolyQuadraticBezierSegment;
if(polyQuadraticSegment!=null) { ctx.PolyQuadraticBezierTo(polyQuadraticSegment.Points, polyQuadraticSegment.IsStroked, polyQuadraticSegment.IsSmoothJoin); continue; }

var arcSegment = segment as ArcSegment;
if(arcSegment!=null) { ctx.ArcTo(arcSegment.Point, arcSegment.Size, arcSegment.RotationAngle, arcSegment.IsLargeArc, arcSegment.SweepDirection, arcSegment.IsStroked, arcSegment.IsSmoothJoin); continue; }
}
}
}

另一种选择是自己计算点。通过将控制点设置为半径的 (Math.Sqrt(2)-1)*4/3,可以找到椭圆的最佳近似值。因此,您可以按如下方式显式计算点并绘制贝塞尔曲线:
const double ControlPointRatio = (Math.Sqrt(2)-1)*4/3;

var x0 = centerX - radiusX;
var x1 = centerX - radiusX * ControlPointRatio;
var x2 = centerX;
var x3 = centerX + radiusX * ControlPointRatio;
var x4 = centerX + radiusX;

var y0 = centerY - radiusY;
var y1 = centerY - radiusY * ControlPointRatio;
var y2 = centerY;
var y3 = centerY + radiusY * ControlPointRatio;
var y4 = centerY + radiusY;

ctx.BeginFigure(new Point(x2,y0), true, true);
ctx.BezierTo(new Point(x3, y0), new Point(x4, y1), new Point(x4,y2), true, true);
ctx.BezierTo(new Point(x4, y3), new Point(x3, y4), new Point(x2,y4), true, true);
ctx.BezierTo(new Point(x1, y4), new Point(x0, y3), new Point(x0,y2), true, true);
ctx.BezierTo(new Point(x0, y1), new Point(x1, y0), new Point(x2,y0), true, true);

另一种选择是使用两个 ArcTo 调用,但正如我之前提到的那样效率较低。如果您想这样做,我相信您可以弄清楚这两个 ArcTo 调用的详细信息。

关于wpf - 如何在 WPF 的 StreamGeometry 中绘制完整的椭圆?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2979834/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com