Tutorialspoint.dev

Find all angles of a given triangle

Given coordinates of all three vertices of the triangle in the 2D plane, the task is to find all three angles.

Example:

Input : A = (0, 0), 
        B = (0, 1), 
        C = (1, 0)
Output : 90, 45, 45



To solve this problem we use below Law of cosines.

c^2 = a^2 + b^2 - 2(a)(b)(cos beta)

After re-arranging

beta = acos( ( a^2 + b^2 - c^2 ) / (2ab) )

In trigonometry, the law of cosines (also known as the cosine formula or cosine rule) relates the lengths of the sides of a triangle to the cosine of one of its angles.

First, calculate the length of all the sides. 
Then apply above formula to get all angles in 
radian. Then convert angles from radian into 
degrees.

Below is implementation of above steps.

div class="responsive-tabs">

C++

// Code to find all three angles
// of a triangle given coordinate
// of all three vertices
#include <iostream>
#include <utility> // for pair
#include <cmath> // for math functions
using namespace std;
  
#define PI 3.1415926535
  
// returns square of distance b/w two points
int lengthSquare(pair<int,int> X, pair<int,int> Y)
{
    int xDiff = X.first - Y.first;
    int yDiff = X.second - Y.second;
    return xDiff*xDiff + yDiff*yDiff;
}
  
void printAngle(pair<int,int> A, pair<int,int> B,
                pair<int,int> C)
{
    // Square of lengths be a2, b2, c2
    int a2 = lengthSquare(B,C);
    int b2 = lengthSquare(A,C);
    int c2 = lengthSquare(A,B);
  
    // lenght of sides be a, b, c
    float a = sqrt(a2);
    float b = sqrt(b2);
    float c = sqrt(c2);
  
    // From Cosine law
    float alpha = acos((b2 + c2 - a2)/(2*b*c));
    float betta = acos((a2 + c2 - b2)/(2*a*c));
    float gamma = acos((a2 + b2 - c2)/(2*a*b));
  
    // Converting to degree
    alpha = alpha * 180 / PI;
    betta = betta * 180 / PI;
    gamma = gamma * 180 / PI;
  
    // printing all the angles
    cout << "alpha : " << alpha << endl;
    cout << "betta : " << betta << endl;
    cout << "gamma : " << gamma << endl;
}
  
// Driver code
int main()
{
    pair<int,int> A = make_pair(0,0);
    pair<int,int> B = make_pair(0,1);
    pair<int,int> C = make_pair(1,0);
  
    printAngle(A,B,C);
  
    return 0;
}

Java

// Java Code to find all three angles
// of a triangle given coordinate
// of all three vertices
  
import java.awt.Point;
import static java.lang.Math.PI;
import static java.lang.Math.sqrt;
import static java.lang.Math.acos;
  
class Test
{
    // returns square of distance b/w two points
    static int lengthSquare(Point p1, Point p2)
    {
        int xDiff = p1.x- p2.x;
        int yDiff = p1.y- p2.y;
        return xDiff*xDiff + yDiff*yDiff;
    }
      
    static void printAngle(Point A, Point B,
            Point C)
    {
    // Square of lengths be a2, b2, c2
    int a2 = lengthSquare(B,C);
    int b2 = lengthSquare(A,C);
    int c2 = lengthSquare(A,B);
      
    // lenght of sides be a, b, c
    float a = (float)sqrt(a2);
    float b = (float)sqrt(b2);
    float c = (float)sqrt(c2);
      
    // From Cosine law
    float alpha = (float) acos((b2 + c2 - a2)/(2*b*c));
    float betta = (float) acos((a2 + c2 - b2)/(2*a*c));
    float gamma = (float) acos((a2 + b2 - c2)/(2*a*b));
      
    // Converting to degree
    alpha = (float) (alpha * 180 / PI);
    betta = (float) (betta * 180 / PI);
    gamma = (float) (gamma * 180 / PI);
      
    // printing all the angles
    System.out.println("alpha : " + alpha);
    System.out.println("betta : " + betta);
    System.out.println("gamma : " + gamma);
    }
      
    // Driver method
    public static void main(String[] args) 
    {
        Point A = new Point(0,0);
        Point B = new Point(0,1);
        Point C = new Point(1,0);
       
        printAngle(A,B,C);
    }
}


Output:

alpha : 90
betta : 45
gamma : 45

Reference :
https://en.wikipedia.org/wiki/Law_of_cosines

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter