-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIntegral Points between Two Points
53 lines (45 loc) · 1.11 KB
/
Integral Points between Two Points
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <cmath>
using namespace std;
// Class to represent an Integral point on XY plane.
class Point
{
public:
int x, y;
Point(int a=0, int b=0):x(a),y(b) {}
};
// Utility function to find GCD of two numbers
// GCD of a and b
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a%b);
}
// Finds the no. of Integral points between
// two given points.
int getCount(Point p, Point q)
{
// If line joining p and q is parallel to
// x axis, then count is difference of y
// values
if (p.x==q.x)
return abs(p.y - q.y) - 1;
// If line joining p and q is parallel to
// y axis, then count is difference of x
// values
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;
}
// Driver program to test above
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;
}