473,480 Members | 1,897 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Closest pair problem

2 New Member
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");
}
Nov 2 '06 #1
0 5676

Sign in to post your reply or Sign up for a free account.

Similar topics

13
12290
by: mike | last post by:
I have ListArray with number in Eg: 1, 1.456, 2.43, 4, 6.78 next i have a decimal variable containing one number EG: 1.786 Could someone please tell me how i find the "closest match" number...
4
3436
by: Florent Garcin | last post by:
Hello! I would like to use the map structure with a key of Pair<string, string> and an int as the value. Pair is defined as: template <class T1, class T2> class Pair {
11
9080
by: kietzi | last post by:
Hello world, I was wondering whether it would be possible to create a map which uses a pair of ints as key and a float as value. I have used maps with const char* as key, but have so far not been...
2
1604
by: razael1 | last post by:
#include <utility> #include <vector> using namespace std; vector< pair<int,int v; Generates an error: ISO C++ forbids declaration of 'vector' with no type
18
2021
by: desktop | last post by:
I have made this little test with std::pair: test<intt1(1); test<intt2(2); std::pair<test<int>,test<int mypair = std::make_pair(t1,t2); where test is a template class I wrote. It seems a bit...
1
1687
by: yakitori | last post by:
I'm very sorry because I suspect that this is the same-old-same-old to some of you. I did search first. With MySQL 5.0.27, I have the query shown below. It *almost* does what I want. In fact it...
22
4687
by: Steve Richter | last post by:
Does the .NET framework provide a class which will find the item in the collection with a key which is closest ( greater than or equal, less than or equal ) to the keys of the collection? ex:...
3
2937
by: eiji.anonremail | last post by:
Hi folks, I have a compile problem on linux, and maybe someone has an idea: #include <map> #include <iostream>
5
2835
by: p309444 | last post by:
Hi. I have an application in which the user can select a location and view it's distance from several Points Of Interests (POIs). When I retrieve these distances, I would also like to retrieve...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7043
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6737
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6921
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4776
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4481
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.