XAML 在圆表面画垂直线

mm9b1k5b  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(179)

我用skiasharp画几何图形。它们大多是圆。如果我们有一个简单的例子,就像这样:

using System;
using SkiaSharp;

void Draw(SKCanvas canvas, int width, int height)
{
    
        var circleBorder = new SKPaint
        {
            IsAntialias = true,
            Style = SKPaintStyle.Stroke,
            Color = SKColors.Blue,
            StrokeWidth = 5
        };
        canvas.DrawCircle(width / 2, height / 2, 30, circleBorder);
}

我们得到了一个简单的圆。我看了skiasharp文档,仍然没有找到在圆表面上画线的方法。有人能建议如何添加这些线吗?我发送了一个我需要画的图片的例子。任何帮助都是感激的。

8wigbo56

8wigbo561#

根据你的描述,我会用一个基于圆方程的东西:

void Draw(SKCanvas canvas, int width, int height)
{
    var circleBorder = new SKPaint
    {
        IsAntialias = true,
        Style = SKPaintStyle.Stroke,
        Color = SKColors.Blue,
        StrokeWidth = 5
    };

    var radius = 30f;
    var cx = width / 2f;
    var cy = height / 2;

    // define x-wise range on circle where we want to draw lines
    // 0 means leftmost point, 1 - rightmost
    // 0 <= startX < endX <= 1
    var startX = 0.15f;
    var endX = 1f;
    // how many lines in above range
    var count = 4;
    // length of lines
    var lineLength = 40f;

    var step = (endX - startX) / (count - 1);

    for (var i = 0; i < count; i++)
    {
        // R^2 = (x-x0)^2 + (y-y0)^2 => sqrt(R^2 - (x-x0)^2) = |y-y0|
        // we consider here coords from perspective where (0,0) is the leftmost point on the circle
        // therefore x0 = radius, y0 = 0

        var x = (i * step + startX) * radius * 2f; // current x on circle
        var absy_y0 = MathF.Sqrt(2 * x * radius - x * x); // == MathF.Sqrt(radius * radius - (x - radius) * (x - radius));
        var y = -Math.Abs(absy_y0); // we want lines upwards so only care about the smaller solution to equation

        canvas.DrawLine(x + cx - radius, y + cy, x + cx - radius, y + cy - lineLength, circleBorder);
    }

    canvas.DrawCircle(cx, cy, radius, circleBorder);
}

您可以根据需要对硬编码值进行参数化。

相关问题