Number of Integral Points between Two Points | C ++ code to find the number of integral points lying on the line joining the two given 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
#include <iostream> NEW_LINE #include <cmath> NEW_LINE using namespace std ; class Point { public : int x , y ; Point ( int a = 0 , int b = 0 ) : x ( a ) , y ( b ) { } } ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } int getCount ( Point p , Point q ) { if ( p . x == q . x ) return abs ( p . y - q . y ) - 1 ; if ( p . y == q . y ) return abs ( p . x - q . x ) - 1 ; return gcd ( abs ( p . x - q . x ) , abs ( p . y - q . y ) ) - 1 ; } int main ( ) { Point p ( 1 , 9 ) ; Point q ( 8 , 16 ) ; cout << " The ▁ number ▁ of ▁ integral ▁ points ▁ between ▁ " << " ( " << p . x << " , ▁ " << p . y << " ) ▁ and ▁ ( " << q . x << " , ▁ " << q . y << " ) ▁ is ▁ " << getCount ( p , q ) ; return 0 ; }