Java Program to Find All the Angles of a Given Triangle

bookmark

import java.awt.*; 
import static java.lang.Math.*; 
public class Main
{
    public static void main(String[] args)
    {
        // Static initialization of the side of the triangle
        Point a = new Point(0,0); 
        Point b = new Point(0,1); 
        Point c = new Point(1,0);
        // Calling user-defined function by passing the coordinates as parameter
        angle(a, b ,c);
    }
    // Function to find out the area of the circumcircle
    static void angle(Point A, Point B, Point C)
    {
        // Calculates the square of the sides
        int sideSquare1= calcSideSquare(A,B);
        int sideSquare2= calcSideSquare(B,C);
        int sideSquare3= calcSideSquare(C,A);
        // Calculates the length of the sides
        float side1= (float)sqrt(sideSquare1);
        float side2= (float)sqrt(sideSquare2);
        float side3= (float)sqrt(sideSquare3);
        // Calculates the angles
        float angle1= (float) acos((sideSquare2 + sideSquare3 -sideSquare1)/(2*side2*side3)); 
        float angle2= (float) acos((sideSquare1 + sideSquare3 -sideSquare2)/(2*side1*side3)); 
        float angle3= (float) acos((sideSquare1 + sideSquare2 -sideSquare3)/(2*side1*side2)); 
        // Printing the angles
        System.out.println("angle 1 : " + radianToDegree(angle1)); 
        System.out.println("angle 2 : " + radianToDegree(angle2)); 
        System.out.println("angle 3 : " + radianToDegree(angle3)); 
    }
    
    // Returns square of the sides
    static int calcSideSquare(Point p1, Point p2) 
    { 
        int xVaries = p1.x- p2.x; 
        int yVaries = p1.y- p2.y; 
        return xVaries*xVaries + yVaries*yVaries; 
    } 
    
    // Converts radian into degree
    static float radianToDegree(float rad)
    {
        float  pi = (float)3.14;
        float degree = rad * 180 / pi;
        return degree;
    }

 


Output:

angle 1 : 45.022823
angle 2 : 90.04565
angle 3 : 45.022823