diff --git a/build/CMakeFiles/HLlib.dir/src/HiddenLines.cpp.o b/build/CMakeFiles/HLlib.dir/src/HiddenLines.cpp.o index 20101bd..98ba2bd 100644 Binary files a/build/CMakeFiles/HLlib.dir/src/HiddenLines.cpp.o and b/build/CMakeFiles/HLlib.dir/src/HiddenLines.cpp.o differ diff --git a/build/CMakeFiles/main.dir/src/main.cpp.o b/build/CMakeFiles/main.dir/src/main.cpp.o index d127b84..f22e1d0 100644 Binary files a/build/CMakeFiles/main.dir/src/main.cpp.o and b/build/CMakeFiles/main.dir/src/main.cpp.o differ diff --git a/build/libHLlib.a b/build/libHLlib.a index 604d473..4307d52 100644 Binary files a/build/libHLlib.a and b/build/libHLlib.a differ diff --git a/build/main b/build/main index 6f00495..37d4c92 100755 Binary files a/build/main and b/build/main differ diff --git a/src/HiddenLines.cpp b/src/HiddenLines.cpp index 3a08104..a36763b 100644 --- a/src/HiddenLines.cpp +++ b/src/HiddenLines.cpp @@ -42,6 +42,34 @@ void HL::print_sol() std::cout << "Line ID " << std::get<0>(*itr) << " visible from x=" << start_print << " to x=" << stop_print << std::endl; } + + std::cout << std::endl; + auto lines = get_lines(); + for(Line line : lines){ + std::cout << "Line ID " << line.get_id() << " visible from x=" << + line.get_vis_start() << " to x=" << line.get_vis_end() << std::endl; + } +} + +void print_sol(std::vector& lines) +{ + for(auto itr = lines.begin(); itr != lines.end(); itr++){ + Line line = *itr; + double start_range = line.get_vis_start(); + std::string start_print = std::to_string(start_range); + if(start_range == std::numeric_limits::lowest()) + start_print = "-inf"; + + double stop_range = line.get_vis_end(); + std::string stop_print = std::to_string(stop_range); + if(stop_range == std::numeric_limits::max()) + stop_print = "inf"; + + if (start_range == stop_range) + continue; + std::cout << "Line ID " << line.get_id() << " visible from x=" << + start_print << " to x=" << stop_print << std::endl; + } } //Divide and Conquer Strategy using recursive call to divide ls and merge using merge() @@ -59,92 +87,53 @@ std::vector HL::gen_sol(std::vector& ls){ auto end_itr = ls.begin(); std::advance(end_itr, split); - for(auto itr = ls.begin(); itr != end_itr; itr++){ + for(std::vector::iterator itr = ls.begin(); itr != end_itr; itr++){ lh.push_back(*itr); } - for(auto itr = end_itr; itr != ls.end(); itr++){ + for(std::vector::iterator itr = end_itr; itr != ls.end(); itr++){ rh.push_back(*itr); } - std::cout << "Left half is size " << lh.size() << " and right half is size " << rh.size() << std::endl; //Recursive call auto merged = merge(inst.gen_sol(lh), inst.gen_sol(rh)); - for (auto itr = merged.begin(); itr != merged.end(); itr++){ + for (std::vector::iterator itr = merged.begin(); itr != merged.end(); itr++){ Line l1 = *itr; - sol.push_back(std::make_pair(l1.get_id(), std::make_pair(l1.get_vis_start(), l1.get_vis_end()))); + + std::vector>>::iterator it = std::find_if(sol.begin(), sol.end(), + [l1](const std::pair>& p ) + { return (std::get<0>(p) == l1.get_id()); }); + + if(it != sol.end()) + *it = std::make_pair(l1.get_id(), std::make_pair(l1.get_vis_start(), l1.get_vis_end())); + else + sol.push_back(std::make_pair(l1.get_id(), std::make_pair(l1.get_vis_start(), l1.get_vis_end()))); } - //Cleanup Solution - /* - std::set>> new_sol; - for(std::set>>::iterator itr = sol.begin(); itr != sol.end(); itr++){ - auto start_range = std::get<0>(std::get<1>(*itr)); - auto stop_range = std::get<1>(std::get<1>(*itr)); - double my_slope = std::get<1>(*line_holder.find(std::get<0>(*itr))).get_slope(); - int my_id = std::get<0>(*itr); - - if(start_range == stop_range) - continue; - - //Handle overlaps - - std::set>>::iterator it = std::find_if(sol.begin(), sol.end(), - [start_range, my_id](const std::pair>& p ) - { return (std::get<0>(std::get<1>(p)) == start_range && std::get<0>(p) != my_id); }); - - if (it != sol.end()){ - double stored_stop = std::get<1>(std::get<1>(*it)); - double stored_slope = std::get<1>(*line_holder.find(std::get<0>(*it))).get_slope(); - int stored_id = std::get<0>(*it); - - std::cout << "Matched" << std::endl; - //Change lines' vis starts and ends - if (my_slope < stored_slope){ - std::get<1>(*line_holder.find(std::get<0>(*itr))).set_vis_start(start_range); - std::get<1>(*line_holder.find(std::get<0>(*itr))).set_vis_end(stored_stop); - new_sol.insert(std::make_pair(my_id, std::make_pair(start_range, stored_stop))); - - std::get<1>(*line_holder.find(std::get<0>(*it))).set_vis_start(stored_stop); - std::get<1>(*line_holder.find(std::get<0>(*it))).set_vis_end(stop_range); - new_sol.insert(std::make_pair(stored_id, std::make_pair(stored_stop, stop_range))); - - } - else{ - std::get<1>(*line_holder.find(std::get<0>(*it))).set_vis_start(start_range); - std::get<1>(*line_holder.find(std::get<0>(*it))).set_vis_end(stored_stop); - new_sol.insert(std::make_pair(stored_id, std::make_pair(start_range, stored_stop))); - - std::get<1>(*line_holder.find(std::get<0>(*itr))).set_vis_start(stored_stop); - std::get<1>(*line_holder.find(std::get<0>(*itr))).set_vis_end(stop_range); - new_sol.insert(std::make_pair(my_id, std::make_pair(stored_stop, stop_range))); - } - } - - else{ - new_sol.insert(*itr); - } - } - inst.set_sol(new_sol); - */ return merged; } //Set with 2 lines is a base case: both are visible at +-inf respectively, and intersection is where they change //visibility else if (ls.size() == 2){ - Line l1 = *(ls.begin()); - Line l2 = *(ls.end()); + std::vector::iterator bit = ls.begin(); + std::vector::iterator eit = std::prev(ls.end()); + Line l1 = *(bit); + Line l2 = *(eit); double isec = (l2.get_ycept() - l1.get_ycept())/(l1.get_slope() - l2.get_slope()); if (l1.get_slope() < l2.get_slope()){ l1.set_vis_end(isec); + *bit = l1; l2.set_vis_start(isec); + *eit = l2; } else{ l2.set_vis_end(isec); l1.set_vis_start(isec); + *bit = l1; + *eit = l2; } //Insert partial solutions @@ -216,12 +205,10 @@ std::vector HL::construct_HWprob(){ << " and a y-intercept of " << line.get_ycept() << std::endl; if (it == lines.begin()){ line.set_vis_start(std::numeric_limits::lowest()); - std::cout << "Changed Line " << line.get_id() << " start vis to " << line.get_vis_start() << std::endl; *it = line; } if (it == (std::prev(lines.end()))){ line.set_vis_end(std::numeric_limits::max()); - std::cout << "Changed Line " << line.get_id() << " stop vis to " << line.get_vis_end() << std::endl; *it = line; } } @@ -233,6 +220,7 @@ std::vector HL::construct_HWprob(){ std::vector merge(std::vector lh, std::vector rh){ std::vector merged; + std::cout << "Merging a left half of size " << lh.size() << " and a right half of size " << rh.size() << std::endl; auto litr = lh.begin(); auto ritr = rh.begin(); @@ -249,7 +237,7 @@ std::vector merge(std::vector lh, std::vector rh){ l2.set_vis_start(isec); } merged.push_back(l1); - merged.push_back(l2); + //merged.push_back(l2); litr++; } @@ -277,7 +265,7 @@ std::vector merge(std::vector lh, std::vector rh){ l2.set_vis_end(isec); } - merged.push_back(l1); + //merged.push_back(l1); merged.push_back(l2); ritr++; } @@ -286,7 +274,7 @@ std::vector merge(std::vector lh, std::vector rh){ return merged; } -std::vector HL::get_lines() const{ +std::vector HL::get_lines(){ return lines; } diff --git a/src/HiddenLines.h b/src/HiddenLines.h index e09a16b..c8eb947 100644 --- a/src/HiddenLines.h +++ b/src/HiddenLines.h @@ -11,10 +11,11 @@ class HL HL(); void print_sol(); + std::vector gen_sol(std::vector& ls); std::vector construct_HWprob(); - std::vector get_lines() const; + std::vector get_lines(); std::vector>> get_sol() const; void set_sol(std::vector>> &new_sol); @@ -28,3 +29,5 @@ class HL }; std::vector merge(std::vector lh, std::vector rh); +void print_sol(std::vector& lines); + diff --git a/src/main.cpp b/src/main.cpp index 2f745b0..74ce6cb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,9 +15,9 @@ int main(int argc, char *argv[]) std::cout << "Generating Solution.\n" << std::endl; auto lines = inst.get_lines(); - inst.gen_sol(lines); + auto sol = inst.gen_sol(lines); std::cout << "Solution is: " << std::endl; - inst.print_sol(); + print_sol(sol); }