Reputation: 86600
I have a 2D drawing with many straight lines. All those lines are mathematically known. And they are independent of the others.
You can consider I know start and end point of each line and I can make them intersect to find all intersection points. (In detail, they are in Autocad, but I can only work via code. So, I want an Algorythm more than an Autocad solution, although Autocad solution is welcome as well).
The problem is: given a point (anywhere), I want to find the smaller polygon that contains it. That polygon would be formed by the nearest lines.
Details:
I have no declared polygons. Just lines. Any number of lines, any size, any position. And a given point.
Those lines may form one polygon, many or none. So there's no rule to what the polygons looks like. Any number of sides, no regularity. (The points that form the polygons are found by intersecting the lines. And the lines are finite, if they don't intersect, they don't form a polygon.)
My answer is the smallest polygon possible containing a given point.
Upvotes: 1
Views: 1324
Reputation: 1915
Okay, I've been pondering this for a while, and I have formulated a backtracking algorithm that I believe will work. The idea is that you will attempt to build a polygon in a counter-clockwise fashion. And we will set up the problem such that the first polygon we find will be the smallest. That way, we don't have to find all of them.
Algorithm:
On the newly-found line...
A. Loop over all of the other lines that are not already part of the shape you are building, finding their intersection points with this line
B. Sort the intersection points counter-clockwise with respect to the target point.
Find the next intersection point counter-clockwise on the current line. If this is the first point we're checking on this line, then the "next" point is the one after the intersection point that brought us to this line.
A. If there is no such point (this might mean that we've already checked all of the points on the current line), then the current candidate solution is infeasible...
B. If the line that forms the intersection point does not have the target point on it's left side (counter-clockwise), then the polygon it would form does not enclose the target point. Repeat Step 4 for the current line with its next point.
C. If the line that forms the intersection point is the line you started with, then we have found the solution
D. If none of the above is true, do Step 3 for the line that formed the intersection point.
Optimizations:
Remarks:
Upvotes: 2
Reputation: 1915
I believe the following algorithm will work:
Upvotes: 1