Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw arc with 2 points and center of the circle

I have two points of circle and center of this circle. I want to draw an arc between these points. Method drawArc is to simple and doesn't fit my purpose. Anybody help?

like image 961
CarolusPl Avatar asked Nov 16 '10 16:11

CarolusPl


People also ask

How do you find the arc between two points?

If the arc is just a straight line between two points of coordinates (x1,y1), (x2,y2), its length can be found by the Pythagorean theorem: L = √ (∆x)2 + (∆y)2 , where ∆x = x2 − x1 and ∆y = y2 − y1.

Can we draw a circle with 2 points?

No that is impossible. Create two circles with the same radius at centerpoints A + B. At the intersection of these two circles create an circle with the same radius....


2 Answers

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

Lets say that the center of the circle is (x0, y0) and that the arc contains your two points (x1, y1) and (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 luck!

like image 70
botismarius Avatar answered Oct 27 '22 18:10

botismarius


Graphics.drawArc expects the following parameters:

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

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

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

like image 35
Adamski Avatar answered Oct 27 '22 19:10

Adamski