I would like to calculate the distance between two x/y coordinates on the surface of a torus. So, this is a normal grid that has the property that its corners and sides are 'connected'. For example, on a grid of 500x500, the point at (499, 499) is adjacent to (0, 0) and the distance between e.g. (0,0) and (0,495) should then be 5.
Is there any good mathematical way of calculating this?
So you are looking for the Euclidean distance on the two-dimensional surface of a torus, I gather.
sqrt(min(|x1 - x2|, w - |x1 - x2|)^2 + min(|y1 - y2|, h - |y1 - y2|)^2)
where w and h are the width (x) and height (y) of the grid, respectively.
If/while the distance between x coordinates is larger than half of the grid X size, add grid X size to the smaller x coordinate.
Do the same for Y.
Then calculate the distance.
If your grid wraps around at the edges, there will be four distances between each coordinate (for 2 dimensions). I'm assuming you want to know the shortest distance.
Let's use a smaller grid, the numbers are a bit more manageable. Say the grid is 10x10. Let's also use just one dimension for simplicity (in which case there'll be just two distances), just as you have in your example. Say we have the points 0,2 and 0,6. The two distances between the points are d_1 = (6-2) = 4 and d_2 = (10-6) + 2 = 6, so in this case the shortest distance would be d_1.
In general, you can do the following:
For each coordinate:
subtract the smaller from the larger number
if the result is greater than half the width of the grid the shortest distance in this coordinate is the grid width minus the result
if the result is less than half the width of the grid, the shortest distance in this coordinate is the result
Then using Pythagoras' theorem, the shortest distance between the two points is the square root of the sum of the squares of the shortest distances in each direction. You can calculate the other three distances by calculating Pythagoras' theorem using the other combinations of distances in each direction.
As another poster has said, the shape formed when you wrap round at the edges (for a 2 dimensional grid) is a torus and I think the method I've used above is the same as the equation given but has the advantage that it can be extended to n-dimensions if required. Unfortunately there's not really an easy visualisation above 2 dimensions.
for points (x1,y1) and (x2,y2), you need to calculate 4 distances:
from (x1,y1) to (x2,y2)
from (x1,y1) to (x2, 500-y2)
from (x1,y1) to (500-x2, y2)
from (x1,y1) to (500-x2, 500-y2)
and then take the minimum of those.
Related
I've been trying to figure this out for a while and haven't found the answer.
Given:
Height of ellipse,
Width of ellipse,
Xposition of vector,
Ypostion of vector,
Direction of vector.
Find the distance to the edge of the circle
Here's a simple diagram:
Distance to the edge of a circle
I ran across this post: Calculate Point collision between a point of a given vector and the edge of a Circle But this for a circle, not an ellipse.
This is my first time posting on here. I would be very grateful for any help or pointers about this.
One fairly easy way of doing this is to represent both the ellipse and the vector in their cartesian forms
x^2/a^2 + y^2/b^2 = 1 where a & b are the lengths of the semi-major (half the width) and semi-minor (half the height) axes and the centre of the ellipse is assumed to be at (0,0)
and
y - ypos = m(x - xpos) where xpos and ypos are the position of your vector and m is the slope, the cosine of the angle (direction) it makes with the x axis.
Solve them together to get the intercept and use pythagoras to calculate the distance.
This assumes that the centre of the ellipse is at (0,0) and the axes are parallel to the x and y coordinate axes. If this is not the case then you would need a more general equation for the ellipse which is discussed in great detail here in Wikipedia.
As wierdan points out in his comment you can get 0,1 or 2 solutions.
0 if the vector starts outside the ellipse and misses it completely.
1 if the vector is a tangent to the ellipse.
2 if the vector either passes through the ellipse or it's start point is inside the ellipse.
In the case of 2 solutions 0,1 or 2 may be valid
If the vector direction points away from the ellipse then the solutions are for the reciprocal vector, the one pointing 180 degrees in the opposite direction. This may also apply to the tangent solution. So the solution(s) are not valid by your criteria.
If the start point is inside the ellipse then one solution will be for the result you want and the other for the reciprocal vector. So only one solution will be valid.
If the vector passes through the ellipse then both solutions are valid, your choice if you ignore the furthest one.
The calculation is for games so a approximation is better than a computational intense correct calculation. What i need to find is the radius of the given arc.
In this case the "curve" can be thought of a arc as this approximation is good enough. So the situation look like this:
I know:
the length the green lines (which are equal)
the length of the blue arc
the value of α in degrees
What i need to know:
the radius r
Background - Actually i need the radius for two things:
To calculate the length of a arc B with offset of x from the center arc. So that r of B would be r + x
To calculate the centrifugal force of a vehicle driving on that curve
What i tried:
I know how to calculate the radius if i have the circumference and the inner angle of the arc. But i am completely stuck with the given information though i am sure it should not be too complicated ..
If you think a triangle with right angle at the middle of the green line and another point at the center of the circle, then the angle at the intersection of the green segments in that triangle is α/2 and the cosine ratio for that angle is
cos(α/2)*r = g/2
g the length of a green segment.
The angle at the top of a pie slice is π-α so that for the length b of the blue curve segments you should get
b = (π-α)*r
The radius values you get from both formulas should not differ more than the measurement errors let expect.
Alternatively, you could use Catmull-Rom (https://en.wikipedia.org/wiki/Centripetal_Catmull%E2%80%93Rom_spline), Hermite (https://en.wikipedia.org/wiki/Cubic_Hermite_spline), or natural (https://en.wikipedia.org/wiki/Spline_interpolation) cubic spline interpolation to calculate the path between the points.
This will give you cubic polynomials for the (x,y) coordinates, and it's easy to take their 2nd derivatives to get the direction and magnitude of acceleration.
Sometimes was a problem what is the rectangle 3D rotated and be perspective transition (for example in CSS) draw as the tetragon. But we want obtain the rectangle (width, length, Euler angle, perspective) transformed via rotate and perspective draw as the tetragon.
figure
fig.1 points a,c diagonal rectangle(yellow) points A,C diagonal tetragon(shadow) (red)
fig.2 a,b,c,d rectangle points(yellow) A,B,C,D shadow(tetragon) (red)
Solve:
Coordinate system:
The origin of the coordinate system is coincident with diagonals intersection point. Axe Z normal to the tetragon. Axe X crosses point A
a,b,c,d;- ;- rectangular with coordinates
a(x1,y1,z1);
b(x2,y2,z2);
c(x3,y3,z3);
a(x4,y4,z4);
A,B,C,D-shadow. Corner points A(q1,p1,0);
B(q2,p2,0); C(q3,p3,0);
D(q4,p4,0);
k perspective.
In that system of coordinate y1=y3=0.
Fig1.
From similarity transformation triangles is:
x1=1-z1/kq1;
x3=1-z3/kq3
From statement of problem was that diagonal cross is in the origin of the coordinate thus:
z3=-z1 и x3=-x1
Substituting in expression above and equating to each other was :
x1=2*q1*q3/(q3-q1);
z1=(q1+q3)/(q1-q3)*k.
To simplify other calculation imagine that second rectangle diagonal (bd) lie in coordinate system in that Y coordinate of diagonal points is equal zero. In this coordinate system coordinate points b and d was the same as point a and c but we must change z1 to z2, z3 to z4, x1 to x2, x3 to x4,q1 to q2, q3 to q4. To translate from imagine system to real system use rotation coordinate formula (Z axe is the same, z coordinate is equals)
Fig.2
x=x'*cos(a); y=y'*sin(a); The result was:
x2=-x4=2*q2*q4/(q4-q2);
y2=-y4=x2*tan(a);
z2=-z4=(q2+q4)/(q2-q4)k;
tan(a)=(p2-p4)/(q2-q4)
abcd was parallelogram. Diagonal cross point divide diagonal to half. We need to one more expression to make rectangular. Use angle equal 90 degrees. Make scalar multiplication vector of two side in abcd. In coordinate it was:
(a-b)(d-a)=y4y2+(x1-x4)(x1-x2)+(z1-z4)*(z1-z2)=0;
f=(q1*q2-q3q4)(q1*q4-q2*q3)
g=-tan2(a)*q42q22(q1-q3)2+(-q1q2(q3+q4)+q3q4(q1+q2))*(q1q2(q4-q3)+q3q4(q1-q2))
We receive equation to k(perspective): f*k2-g=0, solve it
k=sqrt(g/f).
Collect all formula we get all coordinates of point abcd.
From coordinate of corner is simple to calculate side of rectangular.
Calculating quaternion, rotation matrix, angles see calculate quaternion by coordinate 2 points of object in two positions
Given a point p exterior to an axially aligned, origin centered ellipse E, find the (upto) four unique normals to E passing through p.
This is not a Mathematica question. Direct computation is too slow; I am willing to sacrifice precision and accuracy for speed.
I have searched the web, but all I found involved overly complex calculations which if implemented directly appear to lack the performance I need. Is there a more "programmatical" way to do this, like using matrices or scaling the ellipse into a circle?
Let's assume the ellipse E is in "standard position", center at the origin and axes parallel to the coordinate axes:
(x/a)^2 + (y/b)^2 = 1 where a > b > 0
The boundary cases a=b are circles, where the normal lines are simply ones that pass through the center (origin) and are thus easy to find. So we omit discussion of these cases.
The slope of the tangent to the ellipse at any point (x,y) may be found by implicit differentiation:
dy/dx = -(b^2 x)/(a^2 y)
For the line passing through (x,y) and a specified point p = (u,v) not on the ellipse, that is normal to ellipse E when its slope is the negative reciprocal of dy/dx:
(y-v)/(x-u) * (-b^2 x)/(a^2 y) = -1 (N)
which simplifies to:
(x - (1+g)u) * (y + gv) = -g(1+g)uv where g = b^2/(a^2 - b^2)
In this form we recognize it is the equation for a right rectangular hyperbola. Depending on how many points of intersection there are between the ellipse and the hyperbola (2,3,4), we have that many normals to E passing through p.
By reflected symmetry, if p is assumed exterior to E, we may take p to be in the first quadrant:
(u/a)^2 + (v/b)^2 > 1 (exterior to E)
u,v > 0 (1'st quadrant)
We could have boundary cases where u=0 or v=0, i.e. point p lies on an axis of E, but these cases may be reduced to solving a quadratic, because two normals are the (coinciding) lines through the endpoints of that axis. We defer further discussion of these special cases for the moment.
Here's an illustration with a=u=5,b=v=3 in which only one branch of the hyperbola intersects E, and there will be only two normals:
If the system of two equations in two unknowns (x,y) is reduced to one equation in one unknown, the simplest root-finding method to code is a bisection method, but knowing something about the possible locations of roots/intersections will expedite our search. The intersection in the first quadrant is the nearest point of E to p, and likewise the intersection in the third quadrant is the farthest point of E from p. If the point p were a good bit closer to the upper endpoint of the minor axis, the branches of the hyperbola would shift together enough to create up to two more points of intersection in the fourth quadrant.
One approach would be to parameterize E by points of intersection with the x-axis. The lines from p normal to the ellipse must intersect the major axis which is a finite interval [-a,+a]. We can test both the upper and lower points of intersection q=(x,y) of a line passing through p=(u,v) and (z,0) as z sweeps from -a to +a, looking for places where the ellipse and hyperbola intersect.
In more detail:
1. Find the upper and lower points `q` of intersection of E with the
line through `p` and `(z,0)` (amounts to solving a quadratic)
3. Check the sign of a^2 y(x-u) - b^2 x(y-v) at `q=(x,y)`, because it
is zero if and only `q` is a point of normal intersection
Once a subinterval is detected (either for upper or lower portion) where the sign changes, it can be refined to get the desired accuracy. If only modest accuracy is needed, there may be no need to use faster root finding methods, but even if they are needed, having a short subinterval that isolates a root (or root pair in the fourth quadrant) will be useful.
** more to come comparing convergence of various methods **
I had to solve a problem similar to this, for GPS initialization. The question is: what is the latitude of a point interior to the Earth, especially near the center, and is it single-valued? There are lots of methods for converting ECEF cartesian coordinates to geodetic latitude, longitude and altitude (look up "ECEF to Geodetic"). We use a fast one with only one divide and sqrt per iteration, instead of several trig evaluations like most methods, but since I can't find it in the wild, I can't give it to you here. I would start with Lin and Wang's method, since it only uses divisions in its iterations. Here is a plot of the ellipsoid surface normals to points within 100 km of Earth's center (North is up in the diagram, which is really ECEF Z, not Y):
The star-shaped "caustic" in the figure center traces the center of curvature of the WGS-84 ellipsoid as latitude is varied from pole to equator. Note that the center of curvature at the poles is on the opposite side of the equator, due to polar flattening, and that the center of curvature at the equator is nearer to the surface than the axis of rotation.
Wherever lines cross, there is more than one latitude for that cartesian position. The green circle shows where our algorithm was struggling. If you consider that I cut off these normal vectors where they reach the axis, you would have even more normals for a given position for the problem considered in this SO thread. You would have 4 latitudes / normals inside the caustic, and 2 outside.
The problem can be expressed as the solution of a cubic equation which
gives 1, 2, or 3 real roots. For the derivation and closed form
solution see Appendix B of Geodesics on an ellipsoid of revolution. The boundary between 1 and 3 solutions is an astroid.
I'm using CML to manage the 3D math in an OpenGL-based interface project I'm making for work. I need to know the width of the viewing frustum at a given distance from the eye point, which is kept as a part of a 4x4 matrix that represents the camera. My goal is to position gui objects along the apparent edge of the viewport, but at some distance into the screen from the near clipping plane.
CML has a function to extract the planes of the frustum, giving them back in Ax + By + Cz + D = 0 form. This frustum is perpendicular to the camera, which isn't necessarily aligned with the z axis of the perspective projection.
I'd like to extract x and z coordinates so as to pin graphical elements to the sides of the screen at different distances from the camera. What is the best way to go about doing it?
Thanks!
This seems to be a duplicate of Finding side length of a cross-section of a pyramid frustum/truncated pyramid, if you already have a cross-section of known width a known distance from the apex. If you don't have that and you want to derive the answer yourself you can follow these steps.
Take two adjacent planes and find
their line of intersection L1. You
can use the steps here. Really
what you need is the direction
vector of the line.
Take two more planes, one the same
as in the previous step, and find
their line of intersection L2.
Note that all planes of the form Ax + By + Cz + D = 0 go through the origin, so you know that L1 and L2
intersect.
Draw yourself a picture of the
direction vectors for L1 and L2,
tails at the origin. These form an
angle; call it theta. Find theta
using the formula for the angle
between two vectors, e.g. here.
Draw a bisector of that angle. Draw
a perpendicular to the bisector at
the distance d you want from the
origin (this creates an isosceles
triangle, bisected into two
congruent right triangles). The
length of the perpendicular is your
desired frustum width w. Note that w is
twice the length of one of the bases
of the right triangles.
Let r be the length of the
hypotenuses of the right triangles.
Then rcos(theta/2)=d and
rsin(theta/2)=w/2, so
tan(theta/2)=(w/2)/d which implies
w=2d*tan(theta/2). Since you know d
and theta, you are done.
Note that we have found the length of one side of a cross-section of a frustrum. This will work with any perpendicular cross-section of any frustum. This can be extended to adapt it to a non-perpendicular cross-section.