Horje
Angle between a Pair of Lines

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) 




Reffered: https://www.geeksforgeeks.org


Geometric

Related
Maximum the value of a given expression for any pair of coordinates on a 2D plane Maximum the value of a given expression for any pair of coordinates on a 2D plane
Check if a point is inside, outside or on a Hyperbola Check if a point is inside, outside or on a Hyperbola
Minimum area of the triangle formed by any tangent to an ellipse with the coordinate axes Minimum area of the triangle formed by any tangent to an ellipse with the coordinate axes
Maximize count of intersecting line segments Maximize count of intersecting line segments
Program to calculate angle between two N-Dimensional vectors Program to calculate angle between two N-Dimensional vectors

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13