Find normal at a given point on the curve
Given a curve [ y = x(A – x) ], the task is to find normal at a given point ( x, y) on that curve, where A is an integer number and x, y also any integer.
Examples:
Input: A = 2, x = 2, y = 0
Output: 2y = x - 2
Since y = x(2 - x)
y = 2x - x^2 differentiate it with respect to x
dy/dx = 2 - 2x put x = 2, y = 0 in this equation
dy/dx = 2 - 2* 2 = -2
equation => (Y - 0 ) = ((-1/-2))*( Y - 2)
=> 2y = x -2
Input: A = 3, x = 4, y = 5
Output: Not possible
Point is not on that curve
Approach: First we need to find given point is on that curve or not if the point is on that curve then:
- We need to differentiate that equation that point don’t think too much for differentiation of this equation if you analyze then you find that dy/dx always become A – 2x.
- Put x, y in dy/dx.
- Equation of normal is Y – y = -(1/( dy/dx )) * (X – x).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findNormal( int A, int x, int y)
{
int dif = A - x * 2;
if (y == (2 * x - x * x)) {
if (dif < 0)
cout << 0 - dif << "y = "
<< "x" << (0 - x) + (y * dif);
else if (dif > 0)
cout << dif << "y = "
<< "-x+" << x + dif * y;
else
cout << "x = " << x;
}
else
cout << "Not possible" ;
}
int main()
{
int A = 2, x = 2, y = 0;
findNormal(A, x, y);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void findNormal( int A, int x, int y)
{
int dif = A - x * 2 ;
if (y == ( 2 * x - x * x)) {
if (dif < 0 )
System.out.print( ( 0 - dif) + "y = "
+ "x" +(( 0 - x) + (y * dif)));
else if (dif > 0 )
System.out.print( dif + "y = "
+ "-x+" + (x + dif * y));
else
System.out.print( "x = " +x);
}
else
System.out.println( "Not possible" );
}
public static void main (String[] args) {
int A = 2 , x = 2 , y = 0 ;
findNormal(A, x, y);;
}
}
|
Python3
def findNormal(A, x, y):
dif = A - x * 2
if (y = = ( 2 * x - x * x)):
if (dif < 0 ):
print ( 0 - dif, "y =" , "x" ,
( 0 - x) + (y * dif))
elif (dif > 0 ):
print (dif, "y =" , "- x +" ,
x + dif * y)
else :
print ( "x =" , x)
else :
print ( "Not possible" )
if __name__ = = '__main__' :
A = 2
x = 2
y = 0
findNormal(A, x, y)
|
C#
using System;
class GFG
{
static void findNormal( int A,
int x, int y)
{
int dif = A - x * 2;
if (y == (2 * x - x * x))
{
if (dif < 0)
Console.Write((0 - dif) + "y = " +
"x" + ((0 - x) + (y * dif)));
else if (dif > 0)
Console.Write(dif + "y = " +
"-x + " + (x + dif * y));
else
Console.Write( "x = " + x);
}
else
Console.WriteLine( "Not possible" );
}
static public void Main ()
{
int A = 2, x = 2, y = 0;
findNormal(A, x, y);
}
}
|
PHP
<?php
function findNormal( $A , $x , $y )
{
$dif = $A - $x * 2;
if ( $y == (2 * $x - $x * $x ))
{
if ( $dif < 0)
echo (0 - $dif ), "y = " ,
"x" , (0 - $x ) + ( $y * $dif );
else if ( $dif > 0)
echo $dif , "y = " ,
"-x+" ,( $x + $dif * $y );
else
echo "x = " , $x ;
}
else
echo "Not possible" ;
}
$A = 2;
$x = 2;
$y = 0;
findNormal( $A , $x , $y );
?>
|
Javascript
<script>
function findNormal(A, x, y)
{
let dif = A - x * 2;
if (y == (2 * x - x * x))
{
if (dif < 0)
document.write((0 - dif) + "y = " +
"x" + ((0 - x) + (y * dif)));
else if (dif > 0)
document.write(dif + "y = " +
"-x + " + (x + dif * y));
else
document.write( "x = " + x);
}
else
document.write( "Not possible" );
}
let A = 2, x = 2, y = 0;
findNormal(A, x, y);
</script>
|
Time Complexity : O(1) ,as we are not using any loop.
Auxiliary Space : O(1) ,as we are not using any extra space.
Last Updated :
14 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...