-
Notifications
You must be signed in to change notification settings - Fork 0
/
closest_points_in_a_plane.cpp
50 lines (39 loc) · 1.17 KB
/
closest_points_in_a_plane.cpp
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
#include<bits/stdc++.h>
using namespace std;
double dist(pair<int,int> p1,pair<int,int> p2){
int x1,x2,y1,y2;
x1=p1.first;
x2=p2.first;
y1=p1.second;
y2=p2.second;
double d = pow((x2-x1),2) + pow((y2-y1),2);
return pow(d,0.5);
}
//closest points
double closest(pair<int,int> p[],int left,int right){
if(left==right)
return INT_MAX;
int center = (left+right)/2;
double leftdist = closest(p,left,center);
double rightdist = closest(p,center+1,right);
//now cross dist
pair<int,int> leftcord = p[center];
for(int i = center;i>=left;i--){
if(p[center].first == p[center-i].first && p[center].second > p[center-i].second){
leftcord = p[center-i];
}
}
pair<int,int> rightcord = p[center+1];
for(int i = center+1;i<=right;i++){
if(p[center+1].first == p[center+1+i].first && p[center+1].second > p[center+1+i].second){
rightcord = p[center+1+i];
}
}
double a = min(leftdist,rightdist);
return min(a,dist(leftcord,rightcord));
}
int main(){
pair<int,int> p[] = {{0,0},{2,1},{4,3},{10,9},{12,10},{14,9}};
cout<<closest(p,0,5);
return 0;
}