I want to draw an arc. A fairly wide round arc, between two points, on the canvas in android.
Of course I exhaustively searched here and found several people asking the same question (such as), but try as I might, I couldn't make heads or tails of the answers, and I couldn't get the drawArc function to do what I needed!
Then I finally found this very detailed breakdown of how to do exactly that.
private static void arcBetween(PointF e1, PointF e2, Paint paint, Canvas canvas) {
float a1Degrees = 36.0f;
double a1 = Math.toRadians(a1Degrees);
// l1 is half the length of the line from e1 to e2
double dx = e2.x - e1.x, dy = e2.y - e1.y;
double l = Math.sqrt((dx * dx) + (dy * dy));
double l1 = l / 2.0;
// h is length of the line from the middle of the connecting line to the
// center of the circle.
double h = l1 / (Math.tan(a1 / 2.0));
// r is the radius of the circle
double r = l1 / (Math.sin(a1 / 2.0));
// a2 is the angle at which L intersects the x axis
double a2 = Math.atan2(dy, dx);
// a3 is the angle at which H intersects the x axis
double a3 = (Math.PI / 2.0) - a2;
// m is the midpoint of the line from e1 to e2
double mX = (e1.x + e2.x) / 2.0;
double mY = (e1.y + e2.y) / 2.0;
// c is the the center of the circle
double cY = mY + (h * Math.sin(a3));
double cX = mX - (h * Math.cos(a3));
// rect is the square RectF that bounds the "oval"
RectF oval =
new RectF((float) (cX - r), (float) (cY - r), (float) (cX + r), (float) (cY + r));
// a4 is the starting sweep angle
double rawA4 = Math.atan2(e1.y - cY, e1.x - cX);
float a4 = (float) Math.toDegrees(rawA4);
canvas.drawArc(oval, a4, a1Degrees, false, paint);
}
Alas, the math in it is beyond me, but I managed to copy/paste it into code, and it does produce an arc (as you can see in the image).
However, there are three problems with this arc for my purposes.
Why is there also a straight line with the enclosed area filled? I need just a, curved line, not a dome. I thought that's what the false in
drawArc()
did, but it's turned off and doesn't seem to do it.Though the upper corner of the arc is in the correct location, I've also placed (and super triple checked) that the lower point is on the edge of the blue element, and not on the bottom of the screen. Yet, still the corner of the arc is there.
One very important part though is that I need the arc to be shaped differently, to have more of a bulge in the middle. Really, I need to be able to adjust that bulge programmaticly, so I need it passed somehow as a variable to that function.
That's it, I've been trying, but haven't studied this math and am baffled. Your solutions are deeply appreciation.