Open In App

Angle between a Pair of Lines

Improve
Improve
Like Article
Like
Save
Share
Report

Given two integers M1 and M2 representing the slope of two lines intersecting at a point, the task is to find the angle between these two lines.

Examples:

Input: M1 = 1.75, M2 = 0.27
Output: 45.1455 degrees

Input: M1 = 0.5, M2 = 1.75
Output: 33.6901 degrees

Approach: If ? is the angle between the two intersecting lines, then the angle ? can be calculated by:

tan? = |(M2 – M1) / (1 + M1 * M2)|
=> ? = tan-1( |(M2 – M1) / (1 + M1 * M2)| )

Follow the steps below to solve the problem:

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
#define PI 3.14159265
 
// Function to find the
// angle between two lines
void findAngle(double M1, double M2)
{
    // Store the tan value  of the angle
    double angle = abs((M2 - M1)
                       / (1 + M1 * M2));
 
    // Calculate tan inverse of the angle
    double ret = atan(angle);
 
    // Convert the angle from
    // radian to degree
    double val = (ret * 180) / PI;
 
    // Print the result
    cout << val;
}
 
// Driver Code
int main()
{
    double M1 = 1.75, M2 = 0.27;
 
    findAngle(M1, M2);
 
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
class GFG
{
    static double PI = 3.14159265;
 
    // Function to find the
    // angle between two lines
    static void findAngle(double M1, double M2)
    {
       
        // Store the tan value  of the angle
        double angle = Math.abs((M2 - M1) / (1 + M1 * M2));
 
        // Calculate tan inverse of the angle
        double ret = Math.atan(angle);
 
        // Convert the angle from
        // radian to degree
        double val = (ret * 180) / PI;
 
        // Print the result
        System.out.println(val);
    }
 
    // Driver Code
    public static void main(String []args)
    {
        double M1 = 1.75, M2 = 0.27;
 
        findAngle(M1, M2);
    }
}
 
// This code is contributed by rrrtnx.


Python3




# Python3 program for the above approach
from math import atan
 
# Function to find the
# angle between two lines
def findAngle(M1, M2):
    PI = 3.14159265
     
    # Store the tan value  of the angle
    angle = abs((M2 - M1) / (1 + M1 * M2))
 
    # Calculate tan inverse of the angle
    ret = atan(angle)
 
    # Convert the angle from
    # radian to degree
    val = (ret * 180) / PI
 
    # Print the result
    print (round(val, 4))
 
# Driver Code
if __name__ == '__main__':
    M1 = 1.75
    M2 = 0.27
 
    findAngle(M1, M2)
 
    # This code is contributed by mohit kumar 29.


C#




// C# program for the above approach
using System;
class GFG
{
    static double PI = 3.14159265;
 
    // Function to find the
    // angle between two lines
    static void findAngle(double M1, double M2)
    {
       
        // Store the tan value  of the angle
        double angle = Math.Abs((M2 - M1) / (1 + M1 * M2));
 
        // Calculate tan inverse of the angle
        double ret = Math.Atan(angle);
 
        // Convert the angle from
        // radian to degree
        double val = (ret * 180) / PI;
 
        // Print the result
        Console.Write(val);
    }
 
    // Driver Code
    public static void Main()
    {
        double M1 = 1.75, M2 = 0.27;
 
        findAngle(M1, M2);
    }
}
 
// This code is contributed by ukasp.


Javascript




<script>
 
      // JavaScript program
      // for the above approach
      const PI = 3.14159265;
 
      // Function to find the
      // angle between two lines
      function findAngle(M1, M2) {
        // Store the tan value of the angle
        var angle = Math.abs((M2 - M1) / (1 + M1 * M2));
 
        // Calculate tan inverse of the angle
        var ret = Math.atan(angle);
 
        // Convert the angle from
        // radian to degree
        var val = (ret * 180) / PI;
 
        // Print the result
        document.write(val.toFixed(4));
      }
 
      // Driver Code
      var M1 = 1.75,
        M2 = 0.27;
      findAngle(M1, M2);
       
</script>


Output: 

45.1455

 

Time Complexity: O(1)
Auxiliary Space: O(1) 



Last Updated : 07 May, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads