#include <bits/stdc++.h>
using namespace std;

// Making a class that stores X and Y coordinates of different points
class Coordinate
{
public:
    int x, y;
};

Coordinate p1;
Coordinate p2;

// find distance between two points
float distance(Coordinate p1, Coordinate p2)
{
    return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}

// Brute force algorithm and find distance using it
float brute_force(Coordinate P[], int n)
{

    float mini = FLT_MAX;
    for (int i = 0; i < n; ++i)
        for (int j = i + 1; j < n; ++j)
            if (distance(P[i], P[j]) < mini)
            {
                mini = distance(P[i], P[j]);
                if (distance(p1, p2) > distance(P[i], P[j]))
                {
                    p1.x = P[i].x;
                    p1.y = P[i].y;
                    p2.x = P[j].x;
                    p2.y = P[j].y;
                }
            }
    return mini;
}

// function to compare X coodinate for quick sort
int X_cmp(const void *a, const void *b)
{
    Coordinate *p1 = (Coordinate *)a, *p2 = (Coordinate *)b;
    return (p1->x - p2->x);
}

// function to compare Y coodinate for quick sort
int Y_cmp(const void *a, const void *b)
{
    Coordinate *p1 = (Coordinate *)a, *p2 = (Coordinate *)b;
    return (p1->y - p2->y);
}

// Function to make the strip
float makeverticalstrip(Coordinate v_strip[], int size, float d)
{
    float mini = d; // Initialize the minimum distance as d

    for (int i = 0; i < size; ++i)
    {
        for (int j = i + 1; j < size && (v_strip[j].y - v_strip[i].y) < mini; ++j)
        {
            if (distance(v_strip[i], v_strip[j]) < mini)
            {
                mini = distance(v_strip[i], v_strip[j]);
                p1.x = v_strip[i].x;
                p1.y = v_strip[i].y;
                p2.x = v_strip[j].x;
                p2.y = v_strip[j].y;
            }
        }
    }

    return mini;
}

// Recursive function to divide the plane
float Min(Coordinate Px[], Coordinate Py[], int n)
{
    if (n <= 3)
    {
        return brute_force(Px, n); // Divide until there exist 3 or lesser than 3 points in the partition
    }

    // Make the vertical (partitioning) line
    int middle = n / 2;
    Coordinate middle_point = Px[middle];
    // simple merge sort like algorithm 110-119
    Coordinate Pyl[middle];
    Coordinate Pyr[n - middle];
    int li = 0, ri = 0;
    for (int i = 0; i < n; i++)
    {
        if (Py[i].x <= middle_point.x && li < middle)
            Pyl[li++] = Py[i];
        else
            Pyr[ri++] = Py[i];
    }

    // Getting the minimum distance for the left and right partition
    float dl = Min(Px, Pyl, middle);
    float dr = Min(Px + middle, Pyr, n - middle);

    float d;
    if (dl < dr)
    {
        d = dl;
    }
    else
    {
        d = dr;
    }

    // Making a strip that stores points on the left and right side of the partition which are at lesser distance than d.
    Coordinate v_strip[n];
    int j = 0;
    for (int i = 0; i < n; i++)
        if (abs(Py[i].x - middle_point.x) < d)
        {
            v_strip[j] = Py[i];
            j++;
        }

    return makeverticalstrip(v_strip, j, d);
}

float closestdistance(Coordinate P[], int n)
{
    Coordinate Px[n];
    Coordinate Py[n];
    for (int i = 0; i < n; i++)
    {
        Px[i] = P[i];
        Py[i] = P[i];
    }

    // Sorting the points according to their X and Y coordinates.
    qsort(Px, n, sizeof(Coordinate), X_cmp);
    qsort(Py, n, sizeof(Coordinate), Y_cmp);

    return Min(Px, Py, n);
}

int main()
{

    int n;
    // cout << "Enter the number of point you want to add in plane" << endl;
    cin >> n;

    Coordinate P[n];
    // cout << "Enter the points' x and y coordinates" << endl;
    // for example we will give the co-ordinate of the airplane
    for (int i = 0; i < n; i++)
    {
        cin >> P[i].x;
        cin >> P[i].y;
    }

    // storing first 2 points (if exists)
    p1.x = P[0].x;
    p1.y = P[0].y;

    p2.x = P[1].x;
    p2.y = P[1].y;
    
    cout << "The Smallest Distance Is " << closestdistance(P, n);
    cout << "\n";
    cout << "The Co-Ordinates Of The Points With The Minimum Distance Between Them Are\n";
    cout << "First Point : (" << p1.x << "," << p1.y << ")\n";
    cout << "Second Point : (" << p2.x << "," << p2.y << ")\n";

    return 0;
} 
by

C++ Online Compiler

Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!

Read inputs from stdin

OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string name;
    cout << "Enter name:";
    getline (cin, name);
    cout << "Hello " << name;
    return 0;
}

About C++

C++ is a widely used middle-level programming language.

  • Supports different platforms like Windows, various Linux flavours, MacOS etc
  • C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
  • Case-sensitive
  • C++ is a compiler based language
  • C++ supports structured programming language
  • C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
  • Like C, C++ also allows you to play with memory using Pointers.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition If-Else is used.

if(conditional-expression) {
   //code
}
else {
   //code
}

You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to If-Else-If ladder.

switch(conditional-expression){    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
......    
    
default:     
 code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  //code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while (condition) {  
// code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {  
 // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.

How to declare a Function:

return_type function_name(parameters);

How to call a Function:

function_name (parameters)

How to define a Function:

return_type function_name(parameters) {  
 // code
}