Search code examples
androidandroid-canvasondraw

How to draw Arc between two points on the Canvas?


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.


Solution

  • 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.