- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# Number of Integral Points between Two Points in C++

In this tutorial, we are going to write a program the finds the number of integral points between the given two points.

The number of points between two given points will be gcd(abs(x2), abs(y1-y2)) - 1.

If the line joining is parallel to x-axis, then the number of integral points will be abs(y1 - y2) - 1.

If the line joining is parallel to y-axis, then the number of integral points will be abs(x1 - x2) - 1

If the x points of both points are equal, then they are parallel to the x-axis. If the y points of both points are equal, then they are parallel to the y-axis.

Let's see an example.

**Input**

pointOne = [1, 5] pointTwo = [1, 3]

**Output**

1

## Algorithm

- Initialise two points.
- Check whether they are parallel to x-axis or not.
- If they are parallel to the x-axis, then use the formula abs(y1 - y2) - 1.
- Check whether they are parallel to y-axis or not.
- If they are parallel to the y-axis, then use the formula abs(x1 - x2) - 1.
- If they are not parallel to any of the axes, then use the formula gcd(abs(x1-x2), abs(y1- y2)) - 1.
- Compute the result and print it.

## Implementation

Following is the implementation of the above algorithm in C++

#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); } int getCount(int pointOne[], int pointTwo[]) { if (pointOne[0] == pointTwo[0]) { return abs(pointOne[1] - pointTwo[1]) - 1; } if (pointOne[1] == pointTwo[1]) { return abs(pointOne[0] - pointTwo[0]) - 1; } return gcd(abs(pointOne[0] - pointTwo[0]), abs(pointOne[1] - pointTwo[1])) - 1; } int main() { int pointOne[] = {1, 3}, pointTwo[] = {10, 12}; cout << getCount(pointOne, pointTwo) << endl; return 0; }

## Output

If you run the above code, then you will get the following result.

8

- Related Articles
- Program to find out the number of integral coordinates on a straight line between two points in Python
- Prime points (Points that split a number into two primes) in C++
- C program to calculate distance between two points
- Find number of endless points in C++
- Program to Find the Shortest Distance Between Two Points in C++
- Swift Program to Calculate Distance Between Two Points
- Number of ordered points pair satisfying line equation in C++
- Shading an area between two points in a Matplotlib plot
- Count of obtuse angles in a circle with ‘k' equidistant points between 2 given points in C++
- What is meant by potential difference between two points?
- C++ Program to Find Number of Articulation points in a Graph
- Printing the correct number of decimal points with cout in C++
- Minimum number of points to be removed to get remaining points on one side of axis using C++.
- How do you create line segments between two points in Matplotlib?
- Reaching Points in C++

Advertisements