Search code examples
c#xamarin.formsskiasharpskia

Add values to arc


I am trying to add values to arc and made a method for that purpose. However it looks like all of them are in one place even thought there are different coordinates given with for loop. What might be the problem?

this.RangeEnd is float 200

Here is method for adding values:

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

Here is the output:

enter image description here


EDIT:

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

produces following result:

enter image description here


Solution

  • I had test your code and found the value of xPos and yPos just had two kind of values. The cause of it is that (7 * Math.PI / 4) - (i * (2 * Math.PI / 40)) = 7/4PI - PI*(1,2,3...). This means the value of the Math.Sin(theta) and Math.Cos(theta) just have two kands of value. Because 2PI is a round for a circle. So you need to make the value of theta be 0~7/4PI.