/*
Nama Program : Program Mencari Titik Terdekat
*/.
#include <iostream>
#include <math.h>
using namespace std;
struct Point{
int x,y;
};
void duaTitikTerdekat(Point p[], int n, Point &p1, Point &p2){
double d,dmin,dmax,temp;
int i,j;
dmin=9999;
for(i=1; i<=(n-1); i++){
for(j=i+1; j<=n; j++){
temp=((p[i].x - p[j].x)*(p[i].x - p[j].x))-((p[i].y - p[j].y)*(p[i].y - p[j].y));
d=sqrt(temp);
if(d<dmin){
dmin=d;
p1=p[i];
p2=p[j];
}
}
}
}
int main(){
Point p[100];
int n;
Point p1,p2;
cout<<"Masukkan banyak titik : ";
cin>>n;
cout<<"Masukkan titik - titiknya : "<<endl;
for(int i=0; i<n; i++){
cout<<"Titik ["<<i+1<<"] : "<<endl;
cout<<"x : "; cin>>p[i].x;
cout<<"y : "; cin>>p[i].y;
}
duaTitikTerdekat(p,n,p1,p2);
cout<<endl;
cout<<"Dua titik terdekat = "<<endl;
cout<<"Titik pertama = ( "<<p1.x<<" , " <<p1.y<<" )"<<endl;
cout<<"Titik kedua = ( "<<p2.x<<" , " <<p2.y<<" )";
cout<<endl;
system("PAUSE");
return 0;
}
Screenshot :
0 komentar: