Finding points of intersection when two spheres intersect - math

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 )

Related

Scaling the X axis of a rotated ellipse

I have a scatter plot and I'm allowing points to be selected by drawing an ellipse like so:
In the above image the X axis and Y axis are scaled differently. I'm trying to implement functionality to allow the scale of each axis to be changed, but I'm having trouble scaling the ellipse shape with the axis. Changing the scale of the X axis to match the Y axis gives this:
I know I can scale the cx value of the ellipse with the X axis to get the new centre of the ellipse, but I don't know how to work out the new rx, ry and rotation angle. So my question is given a rotated ellipse with values cx, cy, rx, ry and rotation in radians, how do I calculate the new values for the ellipse given a scale value s for either the X axis or Y Axis? For clarity the rotation is done about the centre (cx, cy) of the ellipse.
I think I understand your problem. For an axis-aligned ellipse with points (x,y), the shape of (a*x,b*y) is still an ellipse with some arbitrary scaling factors a and b acting on the major and minor radii.
But for a rotated ellipse this is not true. In addition to the major and minor radii scaling differently, the rotation angle might differ also.
So mathematically the problem is as such: The parametric points of a rotated ellipse are
with θ the rotation angle r_1 and r_2 the original ellipse radii. The parameter t goes from 0 to 2π.
Now to find the modified parameters such that the ellipse matches a scaled version of the above
The above two are combined into one equation to be solved for r_1' and r_2' as well as θ' for all values of t.
Unfortunately, there is no solution that I can think of, because the combined 2×2 matrix on the RHS in front of the vector needs to be diagonalized by finding the appropriate rotation θ' to bring it down to this form
which is trivially solved for r_1' and r_2'.
But the rotation needs to satisfy two contradictory equations
tan(θ') = (a/b)*tan(θ) and tan(θ') = (b/a)*tan(θ)
which can only be solved if the two scaling factors are identical => a==b.
I suggest posting this problem into the [Mathematics.SE] as math problem, before trying to implement it as an algorithm. Maybe you will have better luck.
The computation is not that easy.
The equation of a rotated ellipse centered at the origin is
(c x + s y)² / a² + (s x - c y)² / b² = 1 = A x² + 2B xy + C y²
(c, s denote the cosine and sine of the rotation angle.) After development, the quadratic coefficients are
A = c² / a² + s² / b², 2B = 2 cs / a² - 2 sc / b², C = s² / a² + c² / b².
When you have the coefficients, you can retrieve 1/a², 1/b², c, s as the Eigenvalues and first Eigen vector of the 2x2 matrix
|A B|
|B C|
and the rotation angle is given by tan Θ = s / c.
Now let us stretch the abscissas by applying a coefficient r, giving
A' = r² A, B' = r B, C' = C.
The matrix becomes
|r²A rB|
|rB C|
and again, you will find the axis and cosine/sine of the angle by computing the Eigenvalues and first Eigenvector.
The Eigenvalues are roots of the polynomial
z² + (r²A + C) z + r²(AC - B²)
where A, B, C are computed as above, and the Eigenvector follows
(r²A - z) c + rB s = 0
from which you draw s/c.

Get plane from 3D polygon and normal

I have a polygon defined by n points and a polygon normal.
Now I want to get the plane of the polygon defined by
a plane normal=(nx,ny,nz)
and a constant d (distance from the origin to the plane).
The plane normal is equal to the polygon normal, but how can I calculate d?
desired plane equation nx*x+ny*y+nz*z+d=0.0
Take any point p=(px, py, pz) on the plane and plug it into the equation to obtain d.
So if your equation is
nx·x + ny·y + nz·z + d = 0
then you get
d = − (nx·px + ny·py + nz·pz).
Another common formulation is using d as the right hand side of the equation, in which case you get the reverse sign. I.e. for the equation
nx·x + ny·y + nz·z = d
you get
d = nx·px + ny·py + nz·pz.

Calculating intersection point of two tangents on one circle?

I tried using a raycasting-style function to do it but can't get any maintainable results. I'm trying to calculate the intersection between two tangents on one circle. This picture should help explain:
I've googled + searched stackoverflow about this problem but can't find anything similar to this problem. Any help?
Well, if your variables are:
C = (cx, cy) - Circle center
A = (x1, y1) - Tangent point 1
B = (x2, y2) - Tangent point 2
The lines from the circle center to the two points A and B are CA = A - C and CB = B - C respectively.
You know that a tangent is perpendicular to the line from the center. In 2D, to get a line perpendicular to a vector (x, y) you just take (y, -x) (or (-y, x))
So your two (parametric) tangent lines are:
L1(u) = A + u * (CA.y, -CA.x)
= (A.x + u * CA.y, A.y - u * CA.x)
L2(v) = B + v * (CB.y, -CB.x)
= (B.x + v * CB.y, B.x - v * CB.x)
Then to calculate the intersection of two lines you just need to use standard intersection tests.
The answer by Peter Alexander assumes that you know the center of the circle, which is not obvious from your figure http://oi54.tinypic.com/e6y62f.jpg.
Here is a solution without knowing the center:
The point C (in your figure) is the intersection of the tangent at A(x, y) with the line L perpendicular to AB, cutting AB into halves. A parametric equation for the line L can be derived as follows:
The middle point of AB is M = ((x+x2)/2, (y+y2)/2), where B(x2, y2). The vector perpendicular to AB is N = (y2-y, x-x2). The vector equation of the line L is hence
L(t) = M + t N, where t is a real number.

Finding the distance between two circles

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.

Hexagonal Grid Coordinates To Pixel Coordinates

I am working with a hexagonal grid. I have chosen to use this coordinate system because it is quite elegant.
This question talks about generating the coordinates themselves, and is quite useful. My issue now is in converting these coordinates to and from actual pixel coordinates. I am looking for a simple way to find the center of a hexagon with coordinates x,y,z. Assume (0,0) in pixel coordinates is at (0,0,0) in hex coords, and that each hexagon has an edge of length s. It seems to me like x,y, and z should each move my coordinate a certain distance along an axis, but they are interrelated in an odd way I can't quite wrap my head around it.
Bonus points if you can go the other direction and convert any (x,y) point in pixel coordinates to the hex that point belongs in.
For clarity, let the "hexagonal" coordinates be (r,g,b) where r, g, and b are the red, green, and blue coordinates, respectively. The coordinates (r,g,b) and (x,y) are related by the following:
y = 3/2 * s * b
b = 2/3 * y / s
x = sqrt(3) * s * ( b/2 + r)
x = - sqrt(3) * s * ( b/2 + g )
r = (sqrt(3)/3 * x - y/3 ) / s
g = -(sqrt(3)/3 * x + y/3 ) / s
r + b + g = 0
Derivation:
I first noticed that any horizontal row of hexagons (which should have a constant y-coordinate) had a constant b coordinate, so y depended only on b. Each hexagon can be broken into six equilateral triangles with sides of length s; the centers of the hexagons in one row are one and a half side-lengths above/below the centers in the next row (or, perhaps easier to see, the centers in one row are 3 side lengths above/below the centers two rows away), so for each change of 1 in b, y changes 3/2 * s, giving the first formula. Solving for b in terms of y gives the second formula.
The hexagons with a given r coordinate all have centers on a line perpendicular to the r axis at the point on the r axis that is 3/2 * s from the origin (similar to the above derivation of y in terms of b). The r axis has slope -sqrt(3)/3, so a line perpendicular to it has slope sqrt(3); the point on the r axis and on the line has coordinates (3sqrt(3)/4 * s * r, -3/4 * s * r); so an equation in x and y for the line containing the centers of the hexagons with r-coordinate r is y + 3/4 * s * r = sqrt(3) * (x - 3sqrt(3)/4 * s * r). Substituting for y using the first formula and solving for x gives the second formula. (This is not how I actually derived this one, but my derivation was graphical with lots of trial and error and this algebraic method is more concise.)
The set of hexagons with a given r coordinate is the horizontal reflection of the set of hexagons with that g coordinate, so whatever the formula is for the x coordinate in terms of r and b, the x coordinate for that formula with g in place of r will be the opposite. This gives the third formula.
The fourth and fifth formulas come from substituting the second formula for b and solving for r or g in terms of x and y.
The final formula came from observation, verified by algebra with the earlier formulas.

Resources