Help!

I've seen some things...

like

``````double AngleBetweenVector(Vector3 A, Vector3 B)
{
return Math.Acos(Vector3.DotProduct(A, B) /
Math.Sqrt(Vector3.DotProduct(A, A) * Vector3.DotProduct(B, B)));
}``````

but...this only returns one angle...

In 3d space, vectors are composed of 3 vertices... to get to another vector, you need two angles to offset the vector on two of the axises. One angle is not enough.

Such is the make up of the calculation to find the point on the surface of a sphere:

``````public static Vector3 GetPointOnSphere(Vector3 sphereOrigin, double radius, double theta, double psi)
{
Vector3 results = new Vector3();

if ((psi >= 0 && psi <= (2 * Math.PI)) && (theta >= 0 && theta <= Math.PI))
{
// x = x0 + r * Sin theta * cos psi
// y = y0 + r * sin theta * sin psi
// z = z0 + r * cos theta
results.X = sphereOrigin.X + radius * Math.Sin(theta) * Math.Cos(psi);
results.Y = sphereOrigin.Y + radius * Math.Sin(theta) * Math.Sin(psi);
results.X = sphereOrigin.X + radius * Math.Cos(theta);
}
return results;
}``````

It takes TWO angles.

So, thinking about two spheres, that are touching, in any orientation, in 3d space, how do I find theta and psi from the theoretical line made from the centre of sphere 1 to the centre of sphere 2, in relation to sphere 2?

Help! Thanks!

R

## Recommended Answers

Two vectors that intersect are always coplaner, so there is only one angle.

As for your two spheres, I'm not sure what you are looking for, the formula for the line described using the 2nd sphere as the origin? The vector from the center of the 2nd sphere to …

Perhaps this Vector3D structure may help you.
It has an AngleBetween method.

## All 6 Replies

Two vectors that intersect are always coplaner, so there is only one angle.

As for your two spheres, I'm not sure what you are looking for, the formula for the line described using the 2nd sphere as the origin? The vector from the center of the 2nd sphere to the center of the 1st sphere? It sounds like you want it in polar coordinates, is this true?

Perhaps this Vector3D structure may help you.
It has an AngleBetween method.

Ok - perhaps my terminology is really bad - not suprizing; I'm not studying math...sorry.

My scenario:

Set sphere 2 center at origin of 0,0,0, with the Y axis down the center of the sphere.

Sphere 1 is touching sphere 2, somewhere in 3 dimensions, so it's centre is at some [x,y,z] where it can satisfy that its radius added to sphere 2's radius is equal to the new line that you could draw from the center of sphere 1 to the center of sphere 2.

Now, I need to calculate the two angles required to describe this line, that we know the length of, and the start and end points of, so I can map it properly in an x,y,z graph.

One angle is not enough to do this. I need an angle from X axis, and an angle from Z axis...

I need this in a spherical coordinate system: I need elevation angle and azimuth angle (zenith is the Y axiz).

I need this in a spherical coordinate system

Ok, if you know the [x,y,z] of the end point of your line segment, the values are:

p = Sqrt(x^2 + y^2 + z^2) -- length of the segment
o = arctan(y/x) -- this is the angle from the x axis and goes from 0 to 360 degrees
t = arctan(Sqrt(x^2 + y^2) / z) -- this is the angle from the z axis and goes from -90 to 90 degrees

Thanks...hmm

I was looking up Spherical coordinates in Wikipedea to get :
r = length or vector magnitude
theta ( θ ) = arccos ( z / r)
psi ( φ ) = arctan ( y / x )

and http://www.mathonweb.com/help_ebook/html/algorithms.htm
arccos(x) = arctan ( Sqr( 1 - x^2) / x )
and
arctan(x) = x - (x^3 / 3) + (x^5 / 5)

and this is what I came up with...does it jive with yours???

``````public static double arctan(double x)
{
// [url]http://www.mathonweb.com/help_ebook/html/algorithms.htm[/url]
// arctan(x) = x - (x^3 / 3) + (x^5 / 5)
return x - (Math.Pow(x, 3) / 3) + (Math.Pow(x, 5) / 5);
}

public static double GetElevationOrTheta(Vector3 A)
{
// These formulas assume that the two systems have the same origin,
// that the spherical reference plane is the Cartesian x−y plane,
// that θ is inclination from the z direction, and that the azimuth
// angles are measured from the Cartesian x axis (so that the y axis has φ=+90°).
// If θ measures elevation from the reference plane instead of inclination from
// the zenith the arccos above becomes an arcsin, and the cos θ and sin θ below become switched.

// theta ( θ ) = cos-1 ( z / r) or arccos ( z/r)
// psi  ( φ ) = tan-1 ( y / x ) or arctan (y/x)

// [url]http://www.mathonweb.com/help_ebook/html/algorithms.htm[/url]
// arccos(x) = arctan ( Sqr( 1 - x^2) / x )

double X = A.Z / A.Magnitude;

return arctan(Math.Sqrt(1 - Math.Pow(X, 2) / X));
}
public static double GetAzimuthOrPsi(Vector3 A)
{
// These formulas assume that the two systems have the same origin,
// that the spherical reference plane is the Cartesian x−y plane,
// that θ is inclination from the z direction, and that the azimuth
// angles are measured from the Cartesian x axis (so that the y axis has φ=+90°).
// If θ measures elevation from the reference plane instead of inclination from
// the zenith the arccos above becomes an arcsin, and the cos θ and sin θ below become switched.

// theta ( θ ) = cos-1 ( z / r) or arccos ( z/r)
// psi  ( φ ) = tan-1 ( y / x ) or arctan (y/x)

//double r = A.Magnitude;
double X = A.Y / A.X;
return arctan(X);
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.