How to draw Arc between two points on the Canvas?

RajaReddy PolamReddy picture RajaReddy PolamReddy · Jun 21, 2012 · Viewed 74k times · Source

I have two points in the canvas, now I'm able to draw a line between those points like this below image by using

This code canvas.drawLine(p1.x, p1.y, p2.x, p2.y, paint); enter image description here

I want to draw the arc between two points like below image.

enter image description here

How can I draw like this.

Answer

RajaReddy PolamReddy picture RajaReddy PolamReddy · Jun 23, 2012

Finally I got the solution from this code:

float radius = 20;
final RectF oval = new RectF();
oval.set(point1.x - radius, point1.y - radius, point1.x + radius, point1.y+ radius);
Path myPath = new Path();
myPath.arcTo(oval, startAngle, -(float) sweepAngle, true);

To calculate startAngle, use this code:

int startAngle = (int) (180 / Math.PI * Math.atan2(point.y - point1.y, point.x - point1.x));

Here, point1 means where you want to start drawing the Arc. sweepAngle means the angle between two lines. We have to calculate that by using two points like the blue points in my Question image.