I'm looking for advice on an algorithm I've been struggling with. I have something represented in code (shown later) that looks like the following diagram (initial state on left, desired output on right):
I have this represented in C++ as the following:
#include <vector>
using namespace std;
class Shape {
public:
Shape(vector<vector<float> > c) {
coordinates = c;
}
vector<vector<float> > coordinates; // in format {{x1,y1},{x2,y2}}
};
class Shape_Group {
public:
Shape_Group(vector<Shape> s) {
shapes = s;
}
vector<Shape> shapes;
Shape generate_border();
};
Shape Shape_Group::generate_border() {
/*
define algorithm here
*/
}
int main() {
// define rectangle shapes
Shape s1({{0,0}, {0,1}, {1,1}, {1,0}});
Shape s2({{0,0}, {0,1}, {1,-1}, {-1,0}});
Shape_Group group({s1, s2});
Shape border = group.generate_border(); // should return a shape with the exterior border
// border should be filed with {{1,0}, {1,1}, {1,-1}, {-1,0}};
}
My initial idea for generating this exterior shape was to do something like the following:
loop through each line segment for each shape in the Shape_Group object
loop through every other segment in the Shape_Group object
if both line segments are colinear and overlap, remove them
whatever's left over should be the exterior border
However, with large inputs this is a very slow algorithm. What else can I do?