Find all angles of a triangle in 3D

21 Jul, 2021
Given coordinates of 3 vertices of a triangle in 3D i.e. A(x1, y1, z1), B(x2, y2, z2), C(x3, y3, z3). The task is to find out all the angles of the triangle formed by above coordinates.Examples:  
Input: 
x1 = -1, y1 = 3, z1 = 2
x2 = 2, y2 = 3, z2 = 5
x3 = 3, y3 = 5, z3 = -2

Output:
angle A =  90.0 degree
angle B =  54.736 degree
angle C =  35.264 degree
 
 
Approach: For finding angle A find out direction ratios of AB and AC : direction ratios of AB = x2-x1, y2-y1, z2-z1 direction ratios of AC = x3-x1, y3-y1, z3-z1 then angle A = For finding angle B find out direction ratios of BA and BC : direction ratios of BA = x1-x2, y1-y2, z1-z2 direction ratios of BC = x3-x2, y3-y2, z3-z2 then angle B = For finding angle C find out direction ratios of CB and CA : direction ratios of CB = x2-x3, y2-y3, z2-z3 direction ratios of CA = x1-x3, y1-y3, z1-z3 then angle C = Below is the implementation of above approach: 
C++
Java
Python3
C#
PHP
Javascript
//CPP program for finding all angles of a triangle#include<bits/stdc++.h>#include<cmath>using namespace std; // function for finding the anglefloat angle_triangle(int x1, int x2, int x3,            int y1, int y2, int y3, int z1, int z2, int z3){     int num = (x2-x1)*(x3-x1)+(y2-y1)*(y3-y1)+(z2-z1)*(z3-z1) ;     float den = sqrt(pow((x2-x1),2)+pow((y2-y1),2)+pow((z2-z1),2))*\                sqrt(pow((x3-x1),2)+pow((y3-y1),2)+pow((z3-z1),2)) ;     float angle = acos(num / den)*(180.0/3.141592653589793238463) ;     return angle ;} // Driver codeint main(){int x1 = -1;int y1 = 3;int z1 = 2;int x2 = 2;int y2 = 3;int z2 = 5;int x3 = 3;int y3 = 5;int z3 = -2;float angle_A = angle_triangle(x1, x2, x3, y1, y2,                                y3, z1, z2, z3);float angle_B = angle_triangle(x2, x3, x1, y2, y3,                                y1, z2, z3, z1);float angle_C = angle_triangle(x3, x2, x1, y3,                            y2, y1, z3, z2, z1);cout<<"Angles are :"<<endl;cout<<setprecision(3);cout<<"angle A = "<<angle_A<<" degree"<<endl;cout<<"angle B = "<<angle_B<<" degree"<<endl;cout<<"angle C = "<<angle_C<< " degree"<<endl;}
//Java program for finding all angles of a triangle class GFG{// function for finding the anglestatic double angle_triangle(int x1, int x2, int x3,            int y1, int y2, int y3, int z1, int z2, int z3){     int num = (x2-x1)*(x3-x1)+(y2-y1)*(y3-y1)+(z2-z1)*(z3-z1) ;     double den = Math.sqrt(Math.pow((x2-x1),2)+                Math.pow((y2-y1),2)+Math.pow((z2-z1),2))*                Math.sqrt(Math.pow((x3-x1),2)+                Math.pow((y3-y1),2)+Math.pow((z3-z1),2)) ;     double angle = Math.acos(num / den)*(180.0/3.141592653589793238463) ;     return angle ;} // Driver codepublic static void main(String[] args){int x1 = -1;int y1 = 3;int z1 = 2;int x2 = 2;int y2 = 3;int z2 = 5;int x3 = 3;int y3 = 5;int z3 = -2;double angle_A = angle_triangle(x1, x2, x3, y1,                            y2, y3, z1, z2, z3);double angle_B = angle_triangle(x2, x3, x1,                            y2, y3, y1, z2, z3, z1);double angle_C = angle_triangle(x3, x2, x1,                            y3, y2, y1, z3, z2, z1);System.out.println("Angles are :");System.out.println("angle A = "+angle_A+" degree");System.out.println("angle B = "+angle_B+" degree");System.out.println("angle C = "+angle_C+" degree");}}// This code is contributed by mits
# Python Code for finding all angles of a triangleimport math # function for finding the angledef angle_triangle(x1, x2, x3, y1, y2, y3, z1, z2, z3):     num = (x2-x1)*(x3-x1)+(y2-y1)*(y3-y1)+(z2-z1)*(z3-z1)     den = math.sqrt((x2-x1)**2+(y2-y1)**2+(z2-z1)**2)*\                math.sqrt((x3-x1)**2+(y3-y1)**2+(z3-z1)**2)     angle = math.degrees(math.acos(num / den))     return round(angle, 3) # driver code    x1 = -1y1 = 3z1 = 2x2 = 2y2 = 3z2 = 5x3 = 3y3 = 5z3 = -2angle_A = angle_triangle(x1, x2, x3, y1, y2, y3, z1, z2, z3)angle_B = angle_triangle(x2, x3, x1, y2, y3, y1, z2, z3, z1)angle_C = angle_triangle(x3, x2, x1, y3, y2, y1, z3, z2, z1)print("Angles are :")print("angle A = ", angle_A, "degree")print("angle B = ", angle_B, "degree")print("angle C = ", angle_C, "degree")
// C# program for finding all// angles of a triangleusing System;                     class GFG{     // function for finding the anglestatic double angle_triangle(int x1, int x2, int x3,                             int y1, int y2, int y3,                             int z1, int z2, int z3){     int num = (x2 - x1) * (x3 - x1) +              (y2 - y1) * (y3 - y1) +              (z2 - z1) * (z3 - z1);     double den = Math.Sqrt(Math.Pow((x2 - x1), 2) +                           Math.Pow((y2 - y1), 2) +                           Math.Pow((z2 - z1), 2)) *                 Math.Sqrt(Math.Pow((x3 - x1), 2) +                           Math.Pow((y3 - y1), 2) +                           Math.Pow((z3 - z1), 2));     double angle = Math.Acos(num / den) *                   (180.0/3.141592653589793238463);     return angle ;} // Driver codepublic static void Main(){    int x1 = -1, y1 = 3, z1 = 2;    int x2 = 2, y2 = 3, z2 = 5;    int x3 = 3, y3 = 5, z3 = -2;    double angle_A = angle_triangle(x1, x2, x3,                                    y1, y2, y3,                                    z1, z2, z3);    double angle_B = angle_triangle(x2, x3, x1,                                    y2, y3, y1,                                    z2, z3, z1);    double angle_C = angle_triangle(x3, x2, x1,                                    y3, y2, y1,                                    z3, z2, z1);    Console.WriteLine("Angles are :");    Console.WriteLine("angle A = " + angle_A +                                   " degree");    Console.WriteLine("angle B = " + angle_B +                                   " degree");    Console.WriteLine("angle C = " + angle_C +                                   " degree");}} // This code is contributed by 29AjayKumar
<?php// PHP program for finding all// angles of a triangle // function for finding the anglefunction angle_triangle($x1, $x2, $x3,                        $y1, $y2, $y3,                        $z1, $z2, $z3){     $num = ($x2 - $x1) * ($x3 - $x1) +           ($y2 - $y1) * ($y3 - $y1) +           ($z2 - $z1) * ($z3 - $z1);     $den = sqrt(pow(($x2 - $x1), 2) +                pow(($y2 - $y1), 2) +                pow(($z2 - $z1), 2)) *           sqrt(pow(($x3 - $x1), 2) +                pow(($y3 - $y1), 2) +                pow(($z3 - $z1), 2));     $angle = acos($num / $den) *                 (180.0 / 3.141592653589793238463);     return $angle ;} // Driver code$x1 = -1; $y1 = 3; $z1 = 2;$x2 = 2; $y2 = 3; $z2 = 5;$x3 = 3; $y3 = 5; $z3 = -2;$angle_A = angle_triangle($x1, $x2, $x3, $y1, $y2,                          $y3, $z1, $z2, $z3);$angle_B = angle_triangle($x2, $x3, $x1, $y2, $y3,                          $y1, $z2, $z3, $z1);$angle_C = angle_triangle($x3, $x2, $x1, $y3,                          $y2, $y1, $z3, $z2, $z1);echo "Angles are :\n";echo "angle A = " . round($angle_A, 3) . " degree\n";echo "angle B = " . round($angle_B, 3) . " degree\n";echo "angle C = " . round($angle_C, 3) . " degree\n"; // This code is contributed by mits?>
<script>//Javascript program for finding all angles of a triangle // function for finding the anglefunction angle_triangle(x1,x2,x3,y1,y2,y3,z1,z2,z3){    let num = (x2-x1)*(x3-x1)+(y2-y1)*(y3-y1)+(z2-z1)*(z3-z1) ;       let den = Math.sqrt(Math.pow((x2-x1),2)+                Math.pow((y2-y1),2)+Math.pow((z2-z1),2))*                Math.sqrt(Math.pow((x3-x1),2)+                Math.pow((y3-y1),2)+Math.pow((z3-z1),2)) ;       let angle = Math.acos(num / den)*(180.0/3.141592653589793238463) ;       return angle ;} // Driver codelet x1 = -1;let y1 = 3;let z1 = 2;let x2 = 2;let y2 = 3;let z2 = 5;let x3 = 3;let y3 = 5;let z3 = -2;let angle_A = angle_triangle(x1, x2, x3, y1,                            y2, y3, z1, z2, z3);let angle_B = angle_triangle(x2, x3, x1,                            y2, y3, y1, z2, z3, z1);let angle_C = angle_triangle(x3, x2, x1,                            y3, y2, y1, z3, z2, z1);document.write("Angles are :<br>");document.write("angle A = "+angle_A+" degree<br>");document.write("angle B = "+angle_B.toFixed(3)+" degree<br>");document.write("angle C = "+angle_C.toFixed(3)+" degree<br>"); // This code is contributed by rag2127</script>
Angles are :
angle A =  90.0 degree
angle B =  54.736 degree
angle C =  35.264 degree
 
SURENDRA_GANGWAR
Mithun Kumar
29AjayKumar
rag2127
Geometric
Mathematical
Python Programs
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum Manhattan distance between a distinct pair from N coordinates
Area of the largest rectangle possible from given coordinates
Total area of two overlapping rectangles
Equation of circle when three points on the circle are given
Maximum distance between two points in coordinate plane using Rotating Caliper's Method
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Merge two sorted arrays