Code cleanup and updating comments
This commit is contained in:
parent
31e585280d
commit
99f427bea4
Binary file not shown.
BIN
build/libHLlib.a
BIN
build/libHLlib.a
Binary file not shown.
BIN
build/main
BIN
build/main
Binary file not shown.
@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
HL::HL()
|
HL::HL()
|
||||||
{
|
{
|
||||||
|
//Read from CSV and construct the problem from the given lines
|
||||||
lines = construct_HWprob();
|
lines = construct_HWprob();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -19,10 +20,12 @@ void HL::print_sol()
|
|||||||
std::cout << "Not yet implemented" << std::endl;
|
std::cout << "Not yet implemented" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Divide and Conquer Strategy using recursive call to divide ls and merge using merge()
|
||||||
std::set<Line> HL::gen_sol(std::set<Line>& ls){
|
std::set<Line> HL::gen_sol(std::set<Line>& ls){
|
||||||
if (ls.size() > 2){
|
if (ls.size() > 2){
|
||||||
HL inst = *this;
|
HL inst = *this;
|
||||||
|
|
||||||
|
//Create left and right half sets
|
||||||
int split = ceil(ls.size()/2);
|
int split = ceil(ls.size()/2);
|
||||||
std::set<Line> lh;
|
std::set<Line> lh;
|
||||||
std::set<Line> rh;
|
std::set<Line> rh;
|
||||||
@ -39,33 +42,45 @@ std::set<Line> HL::gen_sol(std::set<Line>& ls){
|
|||||||
rh.insert(*itr);
|
rh.insert(*itr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Recursive call
|
||||||
return merge(inst.gen_sol(lh), inst.gen_sol(rh));
|
return merge(inst.gen_sol(lh), inst.gen_sol(rh));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//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){
|
else if (ls.size() == 2){
|
||||||
auto l1 = *(ls.begin());
|
auto l1 = *(ls.begin());
|
||||||
auto l2 = *(ls.end());
|
auto l2 = *(ls.end());
|
||||||
double isec = (l2.get_ycept() - l1.get_ycept())/(l1.get_slope() - l2.get_slope());
|
double isec = (l2.get_ycept() - l1.get_ycept())/(l1.get_slope() - l2.get_slope());
|
||||||
l1.set_vis_start(-std::numeric_limits<double>::infinity());
|
|
||||||
l1.set_vis_end(isec);
|
//Smaller Slope: vis from isec to inf
|
||||||
l2.set_vis_start(isec);
|
l1.set_vis_start(isec);
|
||||||
l2.set_vis_end(std::numeric_limits<double>::infinity());
|
l1.set_vis_end(std::numeric_limits<double>::infinity());
|
||||||
|
|
||||||
|
|
||||||
|
//Greater Slope: vis from -inf to isec
|
||||||
|
l2.set_vis_start(-std::numeric_limits<double>::infinity());
|
||||||
|
l2.set_vis_end(isec);
|
||||||
|
|
||||||
return ls;
|
return ls;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Set with 1 line: vis at +-inf
|
||||||
else{
|
else{
|
||||||
auto l1 = *(ls.begin());
|
auto l1 = *(ls.begin());
|
||||||
l1.set_vis_start(-std::numeric_limits<double>::infinity());
|
l1.set_vis_start(-std::numeric_limits<double>::infinity());
|
||||||
l1.set_vis_end(std::numeric_limits<double>::infinity());
|
l1.set_vis_end(std::numeric_limits<double>::infinity());
|
||||||
|
|
||||||
|
return ls;
|
||||||
}
|
}
|
||||||
|
|
||||||
return sol;
|
return sol;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::set<Line> HL::construct_HWprob(){
|
std::set<Line> HL::construct_HWprob(){
|
||||||
//Vector to hold all our Lines
|
//Set to hold all our Lines
|
||||||
std::set<Line> lines;
|
std::set<Line> lines;
|
||||||
|
|
||||||
//Hold our doubles from each line
|
//Hold our doubles from each line
|
||||||
std::vector<double> tmp_data;
|
std::vector<double> tmp_data;
|
||||||
|
|
||||||
@ -81,7 +96,7 @@ std::set<Line> HL::construct_HWprob(){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//Close file
|
||||||
data.close();
|
data.close();
|
||||||
|
|
||||||
while(!tmp_data.empty()){
|
while(!tmp_data.empty()){
|
||||||
@ -113,6 +128,15 @@ std::set<Line> merge(std::set<Line> lh, std::set<Line> rh){
|
|||||||
while(litr != lh.end() && ritr != rh.end()){
|
while(litr != lh.end() && ritr != rh.end()){
|
||||||
Line l1 = *litr;
|
Line l1 = *litr;
|
||||||
Line l2 = *ritr;
|
Line l2 = *ritr;
|
||||||
|
|
||||||
|
if(l1.get_slope() < l2.get_slope()){
|
||||||
|
litr++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -16,13 +16,10 @@ class HL
|
|||||||
std::set<Line> get_lines();
|
std::set<Line> get_lines();
|
||||||
std::set<Line> get_sol();
|
std::set<Line> get_sol();
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::set<Line> sol;
|
std::set<Line> sol;
|
||||||
std::set<Line> lines;
|
std::set<Line> lines;
|
||||||
std::set<std::pair<Line, double>> isec;
|
std::set<std::pair<Line, double>> isec;
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
std::set<Line> merge(std::set<Line> lh, std::set<Line> rh);
|
std::set<Line> merge(std::set<Line> lh, std::set<Line> rh);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user