0

I want to draw an arc using the method: .Canvas.drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)

I have two points on the circle and the circle center point and radius.

What exactly do I need to set in the oval rect and how to calculate the startAngle and sweepAngle?

I tried the code below:

m_radiusRect.set(x1, Math.min(y1, y2), x2, Math.max(y1,y2));
float startAngle = (float)((Math.toDegrees( Math.atan2(x1 - 360.0, 360.0 - y1) ) + 360.0) % 360.0);
float sweepAngle = (float)((Math.toDegrees( Math.atan2(x2 - 360.0, 360.0 - y2) ) + 360.0) % 360.0) - startAngle;

canvas.drawArc(m_radiusRect, startAngle, sweepAngle, false, m_paint);
piojo
  • 1,919
  • 4
  • 24
  • 40

1 Answers1

0

See this answer and find the angle of a point from center.

Based on that find the two angles for x1 and x2 says a1 and a2.

then,

sweepAngle = a2 - a1;
startAngle = a1;

Edited

The formula given in link looks not working since it does not consider center.

Here center is (cx, cy)

float startAngle = (int) ((float) Math.toDegrees( Math.atan2(x1 - cx, y1 - cy)));
         float sweepAngle = (int) ((float) Math.toDegrees( Math.atan2(x2 - cx, y2 - cy))) - startAngle;

         Rect rect = new Rect();

         rect.left = (int) (cx - radius);
         rect.top = (int) (cy - radius);
         rect.right = (int) (cx + radius);
         rect.bottom = (int) (cy + radius);
Community
  • 1
  • 1
Vivek Khandelwal
  • 7,829
  • 3
  • 25
  • 40
  • Its not working. Do I need to pass true as useCenter to the drawArc method? I try drawing a simple arc in a sample project to see if I don't have a mistake in the points – piojo Apr 18 '13 at 07:58
  • you got one thing wrong: `float startAngle = (float) Math.toDegrees( Math.atan2(y1 - cy, x1 - cx)); float sweepAngle = (float) Math.toDegrees( Math.atan2(y2 - cy, x2 - cx)) - startAngle;` Edit your answer and I'll mark it as the correct one – piojo Apr 18 '13 at 08:40
  • I have just gave a suggestion and never tried. its good that u found a correct one. – Vivek Khandelwal Apr 18 '13 at 08:57