Randomly generating coordinates inside a bounded region [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 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.

Related

Is there a generalization of Voronoi Diagrams to curves in the plane? [closed]

Closed. This question is not about programming or software development. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 3 months ago.
Improve this question
I've studied Voronoi Diagrams and Fortune's Algorithm before. What I'm curious about is if there's a generalization of Voronoi diagrams where instead of the input being a set of points, it is instead a set of non-intersecting curves in the plane, where we want to partition the plane into regions based off the Euclidean distance to the nearest curve.
Is this problem well defined and is there any known (hopefully efficient) algorithm to compute this generalization?
I've tried searching for an answer to this, but most resources seem to focus on curved metric spaces or curved regions rather than the input set itself being composed of non-points.
Edit:
If this isn't well defined for non-intersecting curves, will it work for line segments?
Yes, the Voronoi diagram is defined for arbitrary point sets and other distances than Euclidean. A quick web search gives you as many examples as you want. Intersecting curves are also possible.
The construction of the diagram for a set of line segments is well documented. The cells are bounded by line segments and parabolic arcs. If I am right, Fortune's algorithm generalizes to this case.
For general curves, the problem gets harder. In all cases, you need to derive the equation of bisector lines, and intersect them correctly to delimit the proper arcs at triple points.
A digitized version (on a raster grid) is easier and will work with any kind of shape. It is similar to the computation of a distance map, and can be performed in time linear in the number of pixels.

how to fit polygon inside another polygon [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 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)

Computing location based on a list of approximate distances [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have N points in at 3D space (I think I can grasp myself general N-dimensional case) and approximate distances to these points, how can I compute my position relative to these N points?
EDIT
Please note that the distances are approximate, so the more approximate distances I have the more convenient result I should get
Thank you!
I would write down an equation that gives you some measure of the errors associated with a possible location, and then find the location that minimizes this measure. My first attempt would be to minimize the sum of the squares of the difference between the distance measured and the distance worked out from the possible location, for each of your approximate distance, so you are minimizing something like SUM_i((sqrt((X-Ai)^2 + (Y-Bi)^2 + (Z-Ci)^2) - Di)^2) where X,Y,Z is the location co-ordinates you are trying to find, (Ai,Bi,Ci) is the co-ordinates of one of the objects from which you are measuring distances, and Di is the distance measured. It doesn't look very pretty, but you should at least be able to compute derivatives and then find some sort of minimization routine in a math library.
You have distances from given N points in a 3D space and their approximate error values. So, you have a thick sphere for each of the points that you are in. You get all of them, calculate their intersection area, and take that area's center point as your approximate location.

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).

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