[ACCEPTED]-Draw arc with 2 points and center of the circle-geometric-arc

Accepted answer
Score: 27

You can use Canvas.drawArc, but you must 5 compute the arguments it needs:

Lets say 4 that the center of the circle is (x0, y0) and 3 that the arc contains your two points (x1, y1) and 2 (x2, y2). Then the radius is: r=sqrt((x1-x0)(x1-x0) + (y1-y0)(y1-y0)). So:

int r = (int)Math.sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0));
int x = x0-r;
int y = y0-r;
int width = 2*r;
int height = 2*r;
int startAngle = (int) (180/Math.PI*atan2(y1-y0, x1-x0));
int endAngle = (int) (180/Math.PI*atan2(y2-y0, x2-x0));
canvas.drawArc(x, y, width, height, startAngle, endAngle);

Good 1 luck!

Score: 2

Graphics.drawArc expects the following parameters:

  • x
  • y
  • width
  • height
  • startAngle
  • arcAngle

Given 6 your arc start and end points it is possible 5 to compute a bounding box where the arc will be drawn. This 4 gives you enough information to provide 3 parameters: x, y, width and height.

You haven't 2 specified the desired angle so I guess you 1 could choose one arbitrarily.

More Related questions