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 degreesInput: 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:
- Initialize a variable, say A, to store the value of the angle between the two lines.
- Using the above formula, find the value of tan(A) and update the value of angle A by taking the tan inverse of the angle.
- Convert the angle from radian to degrees.
- Print the value of A in degrees as the result.
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)
Please Login to comment...