Geolocations - How to check if 2 circles are overlapping - algorithm

Let's say we have 2 locations (latitude,longitude) and each location has a radius (it may be different from each other), making a circle. How to check if these 2 circles are overlapping?

Check if the distance between the centers is smaller than the sum of the radii.
Say for circles A and B with radius Ar and Br respectively, and coordinates (Ax, Ay) and (Bx, By) respectively, the distance between the circles is
D = sqrt( (Ax - Bx)2 + (Ay - By)2 )
They overlap when
D < Ar + Br
There's a catch, however: the centers of the circles are placed on a sphere. The shortest distance between them is a straight line, beneath the sphere's surface. The distance between them following the surface will be larger. For instance, the distance between the North and South pole is 2 Earth radii, but the path on the surface will be 2π Earth radii. Also, these circles don't overlap. So, the above equations only hold when the distances are relatively small.

Related

Finding the length of 3 rectangles so that they share one corner to form a triangle, given a common width and 3 points

Hi sorry for the confusing title.
I'm trying to make a race track using points. I want to draw 3 rectangles which form my roads. However I don't want these rectangles to overlap, I want to leave an empty space between them to place my corners (triangles) meaning they only intersect at a single point. Since the roads have a common width I know the width of the rectangles.
I know the coordinates of the points A, B and C and therefore their length and the angles between them. From this I think I can say that the angles of the yellow triangle are the same as those of the outer triangle. From there I can work out the lengths of the sides of the blue triangles. However I don't know how to find the coordinates of the points of the blue triangles or the length of the sides of the yellow triangle and therefore the rectangles.
This is an X-Y problem (asking us how to accomplish X because you think it would help you solve a problem Y better solved another way), but luckily you gave us Y so I can just answer that.
What you should do is find the lines that are the edges of the roads, figure out where they intersect, and proceed to calculate everything else from that.
First, given 2 points P and Q, we can write down the line between them in parameterized form as f(t) = P + t(Q - P). Note that Q - P = v is the vector representing the direction of the line.
Second, given a vector v = (x_v, y_v) the vector (y_v, -x_v) is at right angles to it. Divide by its length sqrt(x_v**2 + y_v**2) and you have a unit vector at right angles to the first. Project P and Q a distance d along this vector, and you've got 2 points on a parallel line at distance d from your original line.
There are two such parallel lines. Given a point on the line and a point off of the line, the sign of the dot product of your normal vector with the vector between those two lines tells you whether you've found the parallel line on the same side as the other, or on the opposite side.
You just need to figure out where they intersect. But figuring out where lines P1 + t*v1 and P2 + s*v2 intersect can be done by setting up 2 equations in 2 variables and solving that. Which calculation you can carry out.
And now you have sufficient information to calculate the edges of the roads, which edges are inside, and every intersection in your diagram. Which lets you figure out anything else that you need.
Slightly different approach with a bit of trigonometry:
Define vectors
b = B - A
c = C - A
uB = Normalized(b)
uC = Normalized(c)
angle
Alpha = atan2(CrossProduct(b, c), DotProduct(b,c))
HalfA = Alpha / 2
HalfW = Width / 2
uB_Perp = (-uB.Y, ub.X) //unit vector, perpendicular to b
//now calculate points:
P1 = A + HalfW * (uB * ctg(HalfA) + uB_Perp) //outer blue triangle vertice
P2 = A + HalfW * (uB * ctg(HalfA) - uB_Perp) //inner blue triangle vertice, lies on bisector
(I did not consider extra case of too large width)

How to Follow the Path of a Super Ellipse with 2 Linked Axis

I have two axis linked together ; Axis A, and Axis B. Axis B is attached to the end of Axis A and so its point of origin can vary with the angle of Axis A . Attached to Axis B is a Circle whose Diameter is 10 (and can become smaller). I need to move the edge point of the circle to intersect a Super-Ellipse at each of 38 Cartesian points x,y. So the end point of my axis B - center of the circle should follow the same basic path as the 38 points of the super ellipse - radius of circle. Once I have these points - I will need to determine the position of Axis A x_2,y_2 and angle (or more appropriately just the distance from 0 degree angle to reach the required angle to position x_2,y_2. I then need to position Axis B with relation to Axis A in order to have Axis B X_3,Y_3 match the following of the Super-Ellipse where the center of the circle is supposed to be.
I have a drawing attached and a plot in Excel where I am off as you can see the bow tie is not what I should have. I have also included the points to the super ellipse along with some quick points on the graph. I am not a math major - I am willing to learn if you post the name of an equation - so far I have learned about carnot, parametric equation for circles and formulas for parabolas - but I am still having trouble.
Axis A Radius 13" image is 90 degree rotation
X_Sub1 , Y_Sub1
-6.5 , 5
Axis B Radius 9" image is 180 degree rotation
X_Sub2 , Y_Sub2
6.5 , 5
Circle Diameter 10"
Circle Radius 5"
Super Ellipse
# 12"width
# 8.75" Deep Vertex -8.75
Points Along the Super Ellipse.
0.0000, 0.0000
0.2188, -0.6250
0.2188, -1.2500
0.2433, -1.8750
0.3290, -2.5000
0.4753, -3.1073
0.6804, -3.7091
0.9424, -4.2990
1.2585, -4.8712
1.6255, -5.4197
2.0397, -5.9388
2.4967, -6.4233
2.9920, -6.8682
3.5203, -7.2889
4.0764, -7.7213
4.6544, -8.0500
5.2285, -8.3553
5.7525, -8.5000
6.2188, -8.5516
6.6851, -8.5000
7.1891, -8.3553
7.7832, -8.0500
8.3612, -7.7213
8.9173, -7.2889
9.4456, -6.8682
9.9409, -6.4233
10.3979,-5.9388
10.8121,-5.4197
11.1791,-4.8712
11.4952,-4.2990
11.7572,-3.7091
11.9623,-3.1073
12.1086,-2.5000
12.1943,-1.8750
12.2188,-1.2500
12.2188,-0.6250
12.4376, 0.0000
First, suppose we have 3 points along the super-ellipse, p0 = (2.4967, -6.4233), p1 = (2.9920, -6.8682), and p2 = (3.5203, -7.2889). Let's figure out how to position it to touch the point p1.
First of all at the point p1 the tangent line should be very close to parallel to the line from p0 to p2. So it should be parallel to p2 - p0 = (3.5203, -7.2889) - (2.4967, -6.4233) = (1.0236, -0.8656). But you want the disk to be perpendicular to this. We can construct a perpendicular to the vector (x, y) by (-y, x), which gives us (0.8656, 1.0236) as a direction. (There are two perpendiculars, looking at the diagram this is obviously the correct one.) This means that we want to put the end of axis B a distance of 5 in that direction. Which means it needs to be a distance of 5 in the direction (0.8656, 1.0236) from p1 = (2.9920, -6.8682). So it should be at the position (2.9920, -6.8682) + 5 * (0.8656, 1.0236) / sqrt(0.8656^2 + 1.0236^2) = (6.22057, -3.050307).
Now that we know where the end of axis B is, assuming we know where the start of axis A is (you didn't specify that), we can use the law of cosines (see http://mathworld.wolfram.com/LawofCosines.html) to figure out the cos of the angles that you want. Now you can use the arccosine function to figure out the angles in question.
This procedure can be followed for every point. Figure out what you think it should be tangent to, find the orthogonal, find where you want the end of B to be, then you have a triangle that you can use the cosine law on.

How to check the intersection of two squares (Not axis aligned)

I would like to check the intersection of two squares which are not axis-aligned. I know how to do it for axis-aligned squares. Can I extend the same idea?
Basically I want to distribute squares by gaussian distribution on the x-y plane in +ve quadrant say, but two squares should not be intersecting so I have to shift the original center of the square. Suggestions are welcome.
Separating axis theorem (link2) is suitable for effective checking of convex polygon intersection. For squares prerequisite (normals and so on) calculation becomes especially simple.
After almost 4-5 hours of thinking, I got one click. Just sharing if anyone needs this. PseudoCode
Input: Square1 and Square2
Bool(Sqaure1,square2)
for vertex v0 to v3 of sqaure 1
angle = 0;
for vertex P0 to P3 of square 2 (In anticlockwise of clockwise way)
angle = angle + angle(Pj,vi,P((j+1)%4));
if(abs(angle-360) == 0)
return true;
return false;
IDEA: point of one square inside or on the line of another square will have angle sum of 360 with all points if two squares intersects.So you can call function twice by swapping arguments and if anyone returns true then answer is yes.(Call twice or check if angle sum is 0 or 360 exactly)
If I am right, you can proceed as follows:
rotate both Sa and Sb so that Sa becomes axis-aligned;
check overlap of Sa' with the bounding box of Sb';
rotate both Sa and Sb so that Sb becomes axis-aligned;
check overlap of the bounding box of Sa" with Sb".
If there is a configuration with no overlap, then the squares are disjoint.
In analytical terms, the no-overlap conditions will have a form like
C1 + C2 (|cos(t1+t2)| + |sin(t1+t2)|) < 2D Min(|cos(t1)|, (|sin(t1)|)
where C1, C2 are the sides, D the distance between centers and t1, t2 are the angles between sides and the center line, and similar by exchange of 1/2.

Intersection points between 2 discs in 3d

i am trying to find a algorithm or a way to find the intersection between two circle on a sphere (in 3d). For example if i have two circles center at two pointsA(latitude1,longitude1) and B(latidude2,longitude2)
assuming that they intersect, how can i find the intersection between those two circles? is there an algorithm to do that?
Thank you
Convert from latitude/longitude to 3D Cartesian
coordinates.
For each circle, find the equation nx x + ny y + nz z = d of the
plane whose intersection with the sphere is the circle. Assuming
that the sphere is centered at the origin, the normal vector
(nx, ny, nz) is the circle center (cx, cy, cz) (projected or
not) after normalization.
(cx, cy, cz)
(nx, ny, nz) = -----------------
||(cx, cy, cz)||
2
The distance d is computed using Pythagoras. Let r be the radius
of the circle and R be the radius of the sphere.
2 2 2
R = d + r
_______ _______________
| 2 2 |
d = \|R - r = \|(R + r) (R - r)
The second expression is preferred for numerical stability.
If we know only the length r' on the surface of the sphere from
the projected center of the circle to the circle, then compute
d = R cos(r'/R)
r = R sin(r'/R).
We actually don’t need r in this case.
Find the intersection of the two planes, a
line.
Find the intersection of the line and the sphere, between zero and two
points.
Convert the points to
latitude/longitude.

How can I find the middle points of x, y of a line segment in a binary image?

I have some damaged line segments in a binary image and I need to fix them (make them straight and at their original thick). In order to do that I have to find the middle points of the segment, so when I check the neighborhood to find the thickness of the lines I'll be able to find where the pixel stops being 1 and becomes 0.
Assuming your damaged line segments are straight, you can use regionprops in MATLAB to find the center of each bounding box. Because if a segment is straight, its is always the diagonal line of the bounding box, thus the center of the box is also the center of the semgent.
Let's call the points A and B to reduce ambiguity, A(Xa, Ya) and B(Xb, Yb)
Let C be the middle point.
C(Xc, Yc)
Xc = (Xa + Xb) / 2
Yc = (Ya + Yb) / 2
We have four interesting numbers, two for the X coordinates and two for the Y coordinates.
Xmin = floor(Xc)
Xmax = ceil(Xc)
Ymin = floor(Yc)
Ymax = ceil(Yc)
The X coordinate of your middle point is either Xmin or Xmax, the Y coordinate of your middle point is either Ymin or Ymax.
So we have four potential points: (Xmin, Ymin), (Xmin, Ymax), (Xmax, Ymin), (Xmax, Ymax).
So, finally, we must decide which point is nearest to C.
Distance from P(Xp, Yp) to C(Xc, Yc) is:
sqrt(sqr(Xp - Xc) + sqr(Yp - Yc))
Calculate the four distance from the four points to C, choose the minimum and that will be the best possible middle point.
Suppose
A = [xa ya];
B = [xb yb];
then
C = round( mean([A;B]) );
Matlab's round rounds numbers towards their nearest integer, so this minimizes the (city-block) distance from the analytical center (mean([A;B])) to the nearest pixel.
If you want to keep sub-pixel precision (which is actually advisable for most calculations until an explicit map from a result to pixel indices is required), just drop the round and use only the mean part.

Resources