diff --git a/build/CMakeFiles/HLlib.dir/src/HiddenLines.cpp.o b/build/CMakeFiles/HLlib.dir/src/HiddenLines.cpp.o index 446f934..928d673 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 f22e1d0..85cccfb 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 36a817b..680b9cb 100644 Binary files a/build/libHLlib.a and b/build/libHLlib.a differ diff --git a/build/main b/build/main index 2df4b94..b722624 100755 Binary files a/build/main and b/build/main differ diff --git a/src/HiddenLines.cpp b/src/HiddenLines.cpp index 87a04cc..76cd1b1 100644 --- a/src/HiddenLines.cpp +++ b/src/HiddenLines.cpp @@ -17,40 +17,6 @@ HL::HL() lines = construct_HWprob(); } -std::map HL::get_map() -{ - return line_holder; -} - - -void HL::print_sol() -{ - auto sol = get_sol(); - for(auto itr = sol.begin(); itr != sol.end(); itr++){ - double start_range = std::get<0>(std::get<1>(*itr)); - std::string start_print = std::to_string(start_range); - if(start_range == std::numeric_limits::lowest()) - start_print = "-inf"; - - double stop_range = std::get<1>(std::get<1>(*itr)); - 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 " << 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++){ @@ -75,22 +41,24 @@ void print_sol(std::vector& lines) //Divide and Conquer Strategy using recursive call to divide ls and merge using merge() std::vector HL::gen_sol(std::vector& ls){ HL inst = *this; - auto line_holder = inst.get_map(); + //Case 1: We need to divide more if (ls.size() > 2){ //Create left and right half sets int split = ceil(ls.size()/2); std::vector lh; std::vector rh; - //No index access for sets + //Get iterator for the split point location auto end_itr = ls.begin(); std::advance(end_itr, split); + //Create left half for(std::vector::iterator itr = ls.begin(); itr != end_itr; itr++){ lh.push_back(*itr); } + //Create right half for(std::vector::iterator itr = end_itr; itr != ls.end(); itr++){ rh.push_back(*itr); } @@ -98,31 +66,22 @@ std::vector HL::gen_sol(std::vector& ls){ //Recursive call auto merged = merge(inst.gen_sol(lh), inst.gen_sol(rh)); - for (std::vector::iterator itr = merged.begin(); itr != merged.end(); itr++){ - Line l1 = *itr; - - 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()))); - } - 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){ + //Get non-const iterators to the beginning and end of our vector 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()); + //Get intersection of our two lines + double isec = l1.get_isec(l2); + + //If Line 1 has smaller slope, its visibility end in comparison to Line 2 is the intersection. Vice versa for Line 2. if (l1.get_slope() < l2.get_slope()){ l1.set_vis_end(isec); *bit = l1; @@ -136,14 +95,10 @@ std::vector HL::gen_sol(std::vector& ls){ *eit = l2; } - //Insert partial solutions - sol.push_back(std::make_pair(l1.get_id(), std::make_pair(l1.get_vis_start(), l1.get_vis_end()))); - sol.push_back(std::make_pair(l2.get_id(), std::make_pair(l2.get_vis_start(), l2.get_vis_end()))); - return ls; } - //Set with 1 line: Just return + //Set with 1 line: Just return - no comparisons to make at this point. else{ return ls; } @@ -187,10 +142,6 @@ std::vector HL::construct_HWprob(){ Line newline = Line(slope, ycept); newline.set_id(); sorted_lines.insert(newline); - - auto line_holder = get_map(); - //line_holder[newline.get_id()] = newline; - line_holder.insert(std::pair (newline.get_id(), newline)); } //Copy set to vector @@ -220,6 +171,7 @@ std::vector HL::construct_HWprob(){ std::vector merge(std::vector lh, std::vector rh){ std::vector merged; + //Get non-const iterators to the start of both vectors std::vector::iterator litr = lh.begin(); std::vector::iterator ritr = rh.begin(); @@ -246,11 +198,13 @@ std::vector merge(std::vector lh, std::vector rh){ if (l1.get_ycept() > l2.get_ycept()){ l2.set_vis_start(0); l2.set_vis_end(0); + *ritr = l2; merged.push_back(l1); } else{ l1.set_vis_start(0); l1.set_vis_end(0); + *litr = l1; merged.push_back(l2); } @@ -289,33 +243,33 @@ std::vector HL::get_lines(){ return lines; } -std::vector>> HL::get_sol() const{ - return sol; -} - -void HL::set_sol(std::vector>> &new_sol) -{ - sol = new_sol; -} - std::vector remove_invis(std::vector &merged){ - std::vector tmp_vis; - tmp_vis.push_back(merged.front()); - merged.erase(merged.begin()); - tmp_vis.push_back(merged.front()); - merged.erase(merged.begin()); + //Create separate visible line holder + std::vector tmp_vis; + + //Since we call this function from a merge, we can safely add two elements. + tmp_vis.push_back(merged.front()); + merged.erase(merged.begin()); + tmp_vis.push_back(merged.front()); + merged.erase(merged.begin()); int num_vis = 2; - for(Line line : merged){ + //Loop through the remainder of the original vector + for(Line &line : merged){ + //Pull out the back two lines Line last = tmp_vis.at(num_vis -1); Line seclast = tmp_vis.at(num_vis -2); + //Intersection of our "known" visible lines vs the intersection of the 2nd to last vis line and the original vec double vis_isec = last.get_isec(seclast); double merg_isec = line.get_isec(seclast); + while(merg_isec < vis_isec){ + //line intersects before the last line, and since line slope > last line slope, remove it tmp_vis.erase(tmp_vis.begin() + (num_vis -1)); num_vis--; + line.set_vis_start(merg_isec); if(num_vis == 1) break; diff --git a/src/HiddenLines.h b/src/HiddenLines.h index 36dcab3..c6af5a5 100644 --- a/src/HiddenLines.h +++ b/src/HiddenLines.h @@ -10,22 +10,13 @@ class HL public: HL(); - void print_sol(); - std::vector gen_sol(std::vector& ls); std::vector construct_HWprob(); std::vector get_lines(); - std::vector>> get_sol() const; - void set_sol(std::vector>> &new_sol); - - std::map get_map(); private: - //Solution is in form (Line ID, (vis_start, vis_end)) - std::vector>> sol; std::vector lines; - std::map line_holder; }; std::vector merge(std::vector lh, std::vector rh);