Find X and Y intercepts of a line passing through the given points | Function to find the X and Y intercepts of the line passing through the given points ; if line is parallel to y axis ; x - intercept will be p [ 0 ] ; y - intercept will be infinity ; if line is parallel to x axis ; x - intercept will be infinity ; y - intercept will be p [ 1 ] ; Slope of the line ; y = mx + c in where c is unknown Use any of the given point to find c ; For finding the x - intercept put y = 0 ; For finding the y - intercept put x = 0 ; Driver code
function getXandYintercept ( P , Q ) { let a = P [ 1 ] - Q [ 1 ] ; let b = P [ 0 ] - Q [ 0 ] ; if ( b == 0 ) { document . write ( P [ 0 ] + " " ) ; document . write ( " " + " " ) ; return ; } if ( a == 0 ) { document . write ( " " + " " ) ; document . write ( P [ 1 ] + " " ) ; return ; } let m = a / ( b * 1.0 ) ; let x = P [ 0 ] ; let y = P [ 1 ] ; let c = y - m * x ; y = 0 ; let r = ( y - c ) / ( m * 1.0 ) ; document . write ( r + " " ) ; x = 0 ; y = parseInt ( m * x + c , 10 ) ; document . write ( c . toFixed ( 11 ) + " " ) ; } let p1 = [ 5 , 2 ] ; let p2 = [ 2 , 7 ] ; getXandYintercept ( p1 , p2 ) ;