javagraphicsgeometrytrigonometrysegment

How do I draw an arc based on two given points and a given height describing a circle segment?


I'm trying to draw an arc based on two given points and a given height describing a circle segment. To acomplish this I would use the following method from java.awt.Graphics.

drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)

First I observe that the x, y, width and height values describes a rectangle containing the circle.

The center of the arc is the center of the rectangle whose origin is (x, y) and whose size is specified by the width and height arguments. (http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html)

So first I start by calculating the x,y,width and height values. The picture below describes how I would do this.

enter image description here

The first picture shows what values I've already got. The arc in the first picture is exactly what I'm trying to draw. In picture number two I calculate the lenght of a line between the two points. My code to perform this step would look like this:

int dx = p1.x- p2.x;
int dy = p1.y - p2.y;
double len = Math.sqrt(Math.pow((double)dx, 2) + Math.pow((double)dy, 2));

As shown in picture three I can now calculate the radius of the circle by using the following function.

radius = h/2 + len^2 / 8h

The first problem I encounter is to calculate the CenterPoint of the circle. This is partly where I need help.

If I was to calculate the Center Point I can then easily find the x, y, whith and height coordinates.

x = centerPoint.x - radius;
y = centerPoint.y - radius;
width = radius * 2;
height = radius * 2;

The last part is to calculate the startAngle and arcAngle based on the values we already calculated.

TL;DR I need help with calculating the angles and the center point.

Thanks in advance!


Solution

  • There's a well-known relationship (see here, for instance) between the chord half length, the height of the arc (also called the sagitta), and the radius. Let the chord length (the distance between p1 and p2 be l = 2 d, let the arc height be h, and let the radius be r. Then

    r = (d 2 + h 2) / (2 h)

    The center is on the perpendicular bisector of the chord, at a distance r - h from the chord, on the opposite side from the arc.1 You can then use standard inverse trig functions to get the start and end angles for the chord.

     1 Note that it is not enough to know p1, p2, and h; you need some way of identifying which side of the chord has the center and which side has the arc.