Number of Integral Points between Two Points | Class to represent an Integral point on XY plane . ; Utility function to find GCD of two numbers GCD of a and b ; Finds the no . of Integral points between two given points . ; If line joining p and q is parallel to x axis , then count is difference of y values ; If line joining p and q is parallel to y axis , then count is difference of x values ; Driver program to test above
class Point { constructor ( a , b ) { this . x = a ; this . y = b ; } } function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function getCount ( p , q ) { if ( p . x == q . x ) return Math . abs ( p . y - q . y ) - 1 ; if ( p . y == q . y ) return Math . abs ( p . x - q . x ) - 1 ; return gcd ( Math . abs ( p . x - q . x ) , Math . abs ( p . y - q . y ) ) - 1 ; } p = new Point ( 1 , 9 ) ; q = new Point ( 8 , 16 ) ; document . write ( " " + " " + p . x + " " + p . y + " " + q . x + " " + q . y + " " + getCount ( p , q ) ) ;