Given coordinates of three points A(x1, y1, z1), B(x2, y2, z2), and C(x3, y3, z3) in a 3D plane, where B is the intersection point of line AB and BC, the task is to find the angle between lines AB and BC.
Examples:
Input: x1 = 1, y1 = 3, z1 = 3; x2 = 3, y2 = 4, z2 = 5; x3 = 5, y3 = 6, z3 = 9;
Output: 54.6065
Input: x1 = 10, y1 = 10, z1 = 10; x2 = 0, y2 = 0, z2 = 0; x3 = 15, y3 = 10, z3 = 15;
Output: 56.4496
Approach:
1. Find the equation of lines AB and BC with the given coordinates in terms of direction ratios as:
AB = (x1 – x2)i + (y1 – y2)j + (z1 – z2)k
BC = (x3 – x2)i + (y3 – y2)j + (z3 – z2)k
2. Use the formula for cos ? for the two direction ratios of lines AB and BC to find the cosine of the angle between lines AB and BC as:
where,
AB.BC is the dot product of direction ratios AB and BC.
|AB| is the magnitude of line AB
|BC| is the magnitude of line BC
3. Suppose there are two direction ratios:
A = ai + bj + ck B = xi + yj + zk
then
Dot Product(A.B) = a*x + b*y + c*z
magnitude of A = |A| =
magnitude of B = |B| =![]()
4. The cosine of the angle calculated gives the cosine value in radian. To find the angle multiply the cosine value by (180/?).
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include "bits/stdc++.h"#define PI 3.14using namespace std;// Function to find the angle between// the two linesvoid calculateAngle( int x1, int y1, int z1, int x2, int y2, int z2, int x3, int y3, int z3){ // Find direction ratio of line AB int ABx = x1 - x2; int ABy = y1 - y2; int ABz = z1 - z2; // Find direction ratio of line BC int BCx = x3 - x2; int BCy = y3 - y2; int BCz = z3 - z2; // Find the dotProduct // of lines AB & BC double dotProduct = ABx * BCx + ABy * BCy + ABz * BCz; // Find magnitude of // line AB and BC double magnitudeAB = ABx * ABx + ABy * ABy + ABz * ABz; double magnitudeBC = BCx * BCx + BCy * BCy + BCz * BCz; // Find the cosine of // the angle formed // by line AB and BC double angle = dotProduct; angle /= sqrt( magnitudeAB * magnitudeBC); // Find angle in radian angle = (angle * 180) / PI; // Print the angle cout << abs(angle) << endl;}// Driver Codeint main(){ // Given coordinates // Points A int x1 = 1, y1 = 3, z1 = 3; // Points B int x2 = 3, y2 = 4, z2 = 5; // Points C int x3 = 5, y3 = 6, z3 = 9; // Function Call calculateAngle(x1, y1, z1, x2, y2, z2, x3, y3, z3); return 0;} |
Java
// Java program for the above approachimport java.util.*;class GFG{ // Function to find the angle // between the two linesstatic void calculateAngle(int x1, int y1, int z1, int x2, int y2, int z2, int x3, int y3, int z3){ // Find direction ratio of line AB int ABx = x1 - x2; int ABy = y1 - y2; int ABz = z1 - z2; // Find direction ratio of line BC int BCx = x3 - x2; int BCy = y3 - y2; int BCz = z3 - z2; // Find the dotProduct // of lines AB & BC double dotProduct = ABx * BCx + ABy * BCy + ABz * BCz; // Find magnitude of // line AB and BC double magnitudeAB = ABx * ABx + ABy * ABy + ABz * ABz; double magnitudeBC = BCx * BCx + BCy * BCy + BCz * BCz; // Find the cosine of the // angle formed by line // AB and BC double angle = dotProduct; angle /= Math.sqrt(magnitudeAB * magnitudeBC); // Find angle in radian angle = (angle * 180) / 3.14; // Print the angle System.out.printf("%.4f", Math.abs(angle));}// Driver codepublic static void main(String[] args){ // Given coordinates // Points A int x1 = 1, y1 = 3, z1 = 3; // Points B int x2 = 3, y2 = 4, z2 = 5; // Points C int x3 = 5, y3 = 6, z3 = 9; // Function Call calculateAngle(x1, y1, z1, x2, y2, z2, x3, y3, z3);}}// This code is contributed by offbeat |
Python3
# Python3 program for the above approachimport math# Function to find the angle # between the two linesdef calculateAngle(x1, y1, z1, x2, y2, z2, x3, y3, z3): # Find direction ratio of line AB ABx = x1 - x2; ABy = y1 - y2; ABz = z1 - z2; # Find direction ratio of line BC BCx = x3 - x2; BCy = y3 - y2; BCz = z3 - z2; # Find the dotProduct # of lines AB & BC dotProduct = (ABx * BCx + ABy * BCy + ABz * BCz); # Find magnitude of # line AB and BC magnitudeAB = (ABx * ABx + ABy * ABy + ABz * ABz); magnitudeBC = (BCx * BCx + BCy * BCy + BCz * BCz); # Find the cosine of # the angle formed # by line AB and BC angle = dotProduct; angle /= math.sqrt(magnitudeAB * magnitudeBC); # Find angle in radian angle = (angle * 180) / 3.14; # Print angle print(round(abs(angle), 4))# Driver Codeif __name__=='__main__': # Given coordinates # Points A x1, y1, z1 = 1, 3, 3; # Points B x2, y2, z2 = 3, 4, 5; # Points C x3, y3, z3 = 5, 6, 9; # Function Call calculateAngle(x1, y1, z1, x2, y2, z2, x3, y3, z3);# This code is contributed by AbhiThakur |
C#
// C# program for the above approachusing System;class GFG{ // Function to find the angle // between the two linesstatic void calculateAngle(int x1, int y1, int z1, int x2, int y2, int z2, int x3, int y3, int z3){ // Find direction ratio of line AB int ABx = x1 - x2; int ABy = y1 - y2; int ABz = z1 - z2; // Find direction ratio of line BC int BCx = x3 - x2; int BCy = y3 - y2; int BCz = z3 - z2; // Find the dotProduct // of lines AB & BC double dotProduct = ABx * BCx + ABy * BCy + ABz * BCz; // Find magnitude of // line AB and BC double magnitudeAB = ABx * ABx + ABy * ABy + ABz * ABz; double magnitudeBC = BCx * BCx + BCy * BCy + BCz * BCz; // Find the cosine of the // angle formed by line // AB and BC double angle = dotProduct; angle /= Math.Sqrt(magnitudeAB * magnitudeBC); // Find angle in radian angle = (angle * 180) / 3.14; // Print the angle Console.Write(String.Format("{0:F4}", Math.Abs(angle)));}// Driver codepublic static void Main(){ // Given coordinates // Points A int x1 = 1, y1 = 3, z1 = 3; // Points B int x2 = 3, y2 = 4, z2 = 5; // Points C int x3 = 5, y3 = 6, z3 = 9; // Function Call calculateAngle(x1, y1, z1, x2, y2, z2, x3, y3, z3);}}// This code is contributed by Code_Mech |
Javascript
<script>// Javascript program for the above approachvar PI = 3.14;// Function to find the angle between// the two linesfunction calculateAngle( x1, y1, z1, x2, y2, z2, x3, y3, z3){ // Find direction ratio of line AB var ABx = x1 - x2; var ABy = y1 - y2; var ABz = z1 - z2; // Find direction ratio of line BC var BCx = x3 - x2; var BCy = y3 - y2; var BCz = z3 - z2; // Find the dotProduct // of lines AB & BC var dotProduct = ABx * BCx + ABy * BCy + ABz * BCz; // Find magnitude of // line AB and BC var magnitudeAB = ABx * ABx + ABy * ABy + ABz * ABz; var magnitudeBC = BCx * BCx + BCy * BCy + BCz * BCz; // Find the cosine of // the angle formed // by line AB and BC var angle = dotProduct; angle /= Math.sqrt( magnitudeAB * magnitudeBC); // Find angle in radian angle = (angle * 180) / PI; // Print the angle document.write(Math.abs(angle).toFixed(4));}// Driver Code// Given coordinates// Points Avar x1 = 1, y1 = 3, z1 = 3;// Points Bvar x2 = 3, y2 = 4, z2 = 5;// Points Cvar x3 = 5, y3 = 6, z3 = 9;// Function CallcalculateAngle(x1, y1, z1, x2, y2, z2, x3, y3, z3);</script> |
54.6065
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

