![]() |
VOOZH | about |
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.
👁 all angles of a given triangle
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.
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