Find Tangent at a given point on the curve
Given a curve [ y = x(A – x) ], the task is to find tangent at given point (x, y) on that curve, where A, x, y are integers.
Examples:
Input: A = 2, x = 2, y = 0
Output: y = -2x - 4
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 ) = ((-2))*( Y - 2)
=> y = -2x -4
Input: A = 3, x = 4, y = 5
Output: Not possible
Point is not on that curve
Approach:
- First find if the given point is on that curve or not.
- If the point is on that curve then, Find the derivative
- Calculate the gradient of the tangent by Putting x, y in dy/dx.
- Determine the equation of the tangent by substituting the gradient of the tangent and the coordinates of the given point into the gradient-point form of the straight line equation, where Equation of normal is Y – y = ( dy/dx ) * (X – x).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findTangent( int A, int x, int y)
{
int dif = A - x * 2;
if (y == (2 * x - x * x)) {
if (dif < 0)
cout << "y = "
<< dif << "x" << (x * dif) + (y);
else if (dif > 0)
cout << "y = "
<< dif << "x+" << -x * dif + y;
else
cout << "Not possible" ;
}
}
int main()
{
int A = 2, x = 2, y = 0;
findTangent(A, x, y);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
static void findTangent( int A, int x, int y)
{
int dif = A - x * 2 ;
if (y == ( 2 * x - x * x)) {
if (dif < 0 )
System.out.println( "y = "
+ dif + "x" + (x * dif + y));
else if (dif > 0 )
System.out.println( "y = "
+ dif + "x+" + -x * dif + y);
else
System.out.println( "Not possible" );
}
}
public static void main(String args[])
{
int A = 2 , x = 2 , y = 0 ;
findTangent(A, x, y);
}
}
|
Python3
def findTangent(A, x, y) :
dif = A - x * 2
if y = = ( 2 * x - x * x) :
if dif < 0 :
print ( "y =" ,dif, "x" ,(x * dif) + (y))
elif dif > 0 :
print ( "y =" ,dif, "x+" , - x * dif + y)
else :
print ( "Not Possible" )
if __name__ = = "__main__" :
A, x, y = 2 , 2 , 0
findTangent(A, x, y)
|
C#
using System;
class GFG
{
static void findTangent( int A, int x, int y)
{
int dif = A - x * 2;
if (y == (2 * x - x * x)) {
if (dif < 0)
Console.Write( "y = "
+ dif + "x" + (x * dif + y)+ "\n" );
else if (dif > 0)
Console.Write( "y = "
+ dif + "x+" + -x * dif + y+ "\n" );
else
Console.Write( "Not possible" + "\n" );
}
}
public static void Main()
{
int A = 2, x = 2, y = 0;
findTangent(A, x, y);
}
}
|
PHP
<?php
function findTangent( $A , $x , $y )
{
$dif = $A - $x * 2;
if ( $y == (2 * $x - $x * $x ))
{
if ( $dif < 0)
echo "y = " , $dif , "x" ,
( $x * $dif ) + ( $y );
else if ( $dif > 0)
echo "y = " ,
$dif , "x+" , - $x * $dif + $y ;
else
echo "Not possible" ;
}
}
$A = 2;
$x = 2;
$y = 0;
findTangent( $A , $x , $y );
?>
|
Javascript
<script>
function findTangent( A, x, y)
{
var dif = A - x * 2;
if (y == (2 * x - x * x)) {
if (dif < 0)
document.write( "y = "
+ dif + "x" + (x * dif + y)+ "\n" );
else if (dif > 0)
document.write( "y = "
+ dif + "x+" + -x * dif + y+ "\n" );
else
document.write( "Not possible" + "\n" );
}
}
var A = 2, x = 2, y = 0;
findTangent(A, x, y);
</script>
|
Output:
y = -2x-4
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...