Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

#include <iostream>

#include <climits>
#include <cmath>
using namespace std;
int MAX=10000;
struct Point
{
double x, y;
};
inline int urutX(const void* a, const void* b)
{
Point *p1 = (Point *)a, *p2 = (Point *)b;
return (p1->x - p2->x);
}
inline int urutY(const void* a, const void* b)
{
Point *p1 = (Point *)a, *p2 = (Point *)b;
return (p1->y - p2->y);
}

inline double jarak(Point p1, Point p2)
{
return sqrt( (p1.x - p2.x)*(p1.x - p2.x) +
(p1.y - p2.y)*(p1.y - p2.y) );
}

double cariLangsung(Point P[], int n, Point &p1, Point &p2)
{
double min = MAX;
for (int i = 0; i < n; ++i)
for (int j = i+1; j < n; ++j)
if (jarak(P[i], P[j]) < min) {
min = jarak(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 min;
}

inline double min(double x, double y)
{
return (x < y)? x : y;
}


double stripPoints(Point strip[], int size, double d, Point &p1, Point &p2)
{
double min = d;

qsort(strip, size, sizeof(Point), urutY);


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

return min;
}

double jarak_terdekat(Point P[], Point strip[], int n, Point &p1, Point &p2)
{
static Point ptemp1, ptemp2, ptemp3, ptemp4;
if (n <= 3)
{
double a= cariLangsung(P, n, ptemp1, ptemp2);
p1.x = ptemp1.x; p1.y = ptemp1.y;
p2.x = ptemp2.x; p2.y = ptemp2.y;
return a;
}

else{

int mid = n/2;
Point midPoint = P[mid];

double dl = jarak_terdekat(P, strip, mid, ptemp1, ptemp2);
double dr = jarak_terdekat(P + mid, strip, n-mid, ptemp3, ptemp4);


if(dl < dr) {
p1.x = ptemp1.x; p1.y = ptemp1.y;
p2.x = ptemp2.x; p2.y = ptemp2.y;
}
else {
p1.x = ptemp3.x; p1.y = ptemp3.y;
p2.x = ptemp4.x; p2.y = ptemp4.y;
}
double dmin = min(dl, dr);

int j = 0;
for (int i = 0; i < n; i++)
if (abs(P[i].x - midPoint.x) < dmin)
strip[j++] = P[i];

double dmin_strip = stripPoints(strip, j, dmin, ptemp1, ptemp2);
double final_min = dmin;
if(dmin_strip < dmin) {
p1.x = ptemp1.x; p1.y = ptemp1.y;
p2.x = ptemp2.x; p2.y = ptemp2.y;
final_min = dmin_strip;
}
return final_min;
}
}

int main()
{
int banyak;
int ulang=0;
Point P[banyak];
do
{

cout<<"Masukkan banyaknya titik : ";
cin>>banyak;
if(banyak<2)
{
cout<<"Maaf, banyak titik minimal adalah 2"<<endl;
}
else
{
for (int i=0;i<banyak;i++)
{
cout<<"Masukkan absis dari titik ke-"<<i+1<<" : ";
cin>>P[i].x;
cout<<"Masukkan ordinat dari titik ke-"<<i+1<<" : ";
cin>>P[i].y;
ulang=1;
}
}
}while(ulang==0);

Point p1 = {MAX, MAX}, p2 = {MAX, MAX};
int n = banyak;

qsort(P, n, sizeof(Point), urutX);

Point *strip = new Point[n];

cout << "Jarak titik terdekat adalah " << jarak_terdekat(P, strip, n, p1, p2
) << endl;
cout << "Yaitu antara titik (" << p1.x << "," << p1.y << ") dan ("
<< p2.x << "," << p2.y << ")" << endl;
delete[] strip;
system("PAUSE");
return EXIT_SUCCESS;
}

You might also like