【问题标题】:Draw vertical lines on surface of the circle在圆的表面画垂直线
【发布时间】:2022-11-27 03:18:55
【问题描述】:

我使用 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 文档,但仍然没有找到在圆表面上绘制线条的方法。谁能建议如何添加这些行。我正在发送我需要绘制的图片示例。任何帮助表示赞赏。

【问题讨论】:

  • 您能否进一步说明“在圆的表面画线”是什么意思?示例图像是否正是您要实现的目标?有什么规则你想如何分配线路?
  • @Maku,我需要做一个稍微复杂一点的形状,但是一开始,我需要做成图片中的样子。

标签: c# xaml skiasharp


【解决方案1】:

根据你的描述,我会选择基于圆方程的东西:

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

您可以根据需要对硬编码值进行参数化。上述代码的结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 2023-02-04
    • 2016-07-29
    • 2021-10-11
    相关资源
    最近更新 更多