简体   繁体   中英

how to draw the arc for three point angle in java

In java I have three points denoting two line with making an angle. Now I have to create the angle arc about 10 pixel apart from common point. with showing angle on the arc. I am able to calculate the angle but how to draw the arc and to show the angle on the arc. Please tell me some code view or link where I can find solution for this. CODE snippet is as below.

public void paintComponent(Graphics g){
        Graphics2D g2=(Graphics2D)g;

        Point p1=new Point(100,100);
        Point p2=new Point(200,100);
        Point p3=new Point(100,0);

        Line2D line1=new Line2D.Double(p1, p2);
        Line2D line2=new Line2D.Double(p1, p3);
        g2.draw(line1);
        g2.draw(line2);
        double angle=getAngle(line1,line2);
        System.out.println(angle);
        //g2.drawArc(110, 100, 20, 20, 100, 30);

    }

    public double getAngle(Line2D line1,Line2D line2){

        double angle1=Math.atan2(line1.getY1()-line1.getY2(), line1.getX1()-line1.getX2());
        double angle2=Math.atan2(line2.getY1()-line2.getY2(), line2.getX1()-line2.getX2());

        return Math.toDegrees(angle1-angle2);
    }

I don't know how to use DrawArc to draw exact arc which i want and also to put the angle on that.

Thanks & Regards.

From the documentation drawArc's arguments are:

int x, int y, int width, int height, int startAngle, int arcAngle

the x and y are your common point (p1), and your width and height are probably both 10 (to draw a circular arc with radius 10pixels)

The angle that you have computed is the last argument (arcAngle) which measure the sweep of the arc in a counter-clockwise direction. So that last part you need to work out is the start angle, which is probably your angle1 or angle2 (0 in this case is the positive x-axis or 3 o'clock position).

Keep in mind, as written you will sometimes be drawing an arc of > 180 degrees, you will need more logic if you want to always find the smallest angle between the two lines.

As for the text you can use drawString and figure out the x and y using some trigonometry with half your sweep angle and your desired radius. Though for optimal placement you might need to figure out what quadrant you are drawing in and adjust from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM