向弧添加值

Add values to arc

我正在尝试向 arc 添加值并为此创建了一个方法。然而,看起来它们都在一个地方,即使 for 循环给出了不同的坐标。可能是什么问题?

this.RangeEnd 是浮点数 200

加值方法如下:

  private void OnDrawValues()
    {
      using (SKPath scalePath = new SKPath())
      {
        for (int i = 0; i <= this.RangeEnd; i += 20)
        {
          double theta = (7 * Math.PI / 4) - (i * (2 * Math.PI / 40));
          double xPos = (_gaugeRect.Width / 2) * 1.2 * Math.Sin(theta) + _drawRect.MidX - 20 / 2;
          double yPos = (_gaugeRect.Width / 2) * 1.2 * Math.Cos(theta) + _drawRect.MidY + 20 / 2;

          using (SKPaint paint = new SKPaint())
          {
            paint.Color = SKColors.White;
            paint.IsAntialias = true;
            paint.TextSize = 40;
            _canvas.DrawText(i.ToString(), (float)xPos, (float)yPos, paint);
          }
        }
      }
    } 

这是输出:


编辑:

private void OnDrawValues()
{
  using (SKPath scalePath = new SKPath())
  {
    double theta = 0;

    for (int i = 0; i <= this.RangeEnd; i += 20)
    {
      theta += (7 * Math.PI / 4) - (i * (2 * Math.PI / 40));
      double xPos = (_gaugeRect.Width / 2) * 1.2 * Math.Sin(theta) + _drawRect.MidX - 20 / 2;
      double yPos = (_gaugeRect.Width / 2) * 1.2 * Math.Cos(theta) + _drawRect.MidY + 20 / 2;

      using (SKPaint paint = new SKPaint())
      {
        paint.Color = SKColors.White;
        paint.IsAntialias = true;
        paint.TextSize = 40;
        _canvas.DrawText(i.ToString(), (float)xPos, (float)yPos, paint);
      }
    }
  }
}

产生以下结果:

我测试了你的代码,发现 xPos 和 yPos 的值只有两种值。其原因是 (7 * Math.PI / 4) - (i * (2 * Math.PI / 40)) = 7/4PI - PI*(1,2,3...) .这意味着 Math.Sin(theta) 和 Math.Cos(theta) 的值只有两个 kands 值。因为2PI是一圈还一圈。所以需要让theta的值为0~7/4PI。

看到 theta 是您的公式中可以更改的主要内容:

double xPos = (_gaugeRect.Width / 2) * 1.2 * Math.Sin(theta) + _drawRect.MidX - 20 / 2;
double yPos = (_gaugeRect.Width / 2) * 1.2 * Math.Cos(theta) + _drawRect.MidY + 20 / 2;

我认为你需要在循环中增加 theta theta +=:

theta += (7 * Math.PI / 4) - (i * (2 * Math.PI / 40));

我最喜欢的作者是Rod Stephens. For any drawing development definitely consider his book(s)

这应该可以帮助您解决数学问题 (原来它应该是 theta += dtheta;):

namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
        }

        private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            const int RADIUS_INNER = 95;
            const int RADIUS_CIRCLE = 100;
            const int RADIUS_OUTER = 105;

            // Draw the circle.
            e.Graphics.DrawEllipse(Pens.Black, 10, 10, 2 * RADIUS_CIRCLE, 2 * RADIUS_CIRCLE);

            // Draw the tick marks.
            int cx = 10 + RADIUS_CIRCLE;  // Center point.
            int cy = 10 + RADIUS_CIRCLE;
            double theta = 0;
            double dtheta = 30 * Math.PI / 180;

            for (int i = 1; i <= 360 / (double)30; i++)
            {
                int x1 = (int)(cx + Math.Cos(theta) * RADIUS_INNER);
                int y1 = (int)(cy + Math.Sin(theta) * RADIUS_INNER);
                int x2 = (int)(cx + Math.Cos(theta) * RADIUS_OUTER);
                int y2 = (int)(cy + Math.Sin(theta) * RADIUS_OUTER);
                e.Graphics.DrawLine(Pens.Red, x1, y1, x2, y2);

                theta += dtheta;
            }
        }
    }
}