Length of the normal from origin on a straight line whose intercepts are given
Last Updated :
08 Jun, 2022
Given the intercepts of a straight line on both the axes as m and n. The task is to find the length of the normal on this straight line from the origin.
Examples:
Input: m = 5, n = 3
Output: 2.57248
Input: m = 13, n = 9
Output: 7.39973
Approach: A normal to a line is a line segment drawn from a point perpendicular to the given line.
Let p be the length of the normal drawn from the origin to a line, which subtends an angle ? with the positive direction of x-axis as follows.
Then, we have cos ? = p / m and sin ? = p / n
Since, sin2 ? + cos2 ? = 1
So, (p / m)2 + (p / n)2 = 1
We get, p = m * n / ?(m2 + n2)
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float normal( float m, float n)
{
float N = (fabsf(m) * fabsf(n))
/ sqrt ((fabsf(m) * fabsf(m))
+ (fabsf(n) * fabsf(n)));
return N;
}
int main()
{
float m = -5, n = 3;
cout << normal(m, n);
return 0;
}
|
Java
class GFG
{
static float normal( float m, float n)
{
float N = ( float ) ((Math.abs(m) * Math.abs(n))
/ Math.sqrt((Math.abs(m) * Math.abs(m))
+ (Math.abs(n) * Math.abs(n))));
return N;
}
public static void main(String[] args)
{
float m = - 5 , n = 3 ;
System.out.println(normal(m, n));
}
}
|
Python3
import math;
def normal(m, n):
N = (( abs (m) * abs (n)) /
math.sqrt(( abs (m) * abs (m)) +
( abs (n) * abs (n))));
return N;
m = - 5 ; n = 3 ;
print (normal(m, n));
|
C#
using System;
class GFG
{
static float normal( float m, float n)
{
float N = ( float )((Math.Abs(m) * Math.Abs(n)) /
Math.Sqrt((Math.Abs(m) * Math.Abs(m)) +
(Math.Abs(n) * Math.Abs(n))));
return N;
}
public static void Main()
{
float m = -5, n = 3;
Console.Write(normal(m, n));
}
}
|
PHP
<?php
function normal( $m , $n )
{
$N = ( abs ( $m ) * abs ( $n )) /
sqrt(( abs ( $m ) * abs ( $m )) +
( abs ( $n ) * abs ( $n )));
return $N ;
}
$m = -5; $n = 3;
echo normal( $m , $n );
?>
|
Javascript
<script>
function normal(m , n)
{
var N = ((Math.abs(m) * Math.abs(n))
/ Math.sqrt((Math.abs(m) * Math.abs(m))
+ (Math.abs(n) * Math.abs(n))));
return N;
}
var m = -5, n = 3;
document.write(normal(m, n).toFixed(5));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...