32{
34
35 string inputFile;
38 ULong_t seed;
40
41 try {
42
43 JParser<> zap(
"Example program to find smallest distance between two points.");
44
50
51 zap(argc, argv);
52 }
53 catch(const exception &error) {
54 FATAL(error.what() << endl);
55 }
56
57 gRandom->SetSeed(seed);
58
60
61
63
64 typedef vector<JVector2D>::const_iterator const_iterator;
65
66
67 if (inputFile != "") {
68
69 ifstream in(inputFile.c_str());
70
71 for (
double x, y; in >>
x >>
y; ) {
73 }
74
75 in.close();
76
78
79 NOTICE(
"Seed: " << gRandom->GetSeed() << endl);
80
82
83 buffer.push_back(
JVector2D(gRandom->Uniform(-1.0, +1.0),
84 gRandom->Uniform(-1.0, +1.0)));
85 }
86
88
90
91 for (const_iterator i = buffer.begin(); i != buffer.end(); ++i)
92 out << setw(7) << i->getX() << ' '
93 << setw(7) << i->getY() << endl;
94
95 out.close();
96 }
97 }
98
99 if (buffer.size() < 2) {
100 FATAL(
"Not enough points." << endl);
101 }
102
103 for (const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
104 DEBUG(i->getX() <<
' ' << i->getY() << endl);
105 }
106
107
108 {
110
111 timer.start();
112
113 double dmin = numeric_limits<double>::max();
114
115 for (const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
116 for (const_iterator j = i; ++
j != buffer.end(); ) {
117
118 const double d = i->getDistance(*j);
119
120 if (d < dmin) {
121 dmin = d;
122 }
123 }
124 }
125
126 timer.stop();
127
128 cout <<
"Minimal distance " <<
SCIENTIFIC(12,5) << dmin << endl;
129 timer.print(cout);
130 }
131
132
133 JTimer timer(
"O(n log(n))");
134
135 timer.start();
136
137 const double dmin = getSmallestDistance2D(buffer.begin(), buffer.end());
138
139 timer.stop();
140
141 cout <<
"Minimal distance " <<
SCIENTIFIC(12,5) << dmin << endl;
142 timer.print(cout);
143
144
146
147 cout << "pair "
149 <<
"(" <<
FIXED(7,5) <<
result.second->getX() <<
"," <<
FIXED(7,5) <<
result.second->getY() <<
")" << endl;
151
152
153 TApplication* tp = new TApplication("user", NULL, NULL);
154
155 TCanvas cv("cv", "", 400, 400);
156
157 cv.SetFillStyle(4000);
158 cv.SetFillColor(0);
159
160 cv.Divide(1, 1);
161 cv.cd(1);
162
163
164 const Int_t MAX_BUFFER_SIZE = buffer.size() + 1;
165
166 Double_t
x[MAX_BUFFER_SIZE];
167 Double_t
y[MAX_BUFFER_SIZE];
168
169 int N = 0;
170
171 for (const_iterator i = buffer.begin(); i != buffer.end(); ++i, ++N) {
174 }
175
176
177 Double_t
xmin = -1.1;
178 Double_t
xmax = +1.1;
179 Double_t ymin = -1.1;
180 Double_t ymax = +1.1;
181
182 TH2D h2("h2", "", 1000, xmin, xmax, 1000, ymin, ymax);
183
184 h2.SetStats(kFALSE);
185 h2.Draw();
186
187
188 TGraph g(N, x, y);
189
190 g.SetMarkerStyle(20);
191 g.SetMarkerColor(kBlack);
192 g.SetMarkerSize(0.7);
193 g.Draw("P");
194
195 TMarker m1(
result.first ->getX(),
result.first ->getY(), 20);
196 TMarker m2(
result.second->getX(),
result.second->getY(), 20);
197
198 m1.SetMarkerColor(kRed);
199 m2.SetMarkerColor(kRed);
200
201 m1.SetMarkerSize(0.7);
202 m2.SetMarkerSize(0.7);
203
204 m1.Draw();
205 m2.Draw();
206
207 cv.Update();
208
209 tp->Run();
210}
#define DEBUG(A)
Message macros.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Auxiliary class for CPU timing and usage.
Data structure for vector in two dimensions.
Utility class to parse command line options.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary data structure for floating point format specification.
Auxiliary data structure for floating point format specification.