Finding the distance between two circles - math

I'm trying to figure out how to get the distance from two circles relative to the corners of their square container boxes. I need some help with the maths here.
How can I work out the number of pixels for the line marked with a question mark?
Appreciate the help as always.

tldr: Calculate the distance between each circles center point, then subtract the radius' of each circle from that.
For the purpose of a demonstration, we will assume the following:
The 200px diameter (r1 = 100) circle is at the (x, y) coordinates of (0, 0), and
the 100px diameter (r2 = 50) circle is at (x, y) coordinates of (150, -150).
Given that the distance between their centers is:
To find the distance between their boundaries, we subtract the radius of each circle from the distance between their centers.
This leaves us with the equation:
sqrt((x2 − x1)^2 + (y2 − y1)^2) − (r2 + r1)
Inserting your values into the above gives:
sqrt((150 − 0)^2 + (-150 − 0)^2) − (100 + 50) = 62.132034356px

Do you have the middle point of each circles? If you do, first calculate the distance from the centre of circles.
distance² = center1² + center2²
Then, you will need to minus the radius of both circles. In your case, it will be 150 (100 + 50)

Let's see... each radius is half each side length, and subtracting the sum of the radii from the distance between the center gives you the amount that's left.
Hope that helps?

The algebraically simplified version of Daniel's answer is
(r1 + r2) * (sqrt(2) - 1)
= (s1 + s2) * (sqrt(2) - 1)/2
where r1,r2 are the two radii and s1,s2 are the two square sides. This is easily seen by looking at each square individually and noticing that the distance d1 from the circle/square center to the square corner is sqrt(2) * r, and the desired distance within that square is d1 - the circle radius r.

Related

How to calculate the point lying on the outer circle following the tagent line of the inner circle

My question is about calculating points coordinates in 2D space.
I have two circles - outer and inner, that are centered between them (the inner is in the middle of the outer).
What I know:
-the two circles' radiuses (R1,R2)
-the 2D coordinates of a random point (x) in space always outside of the inner circle
What I want to find out:
-The 2D coordinates of the two points (y,z) that are lying on the outer circle following the two tangent lines from the random point (x)
Here is an illustration of what I need
Let's circles' center is coordinate origin (0,0) (shift other coordinates by true center ones), random point is P, point at big circle is Q, small radius is r, larger one is R.
We could build a system of equations for distance from center to tangent point and for intersection point, but it requires solving of quartic equation with rather long coefficients.
So at first find equation of tangent from point P to small circle with trigonometry:
Dist = Sqrt(px^2+py^2)
tan_angle = ArcSin(r / Dist)
rot_angle = ArcTan2(py, px)
ta1 = rot_angle - tan_angle
ta2 = rot_angle + tan_angle
and tangent points are
t1x = r * sin(ta1)
t1y = - r * cos(ta1)
t2x = - r * sin(ta2)
t2y = r * cos(ta2)
Now for both tangent points solve quadratic equation like
(px + s * (t1x - px))^2 + (py + s * (t1y - py))^2 = R^2
for unknown parameter s, get two solutions s1,s2 and find points of intersections
q11x = px + s1 * (t1x - px)
and so on
Note that solution consists of four points - two tangents, two intersection points for every tangent.

Point with lat/long inside a circle

I know the formula for determining if a point(x, y) is inside a circle is (x - x_c)^2 + (y - y_c)^2 < radius^2 but what if my x and y are in degrees (lat/long)?
As mentioned by interjay, the solution is to calculate the distance between the point and the center of the circle and compare it with the radius. The code is available here:
Calculate distance between two latitude-longitude points? (Haversine formula)

Finding points of intersection when two spheres intersect

I have the center (xyz - in 3 dimensional space) and the radius of two spheres A and B.
Now I have to figure out a point or more than 1 point where these spheres meet. It is fairly easy to figure out if the two spheres collide or not, but how do I find out the points of intersection of 2 spheres?
Any help would be greatly appreciated.
The curve where they intersect is a circle. The equation for the radius of the circle is a bit complicated, but is shown here, in eqn. 8, and this distance of the circle from the center of one of the spheres is shown in eqn. 5.
If the radius of the smaller sphere is A, and the bigger is B, and their centers are D units apart, then the points of intersection are on a circle of radius r centered on a point directly between the centers of the two spheres, which is y units from the center of the bigger sphere, and x units from the center of the other, where
y = 1/2 (D + (B^2 - A^2)/D)
and
x = 1/2 (D - (B^2 - A^2)/D)
with radius
r = B^2 - x^2 = A^2 - y^2
If you need the equation for this circle the best way is to represent it as a set of three parameterized equations, where the x, y, and z coordinates are each expressed a a function of some t, which represents the radius vector trraveling around the circle once, from zero to 2PI...
To construct these equations, think about expressing the point which is the radius r from the center, on the 2D plane which is normal to the line between the two spheres.
Check out this link for some iedas on how to do this..
Derivation is as follows: draw a line between the centers of the two spheres. Label it as D
Designate a point on this line as the center of the final solution circle label it as point O
Label the smaller portion of D as x, and the large portion as y
draw a line from O perpindicular to D, for some distance r to represent the radius of the solution circle
Label the end of this radius as Q
Now draw B between the center of the larger sphere to Q and A from the center of smaller sphere and Q
From Pythagoras:
B^2 = y^2 + r^2 and A^2 = x^2 + r^2
so, after eliminating r and a bit of algebra,
y-x = (B^2 - A*2) / (x+y)
But x+y = D so,
y-x = (B^2 - A*2) / D
Adding the equation x+y=D to the above eliminates the x, giving
2y = D + (B^2 - A*2) / D
or,
y = 1/2 ( D + (B^2 - A*2) / D )

How to generate random shapes given a specified area.(R language).?

My question is this.. I am working on some clustering algorithms.. For this first i am experimenting with 2d shapes..
Given a particular area say 500sq units .. I need to generate random shapes for a particular area
say a Rect, Square, Triangle of 500 sq units.. etc .. Any suggestions on how i should go about this problem.. I am using R language..
It's fairly straightforward to do this for regular polygon.
The area of an n-sided regular polygon, with a circumscribed circle of radius R is
A = 1/2 nR^2 * sin((2pi)/n)
Therefore, knowing n and A you can easily find R
R = sqrt((2*A)/(n*sin((2pi)/n))
So, you can pick the center, go at distance R and generate n points at 2pi/n angle increments.
In R:
regular.poly <- function(nSides, area)
{
# Find the radius of the circumscribed circle
radius <- sqrt((2*area)/(nSides*sin((2*pi)/nSides)))
# I assume the center is at (0;0) and the first point lies at (0; radius)
points <- list(x=NULL, y=NULL)
angles <- (2*pi)/nSides * 1:nSides
points$x <- cos(angles) * radius
points$y <- sin(angles) * radius
return (points);
}
# Some examples
par(mfrow=c(3,3))
for (i in 3:11)
{
p <- regular.poly(i, 100)
plot(0, 0, "n", xlim=c(-10, 10), ylim=c(-10, 10), xlab="", ylab="", main=paste("n=", i))
polygon(p)
}
We can extrapolate to a generic convex polygon.
The area of a convex polygon can be found as:
A = 1/2 * [(x1*y2 + x2*y3 + ... + xn*y1) - (y1*x2 + y2*x3 + ... + yn*x1)]
We generate the polygon as above, but deviate angles and radii from those of the regular polygon.
We then scale the points to get the desired area.
convex.poly <- function(nSides, area)
{
# Find the radius of the circumscribed circle, and the angle of each point if this was a regular polygon
radius <- sqrt((2*area)/(nSides*sin((2*pi)/nSides)))
angle <- (2*pi)/nSides
# Randomize the radii/angles
radii <- rnorm(nSides, radius, radius/10)
angles <- rnorm(nSides, angle, angle/10) * 1:nSides
angles <- sort(angles)
points <- list(x=NULL, y=NULL)
points$x <- cos(angles) * radii
points$y <- sin(angles) * radii
# Find the area of the polygon
m <- matrix(unlist(points), ncol=2)
m <- rbind(m, m[1,])
current.area <- 0.5 * (sum(m[1:nSides,1]*m[2:(nSides+1),2]) - sum(m[1:nSides,2]*m[2:(nSides+1),1]))
points$x <- points$x * sqrt(area/current.area)
points$y <- points$y * sqrt(area/current.area)
return (points)
}
A random square of area 500m^2 is easy - its a square of side sqrt(500)m. Do you care about rotations? Then rotate it by runif(x,0,2*pi). Do you care about its location? Add an (x,y) offset computed from runif or whatever.
Rectangle? Given the length of any one pair of sides you only have the freedom to choose the length of the other two. How do you choose the length of the first pair of sides? Well, you might want to use runif() between some 'sensible' limits for your application. You could use rnorm() but that might give you negative lengths, so maybe rnorm-squared. Then once you've got that side, the other side length is 500/L. Rotate, translate, and add salt and pepper to taste.
For triangles, the area formula is half-base-times-height. So generate a base length - again, runif, rnorm etc etc - then choose another point giving the required height. Rotate, etc.
Summarily, a shape has a number of "degrees of freedom", and constraining the area to be fixed will limit at least one of those freedoms[1], so if you start building a shape with random numbers you'll come to a point where you have to put in a computed value.
[1] exactly one? I'm not sure - these aren't degrees of freedom in the statistical sense...
I would suggest coding a random walk of adjacent tiny squares, so that the aggregation of the tiny squares could be of arbitrary shape with known area.
http://en.wikipedia.org/wiki/File:Random_walk_in2D.png
It would be very tough to make a generic method.
But you could code up example for 3, 4, 5 sided objects.
Here is an example of a random triangle.(in C#)
class Triangle
{
double Angle1;
double Angle2;
//double angle3; 180 - angle1 - angle2;
double Base;
}
Triangle randomTriangle(double area){
//A = (base*hieght)/2.0;
double angle1 = *random number < 180*;
double angle2 = *random number < (180 - angle1)*;
*use trig to get height in terms of angles and base*
double base = (area*2.0)/height;
return new Triangle(){Angle1 = angle1, Angle2 = angle2, Base = base};
}

Knowing two points of a rectangle, how can I figure out the other two?

Hey there guys, I'm learning processing.js, and I've come across a mathematical problem, which I can't seem to solve with my limited geometry and trigonometry knowledge or by help of Wikipedia.
I need to draw a rectangle. To draw this rectangle, I need to know the coordinate points of each corner. All I know is x and y for the midpoints of the top and bottom of the box, and the length of all four sides.
There is no guarantee on the orientation of the box.
Any help? This seems like it should be easy, but it is really stumping me.
If this quadrilateral is a rectangle (all four angles are 90 degrees), then it can be solved. (if it could be any quadrilateral, then it is not solvable)
if the points are (x1,y1), and (x2, y2), and if the two points are not perfectly vertical (x1 = x2) or horizontal (y1 = y2), then the slope of one edge of the rectangle is
m1 = (y2-y1) / (x2-x1)
and the slope of the other edge is:
m2 = - 1 / m1
If you know the lengths of the sides, and the midpoints of two opposite sides, then the corrner points are easily determined by adding dx, dy to the midpoints: (if L is length of the sides that the midpoints are on)
dx = Sqrt( L^2 / (1 + m2^2) ) / 2
and
dy = m2 * dx
NOTE: if the points are vertically or horizontally aligned, this technique will not work, although the obvious solution for those degenerative cases is much simpler.
If you know your quadrilateral is a rectangle, then you can use some simple vector maths to find the coordinates of the corners. The knowns are:
(x1,y1) - the coordinate of the midpoint on the top line
(x2,y2) - the coordinate of the midpoint on the bottom line
l1 - the length of the top and bottom lines
l2 - the length of the other two lines
First, we find the vector between the two known points. This vector is parallel to the side lines:
(vx, vy) = (x2 - x1, y2 - y1)
We need to normalize this vector (i.e. make it length 1) so we can use it later as a basis to find our coordinates.
vlen = sqrt(vx*vx + vy*vy)
(v1x, v1y) = (vx / vlen, vy / vlen)
Next, we rotate this vector anticlockwise by 90 degrees. The rotated vector will be parallel to the top and bottom lines. 90 degree rotation turns out to just be swapping the coordinates and negating one of them. You can see this just by trying it out on paper. Or take at look at the equations for 2D rotations and substitute in 90 degrees.
(u1x, u1y) = (-v1y, v1x)
Now we have enough information to find the 'top-left' corner. We simply start at our point (x1, y1) and move back along that side by half the side length:
(p1x, p1y) = (x1 - u1x * l1 / 2, y1 - u1y * l1 / 2)
From here we can find the remaining points just by adding the appropriate multiples of our basis vectors. When implementing this you can obviously speed it up by only calculating each unique multiplication a single time:
(p2x, p2y) = (p1x + u1x * l1, p1y + u1y * l1)
(p3x, p3y) = (p1x + v1x * l2, p1y + v1y * l2)
(p4x, p4y) = (p3x + u1x * l1, p3y + u1y * l1)
function getFirstPoint(x1,y1,x2,y2,l1,l2)
distanceV = {x2 - x1, y2 - y1}
vlen = math.sqrt(distanceV[1]^2 + distanceV[2]^2)
normalized = {distanceV[1] / vlen, distanceV[2] / vlen}
rotated = {-normalized[2], normalized[1]}
p1 = {x1 - rotated[1] * l1 / 2, y1 - rotated[2] * l1 / 2}
p2 = {p1[1] + rotated[1] * l1, p1[2] + rotated[2] * l1}
p3 = {p1[1] + normalized[1] * l2, p1[2] + normalized[2] * l2}
p4 = {p3[1] + rotated[1] * l1, p3[2] + rotated[2] * l1}
points = { p1 , p2 , p3 , p4}
return p1
end
It's definitely a rectangle? Then you know the orientation of the short sides (they're parallel to the line between your points), and hence the orientation of the long sides.
You know the orientation and length of the long sides, and you know their midpoints, so it's straightforward to find the corners from there.
Implementation is left as an exercise to the reader.
This means that there will be two lines parallel to the line between the two points you have. Get the corners by translating the line you have 1/2 the length of the top side in each direction perpendicular to the line you have.
If you know the midpoint for the top, and the length of the top, then you know that the y will stay the same for both top corners, and the x will be the midpoint plus/minus the width of the rectangle. This will also be true for the bottom.
Once you have the four corners, there is no need to worry about the side lengths, as their points are the same as those used for the top and bottom.
midpoint
x,10 10,10 x,10
*--------------------------------------------*
width = 30
mx = midpoint x.
top left corner = (w/2) - mx or 15 - 10
top left corner coords = -5,10
mx = midpoint x.
top right corner = (w/2) + mx or 15 + 10
top left corner coords = 25,10
There's a difference between a "quadrilateral" and a "rectangle".
If you have the midpoint of the top and bottom, and the sides lengths, the rest is simple.
Given:
(x1, y1) -- (top_middle_x, top_middle_y) -- (x2, y1)
(x1, y2) -- (btm_middle_x, btm_middle_y) -- (x2, y2)
and top/bottom length along with right/left length.
x1 = top_middle_x - top/bottom_length / 2;
x2 = x1 + top/bottom_length;
y1 = top_middle_y
y2 = bottom_middle_y
Obviously, that's the simplest case and assuming that the line of (tmx, tmy) (bmx, bmy) is solely along the Y axis.
We'll call that line the "mid line".
The next trick is to take the mid line, and calculate it's rotational offset off the Y axis.
Now, my trig is super rusty.
dx = tmx - bmx, dy = tmy - bmy.
So, the tangent of the angle is dy / dx. The arctangent(dy / dx) is the angle of the line.
From that you can get your orientation.
(mind, there's some games with quadrants, and signs, and stuff to get this right -- but this is the gist of it.)
Once you have the orientation, you can "rotate" the line back to the Y axis. Look up 2D graphics for the math, it's straight forward.
That gets you your normal orientation. Then calculate the rectangles points, in this new normal form, and finally, rotate them back.
Viola. Rectangle.
Other things you can do is "rotate" a line that's half the length of the "top" line to where it's 90 deg of the mid line. So, say you have a mid line that's 45 degrees. You would start this line at tmx, tmy, and rotate this line 135 degrees (90 + 45). That point would be your "top left" corner. Rotate it -45 (45 - 90) to get the "top right" point. Then do something similar with the lower points.
Calculate the angle of the line joining the two midpoints using an arc-tangent function applied to the vector you get between them.
Subtract 90 degrees from that angle to get the direction of the top edge
Starting from the top-center point, move relative (1/2 top width x sin(angle), 1/2 top width x cos(angle)) - that gets the top right corner point.
Continue around the rectangle using the sin and cos of the angles and widths as appropriate
As a test: Check you made it back to the starting point
/* rcx = center x rectangle, rcy = center y rectangle, rw = width rectangle, rh = height rectangle, rr = rotation in radian from the rectangle (around it's center point) */
function toRectObjectFromCenter(rcx, rcy, rw, rh, rr){
var a = {
x: rcx+(Math.sin((rr-degToRad(90))+Math.asin(rh/(Math.sqrt(rh*rh+rw*rw)))) * (Math.sqrt(rh*rh+rw*rw)/2)),
y: rcy-(Math.cos((rr-degToRad(90))+Math.asin(rh/(Math.sqrt(rh*rh+rw*rw)))) * (Math.sqrt(rh*rh+rw*rw)/2))
};
var b = {
x: a.x+Math.cos(rr)*rw,
y: a.y+Math.sin(rr)*rw
};
var c = {
x: b.x+Math.cos(degToRad(radToDeg(rr)+90))*rh,
y: b.y+Math.sin(degToRad(radToDeg(rr)+90))*rh
};
var d = {
x: a.x+Math.cos(degToRad(radToDeg(rr)+90))*rh,
y: a.y+Math.sin(degToRad(radToDeg(rr)+90))*rh
};
return {a:a,b:b,c:c,d:d};
}

Resources