What is the unit to measure distence between any 2 points in an image of a spherical object.
For example the distance between the 2 red points on the tennis ball.
NOTE: As a matter of fact, the "Euclidean distance" cannot be used, since ball is non-Euclidean. Ball is almost spherical, the imaging system projects the ball surface on a plane, where the image elements are not equispaced, neither represent equal areas. The true Euclidean distance depends on the actual position of the points with respect to the camera.
General idea
Supposing the picture takes an isometric projection of space, we can measure coordinates on the picture as if they were coordinates on a plane.
From there we can transform them into an arbitrary ball-centric spherical coordinate system, and then we'll easily get the distance between them.
Definitions
Let us suppose you know the radius r of the sphere. We'll use a coordinate system centered at the center of the sphere, with x the direction orthonormal to the plane projection induced by the picture (thus the vector comes right out of the photo). Then the directions y and z are in the picture, let's take y horizontal and z vertical. See drawing for reference.
Then the spherical coordinate system induced by this is such that we have a distance to the centre which is always r on the sphere, and 2 angles theta and phi :
(source: motionscript.com)
.
Now we can convert each point into spherical coordinates and compute the distance between them.
Convert to spherical coordinates
For each point, the z coordinate is the vertical distance on the picture between the point and the horizontal line that cuts the ball in two equal halves. Express it in terms of r, the radius of the ball, thus z = c * r, with c in [-1,1], negative if the point is below the line, positive if above.
We know that z = r * cos(theta), so theta = arccos(c). Since theta is in [0,Π], no special cases here.
Now measure y in the same way, which is the horizontal distance (to the right is positive) between the point and the vertical line cutting the ball in 2. With y = r * b, and b in [-1,1].
We need theta's sine, which is sin(theta) = sqrt(1 - c*c), then it comes that phi = arcsin( b / sqrt(1 - c*c) ). Because we can see the point on the picture, we know that it has x > 0 by definition of our coordinate system. That means that phi is in [-Π/2,Π/2], so again, no tricks or surprises in the trigonometry here.
Distance between points on a sphere
Well everything is explained in this math exchange question, because most great-arc distances are expressed in terms of latitude and longitude, which use different conventions.
Now we replace elements of the formula in term of the c1, c2, b1 and b2 we previously computed :
The formula you eventually get is , where cos-1 is also known as the arccos function.
I won't delve into the detail (especially because it's such a pain to include latex from a mobile app), but the steps are :
expand the difference inside the cosine
in the only non-trivial term transform the phi's cosines with sqrt(1-sin2)
push both sin(theta) of that term inside the square root, some multiplying with the sin(phi) will give you the b squared terms
express remaining squared sines under the square root as 1-cos2
The final unit of measure will be in whatever unit you express r.
As you can see, you only need the radius at the end, after the arccos (for bs and cs, you only need the size of r and respectively ys and zs on the picture, not in the physical world).
Then, if you are only going to compare distances of points on that same sphere, you may simplify by r, and compare the angles between points at the center of the sphere (i.e. use only the arccos's result without multiplying by r), since these angles are proportional to the arc's distances on the sphere. Your unit of measurement would then be in radians.
Related
For a project I want to do a very simple Pythagoras calculation in C++. An object is equiped with an IMU sensor that gives either a Quaternion rotation or Euler angles. What I want to know is the opposite sides of the triangle underneath the object.:
I want to know these sides of the triangle for both the X and Y axis (black arrows):
This is pretty much very simple, except for the fact that the object can rotate. When the object is rotated I still want to use the X and Y axis in world space (black arrows), but when yawing the Euler angles of the IMU provide me with pitch and roll, which are in local space (red arrows):
In what way can I still get the world space angles (black arrows) while yawing, to be able to calculate my simple Pythagoras calculation? If I can't get them, is there a way to calculate the opposite sides I want using Quaternions?
We can do the calculation by taking into account the Euler angles in the following order -
Pitch
First of all, as you change the roll of the sensor, the sensor "ray" sweeps out a plane inclined to the horizon at angle pitch. We need to first calculate the closest distance between (i) the line of intersection between the plane and the ground, and (ii) the point directly below the sensor on the ground. This is given by d = h * tan(pitch).
Roll
Next we need to do another trigonometric step. As before the roll sweeps through the plane. The offset distance along the axis perpendicular to the line joining (i) and (ii) is given by f = h / cos(pitch) * tan(roll). This gives the intersection point on the ground to be (d, f)
Yaw
Previously, we considered a frame in which the yaw was zero. We now need to rotate this intersection point around the Z-axis by yaw. Thus the final intersection point is given by (x, y) = (d * cos(yaw) - f * sin(yaw), d * sin(yaw) + f * cos(yaw)). You can calculate the "space angle" you want by taking atan2(y, x).
I have 2 sets of points that are restricted to live on the 3D unit sphere, call them {pi} and {qi} (I'll assume correspondence is known). The goal is to register one set to the other, through rotations and translations. Typically I would have used a transformation of the form:
P = RQ + T
where R is a rotation matrix and T a translation vector.
But in this case there is an extra constraint that all points must live on the sphere, how can I include this condition.
Assuming the sets are 'rigid', so you can slide and rotate the whole set on the sphere, but can not change distances between points within a set, all possible transformations are rotations.
Whenever you rotate the set relative to some axis, points move in planes perpendicular to the axis. So all displacements are vectors normal to the axis vector. So each two displacement vectors should make a vector product parallel to the axis vector.
Now, if you already know the correspondence between P and Q points, calculate displacement vectors di from each qi to a corresponding pi and calculate some vector products:
di × dj = (pi - qi) × (pj - qj)
If they have directions close enough to each other, you can assume you have the rotation axis.
Now for each pair or pi,qi find a point ti on an axis such that the PQT triangle is normal to the axis. The angle at the T vertex defines the rotation to slide qi to pi. If all respective angles are equal, you're done. Otherwise you'll have to seek some approximate solution...
I have a circle I want to divide up in to a number of segments all defined by X and Y coordinates. How to I test to see if a point (X, Y) is in a particular segment?
A code example would be preferable.
You don't need to use trigonometry for this (and in general, trigonometry should be avoided whenever possible... it leads to too many precision, domain, and around-the-corner problems).
To determine whether a point P is counter-clockwise of another point A (in the sense of being in the half-plane defined by the left side of a directed line going through the origin and then through A), you can examine the sign of the result of Ax*Py - Ay*Px. This is generally known as the "perpendicular dot product", and is the same as the Z coordinate of the 3D cross product.
If there are two points A and B (with B defining the CCW-most extent) defining a sector, and the sector is less than half the circle, any point which is CCW of A and CW of B can be classified as in that sector.
That leaves only a sector which is more than half of the circle. Obviously, a given set of points can only define at most one such sector. There's clever things you can do with angle bisection, but the easiest approach is probably just to classify points as in that sector if you can't classify them as being in any other sector.
Oh, forgot to mention -- determining the order of the points for the purposes of pairing them up for sectors. Not to go against my previous advice, but the most straightforward thing here is just to sort them by their atan2 (not atan... never ever use atan).
Use the polar coordinate system centred at the centre of the circle, and examine the angular coordinate (φ in the Wikipedia article).
What exactly you do with φ depends on how your segments are defined. For example, if you have n equal segments that start at 0 radians, floor(φ * n / (2 * π)) will give you the segment number.
Your segment is defined by two intersections between the circle and a line. You just have to know if:
The angle between the center of your circle and your point is between
the angles formed by the two previous points and the center.
the point is in the circle (the length from this point to the center is smaller than the radius)
from what side is the point compared to the line (it must be beyond the line).
Remark
In geometry, a circular segment (symbol: ⌓) is a region of a circle
which is "cut off" from the rest of the circle by a secant or a chord.
Here is a segment:
If x & y are not already relative to the center of the circle, subtract the coordinates of the center of the circle:
x -= circle.x
y -= circle.y
Use atan2 to get the angle of the point about the origin of the circle:
angle = atan2(y, x)
This angle is negative for points below the x-axis, so adjust to always be positive:
if (angle < 0) angle += 2 * pi
Assuming your segments are equally spaced, use this formula to get the index of the segment:
segment = floor((angle * numSegments) / (2 * pi))
If you find the result is referring to a segment on the opposite side of the circle to what you want, you might have to do y = -y in the beginning or a segment = (numSegments - 1) - segment at the end to flip it round the right way, but it should basically work.
I have a webpage that I am using a Google Map on. When the user drags the map and lets go, I need to query a server for all data points that fall within the bounds of the visible region of the map. I can quite easily get the North-East and South-West coordinate of the visible region of the map through the javascript API, essentially providing a bounding rectangle. However on the server, I am relying on a database whose geographic query API only supports queries in the form of a center point and a radius. So basically what I would like to do is figure out the minimum radius circle I would need to at least encompass the North-East and South-West points.
The simplest algorithm I thought of involved finding the center point between the NE and SW coordinate and then measuring the radius as the distance from the center point to either the NE or SW coordinate. In a simple euclidean space I'd be comfortable doing this, but I think I'd probably get something wrong with the Earth's non-flat coordinate system. I haven't even been able to convince myself that if I knew that centerpoint that the distance would be the same between the center and NE and the center and SW.
I've come across algorithms for smallest circles on a flat 2D surface and also algorithms describing the opposite i.e. bounding box from circle center and radius. I haven't come across a concise algorithm for this particular problem though.
I assume what you call the east-west and north-south coordinates are the longitude and latitude. You can convert them to Cartesian points and find the midpoint between the edge points of your region. This will yield a point C' below Earth's surface with the same latitude and longitude as your centre point C. (This will only work if the difference of your longitudes is smaller than 180°, however; otherwise you'll get a point on the opposite side of the earth, but with the same latitude.) If you need Cartesian coordinates for your centre point, you can project C' onto the surface by adjusting the radius to find your new centre point.
The distance bewteen the two points on the surface of Earth can be calculated with the great-circle disnatce formula.
Transformation is easy if you assume that Earth is a perfect sphere with radius R = 6373 km:
x = R * cos(lat) * cos(lon)
y = R * cos(lat) * sin(lon)
z = R * sin(lat)
and back:
lon = atan2(y, x)
lat = atan2(z, r) with r = sqrt(x*x + y*y)
(But Earth does not have a constant radius, so you might want to use a better coordinate system, maybe ECEF as explained in this answer if you need more precision.)
My first thought was to find your midpoint in terms of longitude and latitude, which should be okay if you take care of wrapping for the latitude. Then you calculate your distance accpording to the great-circle formula. But averaging the longitudes and latitudes does not seem to be sensible if your map region includes a pole.
It does not need to be very accurate. Does anyone know a good way to do this?
any help is much appreciated.
When you say “it does not need to be very accurate” you don’t say how inaccurate a solution you’re prepared to accept. Also, you don’t say how big the geographic region under consideration is likely to be. These two criteria make a big difference to the kind of approach that needs to be taken.
With a small regions (a few kilometres, say), a flat approximation may be good enough (for example, the Mercator projection) and some of the other responses tell you how to do that. With larger regions you have to take the Earth’s sphericity into account. And if you want inaccuracy less than a percent or so, you need to take the eccentricity of Earth into account.
I’m going to assume for the purposes of this answer that a spherical approximation is good enough, and that your points are at similar enough altitudes that we don’t need to worry about their heights.
You can convert a geographical point (ψ, λ) to Cartesian Earth-centred coordinates using the transformation
(ψ, λ) → (a cos(ψ) cos(λ), a cos(ψ) sin(λ), a sin(ψ))
where a is the mean radius of the Earth (6,371 km). So let’s suppose that the two points that define your line are p₀ and p₁; then the shortest line through p₀ and p₁ is a great circle, which defines a plane that slices the Earth into two halves, with normal n = p₀ × p₁.
Now we need to find the border of the circular region. Suppose the centre of this region is at c and that the surface radius of the region is s. Then the straight-line radius of the region is r = a sin(s/a). We’ll also need the true centre of the circular region, c’ = c cos(s/a). (This point is buried deep underground!)
We’d like to intersect the two circles and solve for the points of intersection. Unfortunately, because of numerical imprecision, the chances are that this procedure will never find any solutions because the imprecise circles will miss each other in 3 dimensions. So I suggest the following procedure: intersect the planes of the two circles, getting the dotted line shown below (unless c’ × n = 0 in which case the two circles are parallel and either c’ = o, in which case they are coincident, or else they do not intersect). Then intersect the line with the circular region.
This two-step procedure reduces the problem to two dimensions, and guarantees that a solution will be found even if numerical imprecision makes the two circles miss in 3 dimensions.
If you need more accuracy than this, then you might need to use geodetic coordinates on a reference ellipsoid such as WGS 1984.
I'd say find the closest point on the line to the center of the circle, then determine whether that point is within the circle (i.e. the distance in question is less than or equal to the circle's radius).
Outline for solving the problem: assume the Earth is a sphere of radius one centered at the origin. Convert all three lat, lon points to 3D coordinates. The two points of the line plus the origin define a plane; intersect that plane with the sphere of radius d centered on the other point. If there is no plane-sphere intersection, then the answer is the line does not intersect the region. If there is a plane-sphere intersection, then the problem is simplified to intersecting the circular region defined by the plane-sphere intersection with the shortest circular arc on the plane going between the end points of the line and centered at the origin. This is a straightforward 2D problem if you convert to the coordinate system of the plane.
This question is too vague to be answered precisely. What do you mean by
a line form by 2 geo points (lat, lon)
This can be either a great circle going through them (also called orthodrome) or it a can be a linear function of spherical coordinates (loxodrome).
BTW, I assume your circle is a circle on the surface of the sphere, right?
Assuming line is formed by points (x1, y1) and (x2, y2), and circle has radius r with origin (0,0):
Calculate:
Incidence = r^2 * [(x2 - x1)^2 + (y2 - y1)^2] - (x1 * y2 - x2 * y1)^2
Then, from the value of Incidence, we can determine the following:
Incidence < 0: No intersection
Incidence = 0: Tangent (intersection at 1 point on circle)
Incidence > 0: Intersection
It's likely your circle is not at the origin (0,0), so to fix this, just add the origin coordinates from your line coordinates in the equation above. So, if the circle is at (x3, y3), x1 in the above equation would become x1 + x3. Likewise, y1 would be y1 + y3, and the same goes for x2 and y2.
For more info check out this link