Point to great-circle segment distance - geometry

I want to calculate the distance from a point given by latitude and longitude to a line-segment given by two points (part of a great-circle). All coordinates are given in WGS84.
I know how to calculate this in Cartesian coordinates but not on a sphere. Can somebody please provide the formula?

spherical to 2D Cartesian
if the distance is not too far and not around pole singularities you can convert both line segment and line emitting from your point and perpendicular to your segment to spherical coordinates (if they are not already) and use the 2 angles as Cartesian space (ignoring radius).
compute intersection point
convert back to spherical
compute arclength between point and intersection
hard to say if you're using sphere or WGS84 or what ....
Cartesian 3D
lets have arc segment AB, sphere of radius R and center C (ideally (0,0,0)) and point P then I see it like this:
find intersection point P' between plane ABC and its normal going through point P in 3D Cartesian
project it back on sphere surface
For spherical surface is this easy as the projection means just to change the vector P'C length to R (if the sphere is centered around (0,0,0)).
P'' = (R*(P'-C)/|P'-C|) + C
compute arclength between the 2 points |P-P''|
Also simple for spherical surface just compute the angle between vectors P-C and P''-C
ang = acos(dot(P-C,P''-C)/(R*R)); // [radians]
and convert to arclength
d = ang*R; // [same Units as R]

This is cross-track distance described here
dxt = asin( sin(δ13) ⋅ sin(θ13−θ12) ) ⋅ R
where
δ13 is (angular) distance from start point to third point
θ13 is (initial) bearing from start point to third point
θ12 is (initial) bearing from start point to end point
R is the earth’s radius
You can calculate needed distance and bearings using formulas from given page
distance
a = sin²(Δφ/2) + cos φ1 ⋅ cos φ2 ⋅ sin²(Δλ/2)
c = 2 ⋅ atan2( √a, √(1−a) )
d = R ⋅ c
where
φ is latitude, λ is longitude, R is earth’s radius (mean radius = 6,371km);
bearing
θ = atan2( sin Δλ ⋅ cos φ2 , cos φ1 ⋅ sin φ2 − sin φ1 ⋅ cos φ2 ⋅ cos Δλ )
where
φ1,λ1 is the start point, φ2,λ2 the end point (Δλ is the difference in longitude)
note that angles need to be in radians to pass to trig functions

Related

Finding the intersection of the Circle and Infinite Cylinder in 3D space

Finding the intersection of the Circle and Infinite Cylinder. (all in 3D)
• Circle is defined by center, plane in which it lies and radius.
• Cylinder is defined by axis and radius.
how can i get the intersection of these two?
WLOG the cylinder has equation X² + Y² = 1 (if not, you can make it so by translation, rotation and scaling).
Then the parametric equation of the circle is
P = C + U cos t + V sin t
where C is the center point and U, V two orthogonal vectors in the circle plane, of length R.
You can rationalize with the substitution cos t = (1 - u²) / (1 + u²), sin t = 2u / (1 + u²).
Combining these equations,
(Cx (1 + u²) + Ux (1 - u²) + Vx 2u)² + (Cy (1 + u²) + Uy (1 - u²) + Vy 2u)² = (1 + u²)²
which is a quartic one. There is no particular simplification of the coefficients.
You can solve numerically or by the closed-form formulas. There can be up to four solutions.
I guess that this is strictly equivalent to finding the intersections between the torus formed by inflating the circle circumference and the straight line obtained by deflating the cylinder to its axis. This is well addressed in the ray-tracing literature.
You can also sse it as a circle/ellipse intersection problem in 2D.
Let's some base point of cylinder is A0, unit axis direction vector is AD, radius AR. Circle center is B0, circle plane unit normal is BN, radius BR.
Circle intersects cylinder, if distance from B0 to cyl. axis is smaller than sum of cylinder radius plus projection of circle radius onto normal to axis
Dist <= AR + BR * Abs(Cos(AD, BN)).
Cos(AD, BN) = DotProduct(AD, BN)
Distance(B0, cyl.axis) = Abs(VectorProduct(AD, B0-A0))

random lat lng coordinates within x km's of another lat lng

We have a location specified by a lat & lng
We want to generate some other random lat & lng locations within say 20km of this location
Does anyone have a good formula for this
Generate two uniform random values r and Fi in range 0..1
Calculate distance as d = Radius * Sqrt(r) (description here for plane circle)
Calculate bearing as Theta=2 * Pi * Fi
Find lat/lon coordinates for given central point and calculated d and Theta as described here in section Destination point given distance and bearing from start point
JavaScript:
(all angles
in radians)
var φ2 = Math.asin( Math.sin(φ1)*Math.cos(d/R) +
Math.cos(φ1)*Math.sin(d/R)*Math.cos(θ) );
var λ2 = λ1 + Math.atan2(Math.sin(θ)*Math.sin(d/R)*Math.cos(φ1),
Math.cos(d/R)-Math.sin(φ1)*Math.sin(φ2));
where φ is latitude, λ is longitude,
θ is the bearing (clockwise from north), d being the distance travelled,
R the earth’s radius

Line intersection - with a difference (excel VBA)

I have been working on a problem and through all of my research I have been unable to find an existing resolution. I do not have the required math knowledge to produce an elegant solution.
There are several examples of working (and very elegant) line - line intersections with either complete lines or line segment (see links) :
https://dl.dropboxusercontent.com/u/99937114/Forums/Eileens%20Lounge%20-%20LinearIntersect_Corrected.xls
(other examples in the dropbox shared folder)
The problem I have is illustrated using the attached image. There is a defined line, starting at L1 (x,y) and finishing at L2 (x,y). There is no second line - instead there are the coordinates for another point (effectively L3) and an axis or angle of travel. I need to calculate the distance to and coordinates of the intersect if the point L3 was continued along the axis / angle of travel.
It would also be useful to get the coordinates of a position when provided with an origin, a bearing and a distance e.g. x, y with a distance of 10m on a bearing of 195 degrees arrives you at x, y? This is effectively the same function, except the distance is defines vs calculating the distance and x, y of the intersection
Coming from a background of projective geometry, computing cross products of homogeneous coordinates is my favorite way to elegantly join points or intersect (infinite) lines. In your case, I'd first rename points. Suppose the first line is defined by A and B, and the second line passes through C and has angle z as you indicated. Then I'd compute
⎡Dx⎤ ⎛⎡Ax⎤ ⎡Bx⎤⎞ ⎛⎡Cx⎤ ⎡sin z⎤⎞ ⎡(Ay Bx - Ax By) sin z - (Cx cos z - Cy sin z) (Ax - Bx)⎤
⎢Dy⎥ = ⎜⎢Ay⎥ × ⎢By⎥⎟ × ⎜⎢Cy⎥ × ⎢cos z⎥⎟ = ⎢(Ay Bx - Ax By) cos z - (Cx cos z - Cy sin z) (Ay - By)⎥
⎣Dz⎦ ⎝⎣1 ⎦ ⎣1 ⎦⎠ ⎝⎣1 ⎦ ⎣ 0 ⎦⎠ ⎣(Ay - By) sin z - (Ax - Bx ) cos z ⎦
Then (Dx/Dz, Dy/Dz) are the coordinates of the point of intersection, and the distance from that to C can be computed in the usual way using Pythagoras. The first inner cross product describes the line joining A and B. The second inner cross product joins C with a point at infinity in the direction you indicated. The outer cross product then intersects these two lines, yielding homogeneous coordinates for the point of intersection.

How to calculate Angles it would take to rotate from one vector to another?

I have two normalized vectors:
A) 0,0,-1
B) 0.559055,0.503937,0.653543
I want to know, what rotations about the axes would it take to take the vector at 0,0,-1 to 0.559055,0.503937,0.653543?
How would I calculate this? Something like, rotate over X axis 40 degrees and Y axis 220 (that's just example, but I don't know how to do it).
Check this out. (google is a good thing)
This calculates the angle between two vectors.
If Vector A is (ax, ay, az) and
Vector B is (bx, by, bz), then
The cos of angle between them is:
(ax*bx + ay*by + az*bz)
--------------------------------------------------------
sqrt(ax*ax + ay*ay + az*az) * sqrt(bx*bx + by*by + bz*bz)
To calculate the angle between the two vectors as projected onto the x-y plane, just ignore the z-coordinates.
Cosine of Angle in x-y plane =
(ax*bx + ay*by)
--------------------------------------
sqrt(ax*ax + ay*ay) * sqrt(bx*bx + by*by
Similarly, to calculate the angle between the projections of the two vectors in the x-z plane, ignore the y-coordinates.
It sounds like you're trying convert from Cartesian coordinates (x,y,z) into spherical coordinates (rho,theta,psi).
Since they're both unit vectors, rho, the radius, will be 1. This means your magnitudes will also be 1 and you can skip the whole denominator and just use the dot-product.
Rotating in the X/Y plane (about the Z axis) will be very difficult with your first example (0,0,-1) because it has no extension in X or Y. So there's nothing to rotate.
(0,0,-1) is 90 degrees from (1,0,0) or (0,1,0). If you take the x-axis to be the 0-angle for theta, then you calculate the phi (rotation off of the X/Y plane) by applying the inverse cos upon (x,y,z) and (x,y,0), then you can skip dot-products and get theta (the x/y rotation) with atan2(y,x).
Beware of gimbal lock which may cause problems.

How to project a point on to a sphere

If i have a point (x,y,z) how to project it on to a sphere(x0,y0,z0,radius) (on its surface).
My input will be the coordinates of point and sphere.
The output should be the coordinates of the projected point on sphere.
Just convert from cartesian to spherical coordinates?
For the simplest projection (along the line connecting the point to the center of the sphere):
Write the point in a coordinate system centered at the center of the sphere (x0,y0,z0):
P = (x',y',z') = (x - x0, y - y0, z - z0)
Compute the length of this vector:
|P| = sqrt(x'^2 + y'^2 + z'^2)
Scale the vector so that it has length equal to the radius of the sphere:
Q = (radius/|P|)*P
And change back to your original coordinate system to get the projection:
R = Q + (x0,y0,z0)
Basically you want to construct a line going through the spheres centre and the point. Then you intersect this line with the sphere and you have your projection point.
In greater detail:
Let p be the point, s the sphere's centre and r the radius then x = s + r*(p-s)/(norm(p-s)) where x is the point you are looking for. The implementation is left to you.
I agree that the spherical coordinate approach will work as well but is computationally more demanding. In the above formula the only non-trivial operation is the square root for the norm.
It works if you set the coordinates of the center of the sphere as origin of the system (x0, y0, z0). So you will have the coordinates of the point referred to that origin (Xp', Yp', Zp'), and converting the coordinates to polar, you discard the radius (distance between the center of the sphere and the point) and the angles will define the projection.

Resources