I need it to stress test some location based web service. The input is 4 pairs of lat/lon defining a bounding rectangle or a set of points defining a polygon.
Are there any libraries/algorithms for generating random point on a map? (Python/java)
In java you can use Math.random()
For example, if you want to generate a random number between 1 and 10:
int randomNumGenerated = (int)(Math.Random()*10) + 1;
You can apply this to the issue you are trying to solve easily.
Take a look at this question, which deals with generating points inside an arbitrary 4-point convex polygon.
Random points inside a 4-sided Polygon
This article, on sphere point picking explains far better than I could why the naive approach of generating 2 random numbers on the interval [0,1) will lead to a poor distribution of points across the surface of the sphere. That may or may not be a concern of OP.
However, it ought to be of concern to OP that randomly generating a set of 4 points on the surface of the Earth might necessitate some tricky programming. Consider the case of the 'polygon' defined by the points (lat/long, all in degrees) (+5,90),(+5,-90),(-5,-90),(-5,90). Does the point (0,0) lie inside this polygon or outside it ? What about the point (0,180) ? It's very easy to generate such ambiguous polygons -- the surface of a sphere is not well modelled by the Euclidean plane.
I'd take a completely different approach -- generate 1 point at random, then generate lat and long offsets. This will give you a quasi-rectangular patch on the surface, and you can tune the generation of the offsets to avoid ambiguous polygons. If you want to generate polygons which are not quasi-rectangular, generate a series of points and angles which, when combined, define a polygon which suits your needs.
Simple: Generate two random numbers, one for latitude and one for longitude, inside the bounding rectangle of the map, for each point.
double longitude = Math.random() * Math.PI * 2;
or use
public static LatLng random(Random r) {
return new LatLng((r.nextDouble() * -180.0) + 90.0,
(r.nextDouble() * -360.0) + 180.0);
}
Why wouldn't you just generate the latitude as a random number between -90 and 90, and the longitude as another random number between -180 and 180?
Then you have a point. Yo can then generate as many points as you need to make a polygon.
You can generate a random number between a and b with something like:
rnum = a + rnd() * (b-a); // where rnd() gives a number from 0 to 1
Related
This question already has answers here:
Generate a random point within a circle (uniformly)
(22 answers)
Closed 5 years ago.
I have the following method in Java:
public static Vector2d random(Circle circle) {
// this returns a random number between 0 and Math.PI * 2
double angle = MathUtils.random(0, Math.PI * 2);
// give the point inside the unit circle
// this returns a normalized vector from a given angle
Vector2d point = new Vector2d(angle);
// however, this is only along the edge
// now add a random magnitude (because this is a normalized vector, we can just multiply it by the desired magnitude)
double magnitude = Math.random();
point = point.multiply(magnitude);
// now expand this to fit the radius
point = point.multiply(circle.getRadius());
// now translate by circleCenter
return point.add(circle.getCenter());
}
This does return a point in the defined circle, however, when you do this many times and plot the points, you can clearly see most points will be toward the center.
Why is this? I don't see how my math can do this.
Comment if you want me to add an image of the points on the plot, if you think that could be helpful.
Of course, when r is small, the generated points are closer to each other.
As said by #DBrowne, you can adjust the density by the inverse CDF trick.
Alternatively, you can spare function evaluations by drawing uniform points in [-R,R]x[-R,R] and rejecting the ones such that X²+Y²>R² (about 21% of them). The method generalizes to any shape known by its implicit equation.
Your math is flawed. Here's an explanation of why and the correct solution:
The task is to generate uniformly distributed numbers within a circle
of radius R in the (x,y) plane. At first polar coordinates seems like
a great idea, and the naive solution is to pick a radius r uniformly
distributed in [0, R], and then an angle theta uniformly distributed
in [0, 2pi]. BUT, you end up with an exess of points near the origin
(0, 0)! This is wrong because if we look at a certain angle interval,
say [theta, theta+dtheta], there needs to be more points generated
further out (at large r), than close to zero. The radius must not be
picked from a uniform distribution, but one that goes as
pdf_r = (2/R^2)*r
That's easy enough to do by calculating the inverse of the cumulative
distribution, and we get for r:
r = R*sqrt( rand() )
where rand() is a uniform random number in [0, 1]
http://www.anderswallin.net/2009/05/uniform-random-points-in-a-circle-using-polar-coordinates/
I want to use the equation of "straight line" with given 2 Latitude+Longitude points on a map.
can i use the raw material of Latitude and Longitude to do the calculation like this-
poly1_lngY= list.get(i).getLongitude(); // Y1 point on a line
poly2_lngY= list.get(j).getLongitude(); // Y2 point on a line
poly1_latX=list.get(i).getLatitude(); // X1 point on a line
poly2_latX=list.get(j).getLatitude(); // X2 point on a line
incline_poly= (poly2_lngY-poly1_lngY)/(poly2_latX-poly1_latX);
or should i use Math class?
if i need to use the Math class, I would appreciate an answer to how to do that
No, what you're suggesting is not correct at all.
Lat and lon are angles in spherical coordinate systems. You should treat them as such.
The earth's radius is large enough and your distances small enough so that treating the earth as a plane is probably okay. But you need to convert those points to rectangular coordinates before calculating distance.
A better solution would be to know how to calculate distance in the correct coordinate system so it's always correct. Go look up how to do it for spherical coordinates or learn something about tensor calculus and metric tensors.
For example, for the dimension d=2, it means that we could generate a random angle 0<=a<2*pi, and then we could just use
(x_1,x_2)=(r*cos(a),r*sin(a)) as the random vector.
However, for the dimension d>=3, we could not just generate an angle and use it to represent the vector. Then how could we generate such a vector (x_1,...,x_d), which is identically distributed on x_1^2+x_2^2+...+x_d^2=r^2?
I have just come up with a new idea, that we could generate a vector (x_1,...,x_d) such that -r<=x_i<r for all i, normalize it if x_1^2+x_2^2+...+x_d^2<=r^2 and abondon it if x_1^2+x_2^2+...+x_d^2>r^2.
However, there is a drawback that the probability that x_1^2+x_2^2+...+x_d^2<=r^2 would become very small if d is very large. Does there exist some better solutions?
Generate random variables (X_1, X_2, ... X_d) that are independent and have standard normal distributions, and then normalize by dividing by sqrt(X_1^2+...+X_d^2)/r.
That the joint distribution of independent normal distributions is rotationally symmetric is not just true, it characterizes normal distributions.
You can generate pairs of independent variables with a standard normal distribution efficiently from uniform random variables using the Box-Muller transform.
I see two ways around it.
The straightforward way is, in pseudo-code:
1. build n-dimensional vector x[0] through x[n-1] with random components
2. find radius
3. go to step 1 if radius > r; otherwise, normalize to radius r
This is non-deterministic, because there is no way to know how many times you will need to cycle before you find an acceptable sphere. Additionally, the probability of finding a bad point goes up with the number of dimensions.
To understand why (thanks commenters!), imagine a 1x1 square. Add a r=1 circle inside. Fill the square with random points. All the points between the center and the circle are evenly distributed when projected on the circle. All the points between the circle and the square's border are not - because there's too many at, say, 45º; and none at, say, 90º.
The non-straightforward version is a generalization of your 2-dimensional approach:
1. assume that we are on an n-sphere; generate angles phi[0], ...phi[n-2]
for a polar-coordinates point
2. convert to cartesian coordinates x[0] through x[n-1]
According to the n-sphere page in wikipedia, the formula is
x[0] = r*cos(phi[0]);
x[1] = r*sin(phi[0])*cos(phi[1]);
x[2] = r*sin(phi[0])*sin(phi[1]);
...
x[n-2] = r*sin(phi[0])*sin(phi[1])* /*...*/ sin(phi[n-3])*sin(phi[n-2])
x[n-1] = r*sin(phi[0])*cos(phi[1])* /*...*/ sin(phi[n-3])*cos(phi[n-2])
The actual algorithm can be implemented a lot more efficiently (sin(phi[0]) is getting calculated a lot, for example)
To avoid non-determinism, I recommend the second approach.
Edit
The recommended approach, not listed above, is in Douglas's answer and many reference sites:
https://mathoverflow.net/questions/136314/what-is-a-good-method-to-find-random-points-on-the-n-sphere-when-n-is-large
http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
http://mathworld.wolfram.com/HyperspherePointPicking.html
Does anybody know how to find out if a set of coordinates are within a triangle for which you have the coordinates for. i know how to work out length of sides, area and perimeter, but i have no idea where to begin working out the whereabouts within the triangle of other points.
Any advice would be appreciated
You can create a Polygon object.
Polygon triangle = new Polygon();
Add the vertexes of your triangle with the addPoint(int x, int y) method.
And then, you just need to check if the set of coordinates is inside your triangle using contains(double x, double y) method.
Use the contains method of the Polygon class as documented here.
For a solution without using the Polygon-class:
Assume that you have giving three points A,B,C the vertices of your polygon. Let P be the point you want to check. First calculate the vectors representing the edges of your triangle. Let us call them AB, BC, CA. Also calculate the three vectors PA, PB, PC.
Now calculate the cross product between the first two of the vectors from above.
The cross product of the first pair gives you the sin(alpha), where alpha is the angle between AB and PA, multiplied with a vector pendenpicular to AB and PA. Ignore this vector because we are interested in the angle and take a look at the sine (in the case of 2D vectors you can imagine it as the vector standing perpendicular to your screen).
The sine can take values between (let's say for the ease) betwenn 0 and 2*Pi. It's 0 exactly at 0 and Pi. For every value in between the sine is positive and for every value between Pi and 2*Pi it's negative.
So let's say your Point p is on the left hand side of AB, so the sine would be positive.
By taking the cross product of each pair from above, you could easily guess that the point P is on the left hand side of each edge from the triangle. This just means that it has to be inside the triangle.
Of course this method can even be used from calculating whether a point P is in a polygon. Be aware of the fact, that this method only works if the sides of the polygon are directed.
I'm trying to write a 2D game in Java that uses the Separating Axis Theorem for collision detection. In order to resolve collisions between two polygons, I need to know the Minimum Translation Vector of the collision, and I need to know which direction it points relative to the polygons (so that I can give one polygon a penalty force along that direction and the other a penalty force in the opposite direction). For reference, I'm trying to implement the algorithm here.
I'd like to guarantee that if I call my collision detection function collide(Polygon polygon1, Polygon polygon2) and it detects a collision, the returned MTV will always point away from polygon1, toward polygon2. In order to do this, I need to guarantee that the separating axes that I generate, which are the normals of the polygon edges, always point away from the polygon that generated them. (That way, I know to negate any axis from polygon2 before using it as the MTV).
Unfortunately, it seems that whether or not the normal I generate for a polygon edge points towards the interior of the polygon or the exterior depends on whether the polygon's points are declared in clockwise or counterclockwise order. I'm using the algorithm described here to generate normals, and assuming that I pick (x, y) => (y, -x) for the "perpendicular" method, the resulting normals will only point away from the polygon if I iterate over the vertices in clockwise order.
Given that I can't force the client to declare the points of the polygon in clockwise order (I'm using java.awt.Polygon, which just exposes two arrays for x and y coordinates), is there a mathematical way to guarantee that the direction of the normal vectors I generate is toward the exterior of the polygon? I'm not very good at vector math, so there may be an obvious solution to this that I'm missing. Most Internet resources about the SAT just assume that you can always iterate over the vertices of a polygon in clockwise order.
You can just calculate which direction each polygon is ordered, using, for example, the answer to this question, and then multiply your normal by -1 if the two polygons have different orders.
You could also check each polygon passed to your algorithm to see if it is ordered incorrectly, again using the algorithm above, and reverse the vertex order if necessary.
Note that when calculating the vertex order, some algorithms will work for all polygons and some just for convex polygons.
I finally figured it out, but the one answer posted was not the complete solution so I'm not going to accept it. I was able to determine the ordering of the polygon using the basic algorithm described in this SO answer (also described less clearly in David Norman's link), which is:
for each edge in polygon:
sum += (x2 - x1) * (y2 + y1)
However, there's an important caveat which none of these answers mention. Normally, you can decide that the polygon's vertices are clockwise if this sum is positive, and counterclockwise if the sum is negative. But the comparison is inverted in Java's 2D graphics system, and in fact in many graphics systems, because the positive y axis points downward. So in a normal, mathematical coordinate system, you can say
if sum > 0 then polygon is clockwise
but in a graphics coordinate system with an inverted y-axis, it's actually
if sum < 0 then polygon is clockwise
My actual code, using Java's Polygon, looked something like this:
//First, find the normals as if the polygon was clockwise
int sum = 0;
for(int i = 0; i < polygon.npoints; i++) {
int nextI = (i + 1 == polygon.npoints ? 0 : i + 1);
sum += (polygon.xpoints[nextI] - polygon.xpoints[i]) *
(polygon.ypoints[nextI] + polygon.ypoints[i]);
}
if(sum > 0) {
//reverse all the normals (multiply them by -1)
}