Fill the area with uniform spacing - algorithm

User Requiremnts
Inputs
User will give boundary points of the closed polygon P in which he wants to fill the area formed by polygon. Red Points P in the
Examples.
Space S in between the lines. Spacing S between the lines of Green points in Examples.
Output
The output will be a closed polygon points. Green G points in the Examples.
Can someone help me, i don't know which algorithm to use.

The example looks like it was created like this:
Create a field of hexagonally packed spots with the desired spacing;
Select the spots that are inside the red shape;
Imagine an edge with a random weight between each pair of adjacent spots (each spot will have up to 6 neighbours), and connect them into a minimum weight spanning tree using Kruskal's or Prim's algorithm (all that is googlable);
trace around the resulting tree

Related

Counting number of ways to make a 3 Partition of n dots in a triangle such that each partition fit in a "Y" shape. (More description below)

I want to find an algorithm to tackle this problem but I have no idea what this problem is even called so I can't google it.
Problem:
There is a triangle, and inside the triangle there are n dots. For example, as below.
Now assume 3 sides of triangle are rubber bands and dots are nails on the wall. We tuck each rubber bands into some dot(s). But each rubber bands must be convex towards the center of triangle.
Also, there should be no intersection of any 2 rubber bands apart from the 3 vertices of original triangle.
Also, there should not be any remaining dot inside the polygon formed by 3 rubber bands.
How many valid arrangement of rubberbands, such that all given conditions are satisfied, are there?
Here, in this picture, the leftmost picture is the original state, 2nd picture is a valid composition, 3rd is invalid since blue and red rubber bands intersect, 4th is invalid since there is a nail remaining inside red-blue-green polygon
My approach: After some thoughts: I guessed that if we pick an arbitrary location inside the triangle which is not one of the given dots(nails), and then we make a Y shape starting from that location towards 3 vertices of outer triangle, that is guaranteed to make a unique valid arrangement of rubber bands, if we imagine that each pair of 2 neighboring edges of Y shape is a rubberband and then we release those bands. They will sort themselves out to form a valid arrangement with elasticity.
So from each 3 vertices of triangle, I draw a line towards all n dot(nails). And then for each polygon partitions that are created by those lines and/or 3 sides of triangle(approx n^3 polygon partitions in total), I can make exactly 1 valid composition by picking any point in the polygon and making Y shape.
For example, if I pull rubber bands to form a y-shape as in the left picture, I can then make a valid composition by releasing those rubber bands, as in the right picture.
However, the problem is that 2 or more polygons may yield same valid composition, so we will count more than the correct answer.
For example, if there are only 2 dots(nails), we could make a bunch of polygon partitions by connecting lines(colored brown) starting from each 3 vertices of triangle to 2 dots(nails). so there are 17 partitions. each 17 partitions would yield a valid composition but the answer would be 6. (3 cases where 2 dots go under same rubber band, 3 cases where 1 dot goes under a band and the other dot goes under the other band.)
So I drew several small examples and tried to figure out a pattern to make an algorithm to de-duplicate it and I kind of see a pattern but can't really formulate it into a pseudo code. Any help is appreciated.

Cut the Cake, or polygon decomposition

I'm facing the following problem: I'm given a set of coordinates on an integer grid that define the vertices of a polygon. The polygon is guaranteed to be convex. It's proven that such a polygon can always be cut into 4 equal area parts by 2 orthogonal lines. Let's call the point of these lines' intersection P.
Given that set, I should calculate the coordinates of P within the polygon and the angle the lines need to be turned on so that the lines cut the polygon into 4 equal parts.
I realise that, put generally, the cake cutting problem has no "good" solution. But this particular case of it should.
I've searched for an algorithm to solve that problem, but found nothing useful.
Where should I look?
My approach would be to calculate the coordinates of the centre of the polygon (that can be done more or less easily), place Pthere and then "wiggle" the lines until the areas of the parts match. But that sounds too inelegant.
UPD: that's the problem I'm dealing with. Perhaps this question should be suspended until I come up with actual code questions.
Here is a partial sketch of the solution:
Choose an arbitrary direction and find the line parallel to that direction that splits the polygon in two. To achieve this, draw a line by every vertex to decompose the polygon in slabs. The respective areas of the slabs will tell you what slab the desired line intersects. Simple linear interpolation will give the exact location of the line.
Now your polygon is split in two convex polygons. For each halve, repeat the above procedure using the perpendicular direction. In general, you will get two distinct splitters, and what remains to be done is to find the direction such that they do coincide.
In the given direction, the splitters intersect four specific edges of the polygon. If you slightly rotate, they still intersect the same four edges. You can decompose a full turn in angular ranges such that the four intersected edges remain the same.
Knowing the four intersected edges, you can establish the relation that tells you the distance between the two perpendicular splitters as a function of the angle. Then you can compute the angle at which the two splitters coincide, and check if this angle belongs to the range defined for these edges.
By trying all ranges in turn, you will find the solution.
Note: the limits of the angular ranges correspond to directions parallel or perpendicular to the lines joining two vertexes.

Algorithm for the decomposition of polygons

Does anyone know a relatively fast algorithm for decomposing a set of polygons into their distinct overlapping and non-overlapping regions, i.e. Given a set of n polygons, find all the distinct regions among them?
For instance, the input would be 4 polygons representing circles as shown below
and the output will be all polygons representing the distinct regions shown in the different colours.
I can write my own implementation using polygon operations but the algorithm will probably be slow and time consuming. I'm wondering if there's any optimised algorithm out there for this sort of problem.
Your problem in called the map overlay problem. It can be solved in O(n*log(n)+k*log(k)) time, where n is the number of segments and k is the number of segment intersections.
First you need to represent your polygons as a doubly connected edge list, different faces corresponding to the interiors of different polygons.
Then use the Bentley–Ottmann algorithm to find all segment intersections and rebuild the edge list. See: Computing the Overlay of Two Subdivisions or Subdivision representation and map overlay.
Finally, walk around each cycle in the edge list and collect faces of that cycle's half-edges. Every set of the faces will represent a distinct overlapping region.
See also: Shapefile Overlay Using a Doubly-Connected Edge List.
I don't think it is SO difficult.
I have answered the similar question on the friendly site and it was checked by a smaller community:
https://cs.stackexchange.com/questions/20039/detect-closed-shapes-formed-by-points/20247#20247
Let's look for a more common question - let's take curves instead of polygons. And let's allow them to go out of the picture border, but we'll count only for simple polygons that wholly belong to the picture.
find all intersections by checking all pairs of segments, belonging to different curves. Of course, filter them before real check for intersection.
Number all curves 1..n. Set some order of segments in them.
For every point create a sequence of intersections SOI, so: if it starts from the border end, SOI[1] is null. If not, SOI[1]= (number of the first curve it is intersecting with, the sign of the left movement on the intersecting curve). Go on, writing down into SOI every intersection - number of curve if there is some, or 0 if it is the intersection with the border.
Obviously, you are looking only for simple bordered areas, that have no curves inside.
Pieces of curves between two adjacent non-null intersection points we'll call segments.
Having SOI for each curve:
for segment of the curve 1, starting from the first point of the segment, make 2 attempts to draw a polygon of segments. It is 2 because you can go to 2 sides along the first intersecting curve.
For the right attempt, make only left turns, for the left attempt, make only the right turns.
If you arrive at point with no segment in the correct direction, the attempt fails. If you return to the curve 1, it success. You have a closed area.
Remember all successful attempts
Repeat this for all segments of curve 1
Repeat this for all other curves, checking all found areas against the already found ones. Two same adjacent segments is enough to consider areas equal.
How to find the orientation of the intersection.
When segment p(p1,p2) crosses segment q(q1,q2), we can count the vector multiplication of vectors pXq. We are interested in only sign of its Z coordinate - that is out of our plane. If it is +, q crosses p from left to right. If it is -, the q crosses p from right to left.
The Z coordinate of the vector multiplication is counted here as a determinant of matrix:
0 0 1
p2x-p1x p2y-p1y 0
q2x-q1x q2y-q1y 0
(of course, it could be written more simply, but it is a good memorization trick)
Of course, if you'll change all rights for lefts, nothing really changes in the algorithm as a whole.

How to approximate a polygon with n rectangles?

Is there any algorithm that can approximate the given polygon with n non overlapping rectangles that gives the maximum coverage? By maximum coverage I mean, the sum of the rectangle areas are maximized. Rectangles are not necessarily equal sized.
The polygons I am dealing are convex. If exact solution is hard/expensive to find (which I am expecting it to be), simple good heuristics are welcome too.
Edit I always thought of approximating the polygon with rectangles inside polygon, but solutions with rectangles not totally inside polygons are also fine. If that is the case, maximization of the area becomes minimization of the area.
Edit 2 I forgot to mention that these rectangles are orthogonal rectangles, i.e. aligned with axises.
One approach would be to create a (in the general case rectangular) bounding box for your polygon. Calculate the difference between the area of the bounding box and the area of the polygon. If the difference is small enough, you're done, if not, continue ...
Divide the box into 4 equal-sized rectangles, 2x2. Figure out which of these rectangles is entirely inside the polygon. Calculate the difference between the total area of the rectangles inside the polygon and of the polygon. If the difference is small enough you're done, if not continue ...
Divide each of the 4 rectangles into 4 sub-rectangles ... if at any stage you find that a rectangle is either fully inside or fully outside your polygon you can remove it from the list of rectangles to subdivide at the next iteration.
In other words, use a quadtree to divide the space containing your polygon and develop it to the extent necessary to meet your accuracy criteria.
Create a queue of polygons, Q, to be processed
Add the initial polygon to Q
Remove a polygon P from Q
Find the longest side A of P
Rotate P so that A is on the X-axis
If P is a triangle, split it with a perpendicular line in the centre of A:
Add the two halves G and H to Q and goto 3
(Now, P has 4 or more sides)
If X and/or Y are acute:
10 . Take the next longest side of P, A, and goto 5
11 . Project a red rectangle up from A. Find the 2 points where it intersects P, B and C:
12 . Choose the longer(B) and finalise the green rectangle
13 . Add the remaining figures (D, E and F) to Q
14 . Goto 3
I realize this is a really old question but I recently stumbled across a similar problem where I had to try to approximate a polygon with rectangles. Using some of the ideas presented here and elsewhere, I started with an inscribed rectangle and generated rectangles around the inscribed rectangle to provide the general shape of the polygon.
This approach works well for convex polygons and reasonable well for some concave polygons - especially if you take an iterative approach (e.g. feed output rectangles as inputs to another iteration).
For extremely concave shapes, you might consider breaking up the polygon into convex hulls and then applying the technique I described above. The Bayazit implementation looks very promising.
In case anyone is interested, I have posted my implementation using inscribed rectangles here:
https://github.com/pborissow/Poly2Rect
A first idea, maybe others can improve on it.
place a squaresomewhere inside the polygon, as far as possible away from any edges.
iteratively
1.) grow it,
2.) move it and turn it to maximize its distance from edges. until you can't grow it any more
start from the beginning, while considering edges of placed rectangles as edges of the polygon.

finding the outside of a geometric graph

I have a map of polygons, for example:
alt text http://img13.imageshack.us/img13/2808/output.png
(sorry the image isn't so great, I'll try and get better one later)
The green lines are the shortest path from between the connected shapes. What I need to do is find the green lines that form the outer edge and which direction I need to traverse them in to circle CCW.
The polygons will always be convex and the lines will never cross. The lines are defined in terms of there X,Y endpoints. I have an index from polygons to related lines and it is tagged with which end is attached.
The reason I need this is because I need to find the edges forming each of the interior section and my wall following solution fails/crashes for the exterior section.
0. Clarification: I am not sure whether the green connections between the convex polygons are provided as part of the input or whether the program has to determine the appropriate green connections as shortest paths between polygons itself. I just noticed that your image is missing a green edge at the top right corner (that would be part of outer border if allowed).
1. Solution: Always choose the next edge. If your input specifies which green lines are allowed and which not, then you simply traverse the boundary of the outside component by finding one start point (e.g. by taking the polygon corner with lowest x-coordinate), ordering the green edges that depart from the polygon of this current point in counter-clock-wise order and choosing the edge immediately following the current point in CCW order. Now, take the other end of that edge as "current point" and repeat the same method to find the next edge... until you return to the first polygon.
2. Solution: Start with the convex hull. If you allow all possible green lines, you don't need them as input. The convex hull of all polygon edges is a first approximation to your solution (details of finding --- see further below): The convex hull contains real polygon edges (let's think of them as "black": They are part of your final solution, already in correct order) and edges that connect polygons (Let's think of them as red: They need to be replaced by green edges and possibly parts of other polygons).
Completing the 2nd solution: divide and conquer: Now, we need to replace each red edge by a combination of green and black edges. We just focus on one red line at a time (and apply the same method for each red line that we may have).
So we have one red line that contains two polygons that have one green line (the shortest connection between them) --- the four edges of these two lines define a quadrangle. If none of the other polygon corners are in this quadrangle, you are done: replace the red edge by the green edge and any black edges on the polygons to get to the connecting points.
But if you find polygon in the quadrangle, select from them the closest to the red edge. Move the red edge towards that point --- so that the new point cuts the red edge into two red edges. These two new red edges replace the one old red edge: apply this method recursively to both of them. Their corresponding quadrangles are much smaller and contain less polygon edges.
As you keep applying this divide and conquer method you will eventually end up with no remaining red edges (because you loose one red edge each time you find an empty quadrangle).
Convex hull: This is a difficult problem in n dimensions but easy in two dimensions: If you search the net or browse SO, you surely find a better solution than I can think of right now, but here is one that comes to mind (again: divide and conquer): Find one point A with maximal x-coordinate and one point B with minimal x-coordinate, connect them by two directed "blue" edges: A->B and B->A. Divide your points into two sets: those at the right hand side of the edge A->B and those on the left hand side (which really is the right hand side of B->A). We repeatedly replace each blue edge until we find the convex hull:
Take one blue edge A->B and look at the points its right hand side. If there are none, then this blue edge is really black (part of your solution). Otherwise, take the point C furthest to the right of the blue edge and replace the edge A->B by two blue edges A->C and C->B. Divide the points that were at the right hand side of A->B into those that are at the right hand side of A->C, those that are at the right hand side of C->B and those that are neither (they are ignored). Repeat until all blue edges are replaced.

Resources