Hello;
The code below is one of solving the closest pair problem but some parts missing can any one help me out.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define MAXPOINTS 30
#define NMAX 400
struct point_t {
double x, y;
} points[MAXPOINTS];
#define SQR(x) ((x)*(x))
#define DIST(p1,p2) sqrt(SQR((p1).x-(p2).x) + SQR((p1).y-(p2).y))
/* Generate a random set of points. */
void generate_random_points(void)
{
int i;
srandom(time(NULL)); /* Seed the random number generator */
for (i = 0; i < MAXPOINTS; i++) {
points[i].x = random() % NMAX;
points[i].y = random() % NMAX;
}
}
void print_points(int l, int r);
int main(void)
{
generate_random_points();
/* Find the closest pair, then call print points to print the points */
return 0;
}
/* Draws the points and a line between the closest pair.
* parameters: l,r are indexes of the closest points in our array.
*/
void print_points(int l, int r)
{
int i;
FILE *f = fopen("shortest.svg", "w");
fprintf(f,"<?xml version='1.0'?>\n");
fprintf(f,"<svg xmlns='http://www.w3.org/2000/svg'>\n");
fprintf(f, "<g id=\"x\">\n");
for (i = 0; i < MAXPOINTS; i++) {
struct point_t p = points[i];
/* Print the point as a circle of radius 2. */
fprintf(f, "<circle cx=\"%.2f\" cy=\"%.2f\" r=\"2\"/>\n",
p.x, p.y);
}
/* Now print a line between the rtwo closest points. */
fprintf(f, "<path d=\"M %.2f %.2f ",
points[l].x, points[l].y); /* SVG move to. */
fprintf(f, "L %.2f %.2f ", points[r].x, points[r].y);
fprintf(f, "\" fill=\"none\" stroke=\"red\"/>\n");
fprintf(f, "</g>\n</svg>\n");
fclose(f);
fprintf(stderr, "Output saved to 'shortest.svg'\n");
}