Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include <cstdlib>
- #include <cmath>
- using namespace std;
- #define dist(x1, y1, x2, y2) sqrt(pow((x2-x1),2)+pow((y2-y1),2))
- struct Point{
- int x, y;
- }points;
- struct pointSet{
- int n;
- Point *pt;
- }pSet;
- int n, x, y;
- void genPoints(pointSet &ps, int n){
- ps.pt = new Point[n];
- srand(time(0));
- for(int i=0;i<n;i++){
- ps.pt[i].x = rand()%201 - 100;
- ps.pt[i].y = rand()%201 - 100;
- }
- }
- Point match(Point p, pointSet ps){
- int minX, minY;
- double distance = 10e9;
- for(int i=0;i<n;i++){
- if(dist(x, y, ps.pt[i].x, ps.pt[i].y)<distance){
- distance = dist(x, y, ps.pt[i].x, ps.pt[i].y);
- minX = ps.pt[i].x;
- minY = ps.pt[i].y;
- }
- }
- cout << endl;
- printf("The nearest point to (%d,%d) is (%d,%d)\n", x, y, minX, minY);
- }
- int main(){
- cout << "Input the number of points and the testing point (x,y): " << endl;
- while(cin >> n >> x >> y){
- genPoints(pSet, n);
- cout << "The points are: " << endl;
- for(int i=0;i<n;i++){
- cout << "(" << pSet.pt[i].x << "," << pSet.pt[i].y << ")" << endl;
- }
- match(points, pSet);
- cout << endl;
- cout << "Input the number of points and the testing point (x,y): " << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement