Search code examples
javagraphicsdrawinglabelpoints

Drawing Arc using two Points as Reference


I already searched over stackoverflow for similar questions, and tried to implement using some suggestions from other answers like this one:

Point p1 = prop.getDisplayPoint();
Point p2 = prop2.getDisplayPoint();

int xCenter = p1.x - 50;
int yCenter;

if(p1.y > p2.y)
        yCenter = p1.y - ((p1.y-p2.y)/2);
else
        yCenter = p2.y - ((p2.y-p1.y)/2);

int r = (int)Math.sqrt((p1.x-xCenter)*(p1.x-xCenter) + (p1.y-yCenter)*(p1.y-yCenter));
int x = xCenter-r;
int y = yCenter-r;
int width = 2*r;
int height = 2*r;
int startAngle = (int) ((180/Math.PI)*Math.atan2(p1.y-yCenter, p1.x-xCenter));
int endAngle = (int) ((180/Math.PI)*Math.atan2(p2.y-yCenter, p2.x-xCenter));
g.drawArc(x, y, width, height, startAngle, endAngle);

But still this didnt helped me. I tried to compute the center of my two points, but the result of the arc was not what I was waiting for.

This was the result:

enter image description here

This is what I am looking for:

Expected result

I have the coordinates of each pair of points I want to connect, like the point at "car" and the point at "bus" then I want to draw an arc between them. The angle will always be something like that.


Solution

  • Looking at the picture, it appears that you have two problems.

    The first is that your origin point is incorrect. I haven't tried specific values, but I think it's because you're calculating an arbitrary center based on the first point, and then calculating the start and end of your arcs based on that center point (rather than the actual text locations).

    However, I think that the bigger problem is that an arc isn't really appropriate to the task. With an arc, you could go for a semi-circle (or semi-oval), or maybe 1/3 of a circle, but those won't look very good. They certainly won't look like your desired example.

    Instead, you want a Bezier Curve, so that you can deepen the sides of the "arc". Here's a SO question that points you to the docs for the bezier curve functions in Java2D. There are also a bunch of examples if you Google for "java draw bezier".