I have some non-intersecting polygon, and I need to "square" it up so that all angles are 90 degree angles.
Is there a good algorithm to do this?
EDIT
Allowing self-intersections in the resultant polygon, I'm looking for the "best" method of tesselating rectangular shaped tiles in the original polygon. The rectangles can span the original edges of the polygon, and the goal is to fit the most rectangles squarely into the polygon.
You can replace each side of the polygon with a "staircase" which resembles the original side, like so:
the more "steps" you add to each staircase, the closer it resembles the original shape.
It is not guaranteed that you can avoid self-intersections if you allow just one new segment per original segment.
You can use a quadtree to subdivide the line segments until each line segment is alone in its quadtree cell (alternatively you can subdivide further until some approximation ratio is reached). Then replace each line-segment by two line segments of its bounding box.
hth
Related
I want to find & draw contour lines like this.
Data is just List of (x,y,z) and only a few points (about 40~60) in there.
(x and y are position and z is height)
How can i find this contour line and point?
As a first approximation, you can admit that your function is piecewise planar over a triangulation of the data points.
The Delaunay triangulation technique can be used, but in this case, given the regular polar arrangement, I guess that a simple rule based on the polar arguments could do.
Interpolating inside the triangles and obtaining the horizontal sections is a simple matter. Unfortunately, this will produce a gross approximation and you will probably notice artifacts due to the coarseness of the polylines.
A possible cure is to smooth the polylines as a postprocessing step, for instance turning them to polyBeziers.
Another method, which I prefer, is to use a higher order interpolation method. For C1 continuity, you can compute estimates of the gradient at the given points and fit quadratic functions on the triangles. Then subdivide the triangles in sub-triangles, interpolate the function at the sub-vertices, and switch to the planar model in these sub-triangles.
As that looks like an irregular grid, you should first build a mesh around it (for instance, from a Voronoi tesellation).
For every triangle, take the maximum and minimum heights of its vertices and find out the heights of the contour lines in that range (for instance, if you are drawing contour lines every 10 units and the heights of a triangle go from 11.5 to 34.2, the contour lines passing through that triangle are at heights 20 and 30).
Then approximating the height function inside the triangle as a linear function, find out where those contour lines lay and draw them.
The data for the contour plot could be generated with a two-dimensional simplification of the marching cubes algorith, which is described here. In the simplification, squares are used instead of cubes and four sampled values are used for the interpolation instead of the the eight corners of the cubes.
The simplification is also termed marching squares.
The problem is to redraw a self-intersecting 2D polygon, whose border is always a separation line between its interior and its exterior and completely crosses itself in some points (that is, in those points, the polygon interior switches the side of the border, from left to right or vice versa).
What is the simplest algorithm that can do that?
The initial polygon (left) and the redrawn one (right):
.
I added another, a little more complicated example of an initial polygon, that is still very simple (it has only one self-intersection vertex) in this third picture where the polygon interior is filled (points A,B,C,D,E appear in alphabetic order initially when drawing the polygon border).
What you are asking isn't that simple.
You can use a sweepline procedure. The general principle is as follows.
When you draw an horizontal line across the polygon, it meets an even number of edges. Sorting the intersections from left to right, and linking them in pairs, you obtain interior segments.
If you do this for all positions of the horizontal, you will decompose the polygon in a number of monotone chains, i.e. polylines always going down.
When the polygon is simple, the chains appear in pairs at a vertex, disappear in pairs at another, and live their own lifes. But when the polygon is crossed, the chains can cross each other. This can be detected by the fact that from one position of the horizontal to the next, the ordering of intersections change.
Now you have to fix that by "uncrossing" the chains, which is done by splitting the edges at the intersection point.
I can't develop more here, try and lookup "sweepline algorithm".
I have a kind of cutting problem. There is an irregular polygon that doesn't have any holes and a list of standard sized of rectangular tiles and their values.
I want an efficient algorithm to find the single best valued tile that fit in this polygon; or an algorithm that just says if a single tile can fit inside the polygon. And it should run in deterministic time for irregular polygons with less than 100 vertices.
Please consider that you can rotate the polygon and tiles.
Answers/hints for both convex and non-convex polygons are appreciated.
Disclaimer: I've never read any literature on this, so there might be a better way of doing this. This solution is just what I've thought about after having read your question.
A rectangle has two important measurements - it's height and it's width
now if we start with a polygon and a rectangle:
1: go around the perimeter of the polygon and take note of all the places the height of the rectangle will fit in the polygon (you can store this as a polygon*):
2: go around the perimeter of the new polygon you just made and take note of all the places the width of the rectangle will fit in the polygon (again, you can store this as a polygon):
3: the rectangle should fit within this new polygon (just be careful that you position the rectangle inside the polygon correctly, as this is a polygon - not a rectangle. If you align the top left node of the rectangle with the top left node of this new polygon, you should be ok)
4: if no area can be found that the rectangle will fit in, rotate the polygon by a couple of degrees, and try again.
*Note: in some polygons, you will get more than one place a rectangle can be fitted:
After many hopeless searches, I think there isn't any specific algorithm for this problem. Until, I found this old paper about polygon containment problem.That mentioned article, present a really good algorithm to consider if a polygon with n points can fit a polygon with m points or not. The algorithm is of O(n^3 m^3(n+m)log(n+m)) in general for two transportable and rotatable 2D polygon.
I hope it can help you, if you are searching for such an irregular algorithm in computational geometry.
This might help. It comes with the source code written Java
http://cgm.cs.mcgill.ca/~athens/cs507/Projects/2003/DanielSud/
I've got some 2D polygons, each as a list of clockwise coordinates. The polygons are
simple (i.e. they may be concave but they don't intersect themselves) and they don't overlap eachother.
I need to subdivide these polygons into smaller polygons to fit a size constraint. Just like the original polygons, the smaller ones should be simple (non-self-intersecting) and the constraint is they should each fit within one 'unit square' (which, for sake of simplicity, I can assume to be 1x1).
The thing is, I need to do this as efficiently as possible, where 'efficient' means the lowest number of resulting (small) polygons possible. Computation time is not important.
Is there some smart algorithm for this? At first I thought about recursively subdividing each polygon (splitting it in half, either horizontally or vertically whichever direction is larger) which works, but I don't seem to get very optimal results with this. Any ideas?
Draw a circle with a center of one of the initial points of initial polygon and radius of your desired length constraint.
The circle will intersect at least two lines at two points. Now you have your first triangle by the biggest as possible. Then choose those intersections as next target. Do until there is no initial points left outside. You have your triangles as large as possible(so as few as possible)
Do not account the already-created triangle edges as an intersection point.
Resulting polygons are not always triangle, they can be quads too. Maybe larger point-numbers too!
They all just nearly equal to the desired size.
Fine-tuning the interior parts would need some calculation.
I suggest you use the following:
Triangulate the polygon, e.g. using a sweep line algorithm.
Make sure all the triangles do not violate the constraint. If one violates the constraint, first try edge-flips to fix it, otherwise subdivide on the longest edge.
Use dynamic programming to join the triangles, while maintaining the constraint and only joining adjacent polygons.
I have two 2D rectangles, defined as an origin (x,y) a size (height, width) and an angle of rotation (0-360°). I can guarantee that both rectangles are the same size.
I need to calculate the approximate area of intersection of these two rectangles.
The calculation does not need to be exact, although it can be. I will be comparing the result with other areas of intersection to determine the largest area of intersection in a set of rectangles, so it only needs to be accurate relative to other computations of the same algorithm.
I thought about using the area of the bounding box of the intersected region, but I'm having trouble getting the vertices of the intersected region because of all of the different possible cases:
I'm writing this program in Objective-C in the Cocoa framework, for what it's worth, so if anyone knows any shortcuts using NSBezierPath or something you're welcome to suggest that too.
To supplement the other answers, your problem is an instance of line clipping, a topic heavily studied in computer graphics, and for which there are many algorithms available.
If you rotate your coordinate system so that one rectangle has a horizontal edge, then the problem is exactly line clipping from there on.
You could start at the Wikipedia article on the topic, and investigate from there.
A simple algorithm that will give an approximate answer is sampling.
Divide one of your rectangles up into grids of small squares. For each intersection point, check if that point is inside the other rectangle. The number of points that lie inside the other rectangle will be a fairly good approximation to the area of the overlapping region. Increasing the density of points will increase the accuracy of the calculation, at the cost of performance.
In any case, computing the exact intersection polygon of two convex polygons is an easy task, since any convex polygon can be seen as an intersection of half-planes. "Sequential cutting" does the job.
Choose one rectangle (any) as the cutting rectangle. Iterate through the sides of the cutting rectangle, one by one. Cut the second rectangle by the line that contains the current side of the cutting rectangle and discard everything that lies in the "outer" half-plane.
Once you finish iterating through all cutting sides, what remains of the other rectangle is the result.
You can actually compute the exact area.
Make one polygon out of the two rectangles. See this question (especially this answer), or use the gpc library.
Find the area of this polygon. See here.
The shared area is
area of rectangle 1 + area of rectangle 2 - area of aggregated polygon
Take each line segment of each rectangle and see if they intersect. There will be several possibilities:
If none intersect - shared area is zero - unless all points of one are inside the other. In that case the shared area is the area of the smaller one.
a If two consecutive edges of one rectactangle intersect with a single edge of another rectangle, this forms a triangle. Compute its area.
b. If the edges are not consequtive, this forms a quadrilateral. Compute a line from two opposite corners of the quadrilateral, this makes two triangles. Compute the area of each and sum.
If two edges of one intersect with two edges of another, then you will have a quadrilateral. Compute as in 2b.
If each edge of one intersects with each edge of the other, you will have an octagon. Break it up into triangles ( e.g. draw a ray from one vertex to each other vertex to make 4 triangles )
#edit: I have a more general solution.
Check the special case in 1.
Then start with any intersecting vertex, and follow the edges from there to any other intersection point until you are back to the first intersecting vertex. This forms a convex polygon. draw a ray from the first vertex to each opposite vetex ( e.g. skip the vertex to the left and right. ) This will divide it into a bunch of triangles. compute the area for each and sum.
A brute-force-ish way:
take all points from the set of [corners of
rectangles] + [points of intersection of edges]
remove the points that are not inside or on the edge of both rectangles.
Now You have corners of intersection. Note that the intersection is convex.
sort the remaining points by angle between arbitrary point from the set, arbitrary other point, and the given point.
Now You have the points of intersection in order.
calculate area the usual way (by cross product)
.