Based on Finding the centroid of a polygon? (P. Bourke), I would like to compute a weighted centroid (i.e. each vertex has an associated weight). There is a formula given in Find the centroid of a polygon with weighted vertices but I think it is not valid (if weights are all the same, you don't get the centroid (see https://math.stackexchange.com/questions/3177/why-doesnt-a-simple-mean-give-the-position-of-a-centroid-in-a-polygon).
You cannot compare centroid for polygon with masses in vertices only (imagine iron balls connected with cocktail tubes) and centroid for polygon with mass distributed over the area (imagine polygon carved from steel sheet).
In the first case use formula from Find the centroid of a polygon with weighted vertices, in the second- the last link given
Related
Currently I'm working with spatial data and applied a Delaunay triangulation on my data points. I additionally calculated the geodesic distances on the WGS84 ellipsoid for every edge (connection between 2 points) in the triangulation. Now I'm going to search the shortest path between every 2 points in the generated graph and calculate the path distance. The shortest distance should thus be calculated as the sum over all edge distances.
Below is a minimal working example:
library(deldir)
set.seed(31)
x <- runif(100)
y <- runif(100)
d <- deldir(x, y) #preforms tesselation & Delaunay triangulation
#Calculate edge distances (for reasons of simplicity I calculate here Euclidean distances)
geodists <- NULL
for (i in 1:nrow(d$delsgs)) {
geodists[i] <- sqrt((x[d$delsgs[i,5]] - x[d$delsgs[i,6]])^2 + (y[d$delsgs[i,5]] - y[d$delsgs[i,6]])^2)
}
#Plot data
plot(d, wlines="triang")
However, I have no idea how I can perform the shortest path search on the deldir object I created. Thus, I'd be very happy if you could provide some solutions for my problem:
How can I identify which edges are involved in the shortest path between point A and B?
How can I then efficiently calculate the path distance matrix?
Thanks a lot in advance for your help!
There are some path finding algorithms. One of them is A* (Wikipedia Link)
Maybe this helps you.
You can replace the regularly ordered points in an Euclidean Metric by the delaunay points of your collection of points.
Then always go to the next neighbor, which is closest to the finish point.
I have the coordinates of n points on a sphere and I know they are all coplanar. How can I find the edges of the polygon which has the vertex the n given points?
OK, your problem is weird ordering.
Project all points onto any convenient plane - the simplest approach is using OXY, OXZ or OYZ plane (choose one that is not perpendicular to your plane) - in this case you just use (P[i].X. P[i].Y, 0) for P[i] point and sort projected 2D points by angle against the first point - it works because points on sphere arc form convex polygon. Then use this ordering as polygon vertex indexes.
I am looking to calculate the distance between points (about 47K) and the closest X countries (of all world countries). I have imported the lat/long of points as SpatialPoints, and loaded a world map as a SpatialPolygons. I think I could build off of the advice given here:
SpatialLinesDataFrame: how to calculate the min. distance between a point and a line
It looks like I have to calculate the distance between all countries and all points and then extract the X closest, which is a bit intense with so many points.
In short, is there a way to impose a polygon limit? If not, what would you suggest- my only thought is to import a smaller number of points and then loop through this code (I am a new R user).
Thanks!
I have a polygon P made of N vertices. I need an algorithm that, given P, subdivide it in a certain number of convex polygons each using at most M vertices.
Ps.
P is a 2D polygon. Furthermore, i can use a polygon triangulation, but i am interested in algorithms that subdivide P into convex polygons having more than 3 vertices (and, as said above, at most M).
Quadtree methods would be my recommendation. Check those out.
How do you draw the curve representing the shortest distance between 2 points on a flat map of the Earth?
Of course, the line would not be a straight line because the Earth is curved. (For example, the shortest distance between 2 airports is curved.)
EDIT: THanks for all the answers guys - sorry I was slow to choose solution :/
I get this sort of information from the Aviation Formulary.
In this case:
Distance between points
The great circle distance d between
two points with coordinates
{lat1,lon1} and {lat2,lon2} is given
by:
d=acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos(lon1-lon2))
A mathematically equivalent formula,
which is less subject to rounding
error for short distances is:
d=2*asin(sqrt((sin((lat1-lat2)/2))^2 +
cos(lat1)*cos(lat2)*(sin((lon1-lon2)/2))^2))
And
Intermediate points on a great circle
In previous sections we have found
intermediate points on a great circle
given either the crossing latitude or
longitude. Here we find points
(lat,lon) a given fraction of the
distance (d) between them. Suppose the
starting point is (lat1,lon1) and the
final point (lat2,lon2) and we want
the point a fraction f along the great
circle route. f=0 is point 1. f=1 is
point 2. The two points cannot be
antipodal ( i.e. lat1+lat2=0 and
abs(lon1-lon2)=pi) because then the
route is undefined. The intermediate
latitude and longitude is then given
by:
A=sin((1-f)*d)/sin(d)
B=sin(f*d)/sin(d)
x = A*cos(lat1)*cos(lon1) + B*cos(lat2)*cos(lon2)
y = A*cos(lat1)*sin(lon1) + B*cos(lat2)*sin(lon2)
z = A*sin(lat1) + B*sin(lat2)
lat=atan2(z,sqrt(x^2+y^2))
lon=atan2(y,x)
To draw the 3D shortest path between two points on Earth's surface onto a 2D map of Earth's surface, you have to know how the 3D surface of Earth was projected onto the 2D map in question. If you know the projection used, you just need to apply it to the 3D shortest path to project it onto the 2D map. If you don't know the exact projection used, but have access to it through some sort of interface (ie. input 3D surface coords -> output 2D map coords), you could sample points along the 3D surface path, generate their corresponding map points through said interface, and then approximate the projected path with line segments/bezier curves/etc. through the projected sample points.