|
6 | 6 |
|
7 | 7 | struct Point
|
8 | 8 | {
|
9 |
| - float x; |
10 |
| - float y; |
| 9 | + float x; |
| 10 | + float y; |
11 | 11 | };
|
12 | 12 |
|
13 | 13 | Point make_random_point()
|
14 | 14 | {
|
15 |
| - return Point{ |
16 |
| - (static_cast<float>(rand()) / static_cast<float>(RAND_MAX)) * 10.0f, |
17 |
| - (static_cast<float>(rand()) / static_cast<float>(RAND_MAX)) * |
18 |
| - 10.0f}; |
| 15 | + return Point{ |
| 16 | + (static_cast<float>(rand()) / static_cast<float>(RAND_MAX)) * 10.0f, |
| 17 | + (static_cast<float>(rand()) / static_cast<float>(RAND_MAX)) * 10.0f}; |
19 | 18 | }
|
20 | 19 |
|
21 | 20 | int main()
|
22 | 21 | {
|
23 |
| - srand(static_cast<unsigned>(time(0))); |
24 |
| - std::ofstream file("kdtree.txt"); |
25 |
| - // Tree from the Wikipedia article |
26 |
| - // std::vector<Point> points = {{2,3}, {5,4}, {9,6}, {4,7}, {8,1}, {7,2} |
27 |
| - //}; |
| 22 | + srand(static_cast<unsigned>(time(0))); |
| 23 | + std::ofstream file("kdtree.txt"); |
| 24 | + // Tree from the Wikipedia article |
| 25 | + // std::vector<Point> points = {{2,3}, {5,4}, {9,6}, {4,7}, {8,1}, {7,2} |
| 26 | + //}; |
28 | 27 |
|
29 |
| - // Generate random points |
30 |
| - file << "# points\n"; |
31 |
| - std::vector<Point> points; |
32 |
| - for (size_t i = 0; i < 550; ++i) |
33 |
| - { |
34 |
| - points.emplace_back(make_random_point()); |
35 |
| - file << points[i].x << "," << points[i].y << '\n'; |
36 |
| - } |
| 28 | + // Generate random points |
| 29 | + file << "# points\n"; |
| 30 | + std::vector<Point> points; |
| 31 | + for (size_t i = 0; i < 550; ++i) |
| 32 | + { |
| 33 | + points.emplace_back(make_random_point()); |
| 34 | + file << points[i].x << "," << points[i].y << '\n'; |
| 35 | + } |
37 | 36 |
|
38 |
| - // Define function to grab object's coordinates |
39 |
| - auto get_coord = [](const Point& p, int axis) |
40 |
| - { |
41 |
| - if (axis == 0) return p.x; |
42 |
| - return p.y; |
43 |
| - }; |
44 |
| - // Create Kdtree |
45 |
| - kdtree::Kdtree<Point, /* Dimension */ 2, decltype(get_coord)> tree( |
46 |
| - points, get_coord); |
| 37 | + // Define function to grab object's coordinates |
| 38 | + auto get_coord = [](const Point& p, int axis) |
| 39 | + { |
| 40 | + if (axis == 0) return p.x; |
| 41 | + return p.y; |
| 42 | + }; |
| 43 | + // Create Kdtree |
| 44 | + kdtree::Kdtree<Point, /* Dimension */ 2, decltype(get_coord)> tree( |
| 45 | + points, get_coord); |
47 | 46 |
|
48 |
| - // Generate point whose neighbors we want to find |
49 |
| - file << "# gold\n"; |
50 |
| - Point gold = make_random_point(); |
51 |
| - file << gold.x << "," << gold.y << '\n'; |
| 47 | + // Generate point whose neighbors we want to find |
| 48 | + file << "# gold\n"; |
| 49 | + Point gold = make_random_point(); |
| 50 | + file << gold.x << "," << gold.y << '\n'; |
52 | 51 |
|
53 |
| - // Find the nearest neighbor |
54 |
| - file << "# nn\n"; |
55 |
| - const Point& nn = tree.find_nn(gold); |
56 |
| - file << nn.x << "," << nn.y << '\n'; |
| 52 | + // Find the nearest neighbor |
| 53 | + file << "# nn\n"; |
| 54 | + const Point& nn = tree.find_nn(gold); |
| 55 | + file << nn.x << "," << nn.y << '\n'; |
57 | 56 |
|
58 |
| - // Find all the neighbors within 'radius' distance |
59 |
| - float radius = 2.0; |
60 |
| - file << "# neighbors\n"; |
61 |
| - file << radius << '\n'; |
62 |
| - std::vector<size_t> neighbors = tree.find_neighbors(gold, radius); |
63 |
| - for (auto&& ind : neighbors) |
64 |
| - { |
65 |
| - auto& neigh = points[ind]; |
66 |
| - file << neigh.x << "," << neigh.y << '\n'; |
67 |
| - } |
| 57 | + // Find all the neighbors within 'radius' distance |
| 58 | + float radius = 2.0; |
| 59 | + file << "# neighbors\n"; |
| 60 | + file << radius << '\n'; |
| 61 | + std::vector<size_t> neighbors = tree.find_neighbors(gold, radius); |
| 62 | + for (auto&& ind : neighbors) |
| 63 | + { |
| 64 | + auto& neigh = points[ind]; |
| 65 | + file << neigh.x << "," << neigh.y << '\n'; |
| 66 | + } |
68 | 67 |
|
69 |
| - return 0; |
| 68 | + return 0; |
70 | 69 | }
|
0 commit comments