how to fit polygon inside another polygon [closed] - algorithm

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have two polygons as shown in the image below.
The left one is "rough polygon" and the right one is "final polygon"
Now, I'm looking for algorithm to fit "final polygon" inside "rough polygon" with best maximum scale.
you can rotate as well as translate "final polygon" as much as you want.
you can't perform individual x dimension or y dimension scaling.
you can only perform uniform scaling (where value of Sx and Sy are same).

Here is a possible line of attack for an exact solution by exhaustive trials; just ideas.
My guess is that a solution is achieved when there are three contacts. I mean three vertexes of either polygon touching an edge of the other or conversely. (If there are less than three contacts, you can inflate the internal polygon so that it comes into a third contact.)
Given two arbitrary triangles, it shouldn't be so difficult to find all possible three-contact positions.
So the global scheme is to take all triples of vertexes/sides from one polygon, and take all complementary triples of sides/vertexes of the other. For every combination, momentarily consider that you have triangles and find the possible three-contact positions. For for every candidate position check if the inner polygon stays confined in the outer one. In the end, keep the admissible solution with the largest scale factor.
For polygons with N and M sides, there will be O(N³M³) configurations to try, and the containment test can be as costly as O(NM). So this approach is only viable for very small polygons.

Scale the rightside polygon by 0.01. (geometrical)
Start spinnning it so fast that it draws circle. (geometrical)
Start incrementing the scale 0.01 by 0.01. (geometrical)
Stop when it touches the outer polygon. (geometrical)
Then bounce it to opposite direction until it bounces again. (physical)
Again and again.(iterations)
Until it cannot move/bounce again.(stuck optimally) (physical)
Use simulated annealing in case of false local solutions.(you need global solution)

Related

Randomly generating coordinates inside a bounded region [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a list of longitudes and latitudes which forms boundary for a geographical area. I would like to generate some random co-ordinates inside this geographical area . Could you suggest some approaches I can take in any language?
Like any problem, there are many ways to solve it, the first thing came into my mind is
Let's call this "geographic area" a polygon.
Find the bounding box of the polygon (easy, just find maxX maxY minX minY).
Generate random coordinate inside the bounding box x=rand()%(maxX-minX)+minX (and same for Y)
Test that the coordinate is inside the polygon, there are many solutions to this problem and they are implemented in any given language so you don't have to implement it by yourself.
Here is an implementation in C/C++ (it is easy to change it to any other language) : Point in Polygon Algorithm
http://en.wikipedia.org/wiki/Point_in_polygon
Edit :
As Jan Dvorak suggested, it might be problematic to use it technique on huge areas, i believe that if your polygon is close to the equator and his size is less the 100km, it will work just fine.
Also you will run into problems if you are near the 180° line because right next to it is the -180°.
First, We'll model the earth's shape as a sphere. Solving the problem for oblate spheroid is much harder.
Generating a random point on a sphere is relatively easy.
Generating a random point on a spherical triangle is harder, but explained in this linked article.
You'll need to divide your polygon into spherical triangles and weight them according to their area. Then randomly select a spherical triangle based on the weights.
For the general case, triangulating a spherical polygon is not possible, however, for most practical cases triangulation is a simple task. One such algorithm is described here (algorithm 1, page 901) with C++ source code available here (search for "Computational methods for calculating geometric parameters of tectonic plates").
You can try this:
Compute all co-ordinates within this geographical area, see save in vector<Point> points.
Generate a random int number within [0, points.size()), see k.
points[k] is what you want.

How can I divide a rectangular area into smaller rectangles that represent a percentage of the total area? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Basically I'm trying to get a "nice" image where all the small rectangles add up to the big rectangle, kind like:
*Later Edit to clarify some things:
I want to be able to draw something like this in a piece of software. So, what I need is closer to an algorithm.
All I need are some rectangles. I don't need them to have some predefined proportions just that they look like a rectangle. Anything between a square and a 3:1 width/height (or height/width) is fine. The extremely naive approach would be to just divide the width of the enclosing rectangle into the percentage that enclosed rectangles have but this will create thin slices and some of the smaller percentage rectangles will drop bellow 1px.
I need to find a way to split the rectangles on multiple rows.
*Second Edit: Problem SOLVED. I was looking for a TreeMap algorithm (as pointed out by Phpdna). Once I had the keyword I was able to quickly find a couple of python implementations that satisfied my requirements.
Treemap is an algorithm that can pack smaller rectangles into a map. You can recursively subdivide a plane into smaller tiles for example by splitting the plane along the 2 axis and save the result to a tree.
This approach guarantees that the small rectangles always cover the initial rectangle. so does any other approach which starts by constructing rectangles within an existing set of rectangles.
Draw a straight line from one edge of the plane to the opposite edge and parallel to the other two edges. Draw it in a location which produces 2 rectangles whose proportions are pleasing to your eye.
If you want another rectangle, draw a line from the first line, perpendicular to it, and extend that line to the edge of the plane. Again, choose its position so that the line creates rectangles of pleasing proportions. Now you have 3 rectangles.
Now, to get the 4th rectangle, choose one of the existing lines to start from and draw a line perpendicular to it until it reaches either the edge of the plane or an existing line. Again, take care to ensure that the proportions of the rectangles created are pleasing to your eye.
Continue until you have all the rectangles you want.
Personally I think that the above is the easy part, the difficult part is determining algorithmically where to draw the lines. I suggest you think of the Golden Ratio, it cousins the Fibonnaci numbers and other ratios such as the basis of the A series of paper sizes, ie 1:sort(2).

Checking convexity from outside [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Is there any method or algorithm to determine convex (or non-convexity) property of a region from outside (perimeter) ?
One way is plotting tangent line in each point of perimeter and discuss how many times this line intersect the perimeter points. If no intersect shown (for all points of perimeter ) we can conclude region is convex. In otherwise region is non-convex.
Second way is determine interior angel of each point of perimeter and discuss if it's bigger than 180 or not. The region is non-convex if at least one point in perimeter exist it's interior angel bigger than 180.
Are there another simpler ways?
Any ideas or solution would be appreciated, thanks.
One thing to observe when doing this is that as you traverse the sides of a convex polygon, all the turns will be to the same side. That is, if you are traversing around the vertices in a counter-clockwise direction, all of the turns will be to the left; if you are traversing around the vertices in a clockwise direction, all of the turns will be to the right. If you ever observe a turn to the opposite side of any others observed, then you know you're dealing with a non-convex polygon. If all of the turns are to one side, then it is a convex polygon.
So, all you need to do is take look three vertices at a time, call them vn, vn+1 and vn+2. You can then determine which side of the line segment connecting vn and vn+2 the vertex vn+1 sits on. For CCW, vn+1 should be on the right of the line segment, and for CW it should be on the left. There is an answer to another question which provides a method for determining this.
There are additional implementation details you should work out (like how to deal with n=N, the number of points in your polygon, but this should provide you with a place to start.
An implementation based on this approach will run in O(N) time and space.
UPDATE: In response to the question below, "how about non-polygonal regions"? In general this is much harder. Mathematically, a region can be shown to be non-convex by finding a line segment with endpoints in the interior of the region but which has some portion of the line segment exterior to the region. I suspect you're looking for a way of implementing this using a digital computer, and so the pure mathematical approach is not practical.
So, you're going to have to offer some sort of constraints as to the types regions before the problem becomes intractable. That is, you have to constrain your problem space so that things like Nyquist sampling of the perimeter of the boundary do not incorrectly identify a non-convex region as being convex.
Assuming you can properly constrain the problem, any solution you can come up with, which can be implemented on a digital computer will have to approximate the region. You can either generate a piece-wise linear approximation of the region in question and run the algorithm above, or pick the proper set of points along the boundary of the region and calculate their derivative. Each successive sample should rotate the angle of the tangent line by some increment in the same direction. But again, it gets downs to sampling.
If you have other information about the nature of any nonlinearities which comprise the boundary of your region, you may be able to symbolically demonstrate whether a segment of the boundary is convex. The problem then reduces to showing that it remains convex when joined to the adjacent sections, which again is going to be problem specific.
So, my suggestion is, for digital computer implementation, approximate as needed the boundary of the region by a polygon and run the method defined above on that approximation.
An algorithm I've used (in pseudo code):
function isConvex(vertices[Count] V):
convex = true
if Count <= 3 return convex
for N = 0 to Count while convex:
// line segment between previous and subsequent vertices
LineSegment segment1 = new LineSegment(
V[(N + Count - 1) % Count], V[(N + 1) % Count]);
// line segment between the point and any other point
LineSegment segment2 = new LineSegment((V[N], V[N+2 % Count]);
if not segment1.intersects(segment2) then convex = false;
return convex
I don't know if this is optimal or simpler than the algorithms you've already tried.
The LineSegment.intersects() method already existed making this really easy to write.
The actual code used segment2 from the previous iteration as segment 1 of the current iteration making it faster but more complex to write even in pseudo code.
And also, for what it's worth, the original of this algorithm was written in assembly language on a processor that no longer exists, so I won't be providing actual code ;-).

the centroid of the intersection of n disks/circles [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Given that n disks/circles share a common area, meaning that every two of them intersect one another, and we know their coordinates (x1,y1,r1), (x2,y2,r2), ..., (xn,yn,rn), where xi,yi,rn represent the x axis coordinate, the y axis coordinate, and the radius of the ith disks/circle, respectively, can you provide a method to calculate the coordinate of the centroid of the intersection of these disks/circles?!
Let's assume that all the circles overlap such that one can trace a path from any point in one of the circles to an arbitrary point in any other circle while traversing only points contained by circles. And, for generality, that the circles may be of different radii.
Per the wiki page you can decompose this shape into separate geometric regions. That is, you can find an intermediate value for the centroid by considering each circle separately (i.e. pretending they do not overlap).
Unfortunately some of the circles overlap, so you will be counting regions of the figure twice. The figure below, taken from this page, shows these regions of overlap. You therefore must find the centroid of the circle-circle intersection and subtract this from your intermediate centroid (see the wiki page's description of geometric decomposition for further details).
Since you can determine which circles overlap just do these for each overlapping pair and then each region of space will be counted only once. Your problem then reduces to finding the centroid of a circle-circle intersection.
You can find this by using geometric decomposition to break each lens of intersection into circular segments with the height of the segment given via a method here and coupling the result with appropriate coordinate transformations to rotate and translate the centroid to a location relative the center of one of the circles.

Finding centre of rotation for a set of points [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
If I have an arbitrary set of points, and then the same set of points rotated by some degree, does anyone know of any algorithms to calculate/estimate where the centre of the rotation is? Or an area of study where these kinds of algorithms are needed?
I am having trouble finding any relevant information.
Thanks
Lets say you have one point (x, y), that moved to (x', y').
Then the center of rotation must lie on the line that is perpendicular to (x,y)-(x',y'), and that intersects the center (x,y)-(x',y').
Now take another point, (x2, y2), that moved to (x'2, y'2). This also gives rise to a line on which the center of rotation must be located on.
Now take these two lines and compute the intersection. There you have the center of rotation.
Update: If you don't have the correspondence of which point went where, it shouldn't be too hard to figure out. Here is a suggestion from top of my head: Find the center of mass of the "before"-points. Order the points according to their distance from this point. Now do the same with the "after"-points. The order of the two sets should now match. (The point closest to the center of mass before rotation, should be the point closest to the center of mass after rotation.)
It would be crazy overkill for this type of problem, but I think the functionality of the generalized Hough transform for object detection at least encompasses what you want, even though it's not quite meant for this purpose.
Given an arbitrary shape created from a set of points, and another arbitrary set of points, it tries to find the shape in the set of the points even though it's been rotated, scaled, and translated. You might be able to take out the scaling and translation and get what you want.
Basically what it would come down to is brute forcing possible rotation points to see which one fit the second set of points best.
Very interesting problem. My knowledge on this is a bit out of date, but as I recall, there's some research in the use of subgraph analysis on this; that is, characterizing subsections of the set of points by the distances between the points and the variances therein, and then correlating those subgraph analyses between the before and after rotations.
This is, of course, assuming a very complex set of points with a nonuniform distribution.
You need to find some signature on your data set that allows to identify the points from the first set (A) with those on the second set (B).
An easy way is as follows:
For every element E in A, find the two nearest points (N1, N2) and calculate the angle between N1,E,N2 resulting in three values: the angle and the distances from E to N1 and N2 (ang, d1, d2).
Find 3 points in A with unique tuples (ang, d1, d2).
For every element in B calculate also the distance to its two nearest neighbors and the angle. Find the 3 points matching those selected from A.
Calculating the rotation is just a matter of geometric analysis.
update: you need 3 points to determine the rotation in 3D space. In 2D, two will do.
update 2: as others have commented on other posts, there may be symmetries in A that would stop you for finding the 3 unique triplets for (ang, d1, d2). In that case, for every one of the selected three points in A, you will have to perform a search over all the elements in B matching their triplets until some combination results in a rotation that works for all the elements in A.

Resources