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.
C++
#include <iostream>
#include <utility> // for pair
#include <cmath> // for math functions
using namespace std;
#define PI 3.1415926535
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)
{
int a2 = lengthSquare(B,C);
int b2 = lengthSquare(A,C);
int c2 = lengthSquare(A,B);
float a = sqrt (a2);
float b = sqrt (b2);
float c = sqrt (c2);
float alpha = acos ((b2 + c2 - a2)/(2*b*c));
float beta = acos ((a2 + c2 - b2)/(2*a*c));
float gamma = acos ((a2 + b2 - c2)/(2*a*b));
alpha = alpha * 180 / PI;
beta = beta * 180 / PI;
gamma = gamma * 180 / PI;
cout << "alpha : " << alpha << endl;
cout << "beta : " << beta << endl;
cout << "gamma : " << gamma << endl;
}
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
import java.awt.Point;
import static java.lang.Math.PI;
import static java.lang.Math.sqrt;
import static java.lang.Math.acos;
class Test
{
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)
{
int a2 = lengthSquare(B,C);
int b2 = lengthSquare(A,C);
int c2 = lengthSquare(A,B);
float a = ( float )sqrt(a2);
float b = ( float )sqrt(b2);
float c = ( float )sqrt(c2);
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));
alpha = ( float ) (alpha * 180 / PI);
betta = ( float ) (betta * 180 / PI);
gamma = ( float ) (gamma * 180 / PI);
System.out.println( "alpha : " + alpha);
System.out.println( "betta : " + betta);
System.out.println( "gamma : " + gamma);
}
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);
}
}
|
Python3
import math
def lengthSquare(X, Y):
xDiff = X[ 0 ] - Y[ 0 ]
yDiff = X[ 1 ] - Y[ 1 ]
return xDiff * xDiff + yDiff * yDiff
def printAngle(A, B, C):
a2 = lengthSquare(B, C)
b2 = lengthSquare(A, C)
c2 = lengthSquare(A, B)
a = math.sqrt(a2);
b = math.sqrt(b2);
c = math.sqrt(c2);
alpha = math.acos((b2 + c2 - a2) /
( 2 * b * c));
betta = math.acos((a2 + c2 - b2) /
( 2 * a * c));
gamma = math.acos((a2 + b2 - c2) /
( 2 * a * b));
alpha = alpha * 180 / math.pi;
betta = betta * 180 / math.pi;
gamma = gamma * 180 / math.pi;
print ( "alpha : %f" % (alpha))
print ( "betta : %f" % (betta))
print ( "gamma : %f" % (gamma))
A = ( 0 , 0 )
B = ( 0 , 1 )
C = ( 1 , 0 )
printAngle(A, B, C);
|
C#
using System;
class GFG
{
class Point
{
public int x, y;
public Point( int x, int y)
{
this .x = x;
this .y = y;
}
}
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)
{
int a2 = lengthSquare(B, C);
int b2 = lengthSquare(A, C);
int c2 = lengthSquare(A, B);
float a = ( float )Math.Sqrt(a2);
float b = ( float )Math.Sqrt(b2);
float c = ( float )Math.Sqrt(c2);
float alpha = ( float ) Math.Acos((b2 + c2 - a2) /
(2 * b * c));
float betta = ( float ) Math.Acos((a2 + c2 - b2) /
(2 * a * c));
float gamma = ( float ) Math.Acos((a2 + b2 - c2) /
(2 * a * b));
alpha = ( float ) (alpha * 180 / Math.PI);
betta = ( float ) (betta * 180 / Math.PI);
gamma = ( float ) (gamma * 180 / Math.PI);
Console.WriteLine( "alpha : " + alpha);
Console.WriteLine( "betta : " + betta);
Console.WriteLine( "gamma : " + gamma);
}
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);
}
}
|
Javascript
function lengthSquare(X, Y){
let xDiff = X[0] - Y[0];
let yDiff = X[1] - Y[1];
return xDiff*xDiff + yDiff*yDiff;
}
function printAngle(A, B, C){
let a2 = lengthSquare(B,C);
let b2 = lengthSquare(A,C);
let c2 = lengthSquare(A,B);
let a = Math.sqrt(a2);
let b = Math.sqrt(b2);
let c = Math.sqrt(c2);
let alpha = Math.acos((b2 + c2 - a2)/(2*b*c));
let beta = Math.acos((a2 + c2 - b2)/(2*a*c));
let gamma = Math.acos((a2 + b2 - c2)/(2*a*b));
alpha = alpha * 180 / Math.PI;
beta = beta * 180 / Math.PI;
gamma = gamma * 180 / Math.PI;
console.log( "alpha : " , alpha);
console.log( "beta : " , beta);
console.log( "gamma : " , gamma);
}
let A = [0, 0];
let B = [0, 1];
let C = [1, 0];
printAngle(A,B,C);
|
Output:
alpha : 90
beta : 45
gamma : 45
Time Complexity: O(log(n)) since using inbuilt sqrt functions
Auxiliary Space: O(1)
Reference :
https://en.wikipedia.org/wiki/Law_of_cosines
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
16 Feb, 2023
Like Article
Save Article