如何使用 InkStroke 绘制圆?

How can I draw a Circle with InkStroke?

我有一个 InkCanvas。我需要将 Circle 绘制为 InkStroke。

I know, i can a Circle or Ellipse draw with InkAnalyzer, but i need that Circle as InkStroke in InkCanvas, not in Canvas and i don't want to the protractor use.

我需要一个正确的圆形画。

对于我这段代码开发的直线;

private void StrokeInput_StrokeEnded(InkStrokeInput sender, PointerEventArgs args)
{
    List<InkPoint> points = new List<InkPoint>();
    InkStrokeBuilder builder = new InkStrokeBuilder();


    InkPoint pointOne = new InkPoint(new Point(line.X1, line.Y1), 0.5f);
    points.Add(pointOne);
    InkPoint pointTwo = new InkPoint(new Point(line.X2, line.Y2), 0.5f);
    points.Add(pointTwo);

    InkStroke stroke = builder.CreateStrokeFromInkPoints(points, System.Numerics.Matrix3x2.Identity);
    InkDrawingAttributes ida = inkCanvas.InkPresenter.CopyDefaultDrawingAttributes();
    stroke.DrawingAttributes = ida;
    inkCanvas.InkPresenter.StrokeContainer.AddStroke(stroke);

}

private void StrokeInput_StrokeContinued(InkStrokeInput sender, PointerEventArgs args)
{
    line.X2 = args.CurrentPoint.RawPosition.X;
    line.Y2 = args.CurrentPoint.RawPosition.Y;
}

private void StrokeInput_StrokeStarted(InkStrokeInput sender, PointerEventArgs args)
{
    line = new Line();
    line.X1 = args.CurrentPoint.RawPosition.X;
    line.Y1 = args.CurrentPoint.RawPosition.Y;
    line.X2 = args.CurrentPoint.RawPosition.X;
    line.Y2 = args.CurrentPoint.RawPosition.Y;

    line.Stroke = new SolidColorBrush(Colors.Purple);
    line.StrokeThickness = 4;
} 

我如何将此代码调整为圆圈?或者我怎样才能画一个圆?

谢谢,

如果我没理解错你的问题,你只是想用InkStroke画一个椭圆,然后把这些椭圆的笔画添加到InkCanvas中。然后,您可以将这些笔画用于其他目的(例如,保存这些笔画并用于下一次使用)。如果是这样,我只能告诉你这是不可能的。因为你在InkCanvas上画一个类似的椭圆,你可以用InkAnalyzer把它转成椭圆,但它实际上只有returns四个点给你。 Jayden 也在这个帖子中提到了它:Add polygon/ellipse to inkcanvas in uwp

但正如我所说,如果使用墨迹画椭圆的原因是为了节省笔画以作他用,我可以给你另一种方法。您可以使用win2D-UWP相关API在CanvasControl上绘制椭圆并保存为CanvasSvgDocument。然后,您可以将其另存为 svg xml 文件。对于网络使用,您可以通过 CanvasSvgDocument.LoadAsync 将此 svg xml 文件加载到 CanvasControl 中。

如果您对 CanvasSvgDocument 感兴趣,可以参考SvgExample了解更多详情。

float stepsize = 0.05f;
float radius = 10f;

for (float i = 0; i <= 2*Math.PI; i+= stepsize )

  var x = position.x + radius * Math.cos(i);
  var y = position.y + radius * Math.sin(i);
  // draw dot here at x,y
}

如果您想要线而不是点,请从前一个 xy 到当前 xy 画一条线。 (在 xy_old 中缓存 xy)