Find X and Y intercepts of a line passing through the given points | Java implementation of the approach ; 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
class GFG { static void getXandYintercept ( int P [ ] , int Q [ ] ) { int a = P [ 1 ] - Q [ 1 ] ; int b = P [ 0 ] - Q [ 0 ] ; if ( b == 0 ) { System . out . println ( P [ 0 ] ) ; System . out . println ( " infinity " ) ; return ; } if ( a == 0 ) { System . out . println ( " infinity " ) ; System . out . println ( P [ 1 ] ) ; return ; } double m = a / ( b * 1.0 ) ; int x = P [ 0 ] ; int y = P [ 1 ] ; double c = y - m * x ; y = 0 ; double r = ( y - c ) / ( m * 1.0 ) ; System . out . println ( r ) ; x = 0 ; y = ( int ) ( m * x + c ) ; System . out . print ( c ) ; } public static void main ( String [ ] args ) { int p1 [ ] = { 5 , 2 } ; int p2 [ ] = { 2 , 7 } ; getXandYintercept ( p1 , p2 ) ; } }