Angle between a Pair of Lines
Last Updated :
07 May, 2021
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++
#include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265
void findAngle( double M1, double M2)
{
double angle = abs ((M2 - M1)
/ (1 + M1 * M2));
double ret = atan (angle);
double val = (ret * 180) / PI;
cout << val;
}
int main()
{
double M1 = 1.75, M2 = 0.27;
findAngle(M1, M2);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static double PI = 3.14159265 ;
static void findAngle( double M1, double M2)
{
double angle = Math.abs((M2 - M1) / ( 1 + M1 * M2));
double ret = Math.atan(angle);
double val = (ret * 180 ) / PI;
System.out.println(val);
}
public static void main(String []args)
{
double M1 = 1.75 , M2 = 0.27 ;
findAngle(M1, M2);
}
}
|
Python3
from math import atan
def findAngle(M1, M2):
PI = 3.14159265
angle = abs ((M2 - M1) / ( 1 + M1 * M2))
ret = atan(angle)
val = (ret * 180 ) / PI
print ( round (val, 4 ))
if __name__ = = '__main__' :
M1 = 1.75
M2 = 0.27
findAngle(M1, M2)
|
C#
using System;
class GFG
{
static double PI = 3.14159265;
static void findAngle( double M1, double M2)
{
double angle = Math.Abs((M2 - M1) / (1 + M1 * M2));
double ret = Math.Atan(angle);
double val = (ret * 180) / PI;
Console.Write(val);
}
public static void Main()
{
double M1 = 1.75, M2 = 0.27;
findAngle(M1, M2);
}
}
|
Javascript
<script>
const PI = 3.14159265;
function findAngle(M1, M2) {
var angle = Math.abs((M2 - M1) / (1 + M1 * M2));
var ret = Math.atan(angle);
var val = (ret * 180) / PI;
document.write(val.toFixed(4));
}
var M1 = 1.75,
M2 = 0.27;
findAngle(M1, M2);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...