Given two integers D and A representing the perpendicular distance from the origin to a straight line and the angle made by the perpendicular with the positive x-axis respectively, the task is to find the equation of the straight line.
Examples:
Input: D = 10, A = 30 degrees
Output: 0.87x +0.50y = 10
Input: D = 12, A = 45 degrees
Output: 0.71x +0.71y = 12
Approach: The given problem can be solved based on the following observations:

Figure 1
- Let the perpendicular distance be (p) and the angle between the perpendicular and the positive x-axis be (?) degrees.
- Consider a point P with coordinates (x, y) on the required line.
- Draw a perpendicular from P to meet the x-axis at L.
- From L, draw a perpendicular on OQ at M.
- Now, draw a perpendicular from P to meet ML at N.

Figure 2
Now consider right triangle OLM

— (1)
Now consider right triangle PNL


— (2)
Now 
Using equations (1) and (2)
which is the equation of the required line
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
void findLine( int distance, float degree)
{
float x = degree * 3.14159 / 180;
if (degree > 90) {
cout << "Not Possible" ;
return ;
}
float result_1 = sin (x);
float result_2 = cos (x);
cout << fixed << setprecision(2)
<< result_2 << "x +"
<< result_1 << "y = " << distance;
}
int main()
{
int D = 10;
float A = 30;
findLine(D, A);
return 0;
}
|
Java
class GFG{
static void findLine( int distance, float degree)
{
float x = ( float ) (degree * 3.14159 / 180 );
if (degree > 90 ) {
System.out.print( "Not Possible" );
return ;
}
float result_1 = ( float ) Math.sin(x);
float result_2 = ( float ) Math.cos(x);
System.out.print(String.format( "%.2f" ,result_2)+ "x +"
+ String.format( "%.2f" ,result_1)+ "y = " + distance);
}
public static void main(String[] args)
{
int D = 10 ;
float A = 30 ;
findLine(D, A);
}
}
|
Python3
import math
def findLine(distance, degree):
x = degree * 3.14159 / 180
if (degree > 90 ):
print ( "Not Possible" )
return
result_1 = math.sin(x)
result_2 = math.cos(x)
print ( '%.2f' % result_2,
"x +" , '%.2f' % result_1,
"y = " , distance, sep = "")
D = 10
A = 30
findLine(D, A)
|
C#
using System;
class GFG
{
static void findLine( int distance, float degree)
{
float x = ( float )(degree * 3.14159 / 180);
if (degree > 90) {
Console.WriteLine( "Not Possible" );
return ;
}
float result_1 = ( float )(Math.Sin(x));
float result_2 = ( float )(Math.Cos(x));
Console.WriteLine(result_2.ToString( "0.00" ) + "x +"
+ result_1.ToString( "0.00" ) + "y = " + distance);
}
static void Main ()
{
int D = 10;
float A = 30;
findLine(D, A);
}
}
|
Javascript
<script>
function findLine(distance, degree) {
let x = degree * 3.14159 / 180;
if (degree > 90) {
document.write( "Not Possible" );
return ;
}
let result_1 = Math.sin(x);
let result_2 = Math.cos(x);
document.write(result_2.toPrecision(2) + "x + "
+ result_1.toPrecision(2) + "y = " + distance);
}
let D = 10;
let A = 30;
findLine(D, A);
</script>
|
Output: 0.87x +0.50y = 10
Time Complexity: O(1)
Auxiliary Space: O(1)