# Finding a random vector exactly yay far from another point in 3D space

So I am trying to find a vector a certain distance away from another point ( the distance varies based on an input ) and I've figured out that

distance^2=(newPoint-centerPoint):Dot(newPoint-centerPoint)

Where newPoint is what I'm looking for and centerPoint is what I have. I don't know where to go from there to further solve for newPoint and I also know that there's many possible answers, is there a way to make it pick just one of those? Sorry if this is a fairly simple question, my highest level of math education is geometry.

• You know how to add a vector to a point, right? Then, pick a unit vector unitVector at your convenience and set (nextPoint)=(centerPoint)+(distance).(unitVector). – Did Jul 9 '11 at 7:50
• If the question is about how to generate unitVector, pick n independent standard real Gaussian gaussReal(k), make them into a vector gaussVector=[gaussReal(k),k=1..n], compute the real number gaussLength defined by gaussLength^2=sum[gaussReal(k)^2,k=1..n] and set unitVector=(gaussLength)^{-1}.gaussVector. – Did Jul 9 '11 at 8:13
• @Didier Piau thanks that worked! I know how to get the unit vector, but I truly have no idea what they are. – SDuke Jul 10 '11 at 0:58

Your problem can be reduced to the well known problem of picking a uniformly distributed point on a unit sphere. There are various methods for solving this problem, see e.g. www.cgafaq.info or Mathworld. A popular method to get $(x, y, z)$ uniformly distributed on the unit sphere is the following:

1. Choose $z$ uniformly distributed in [−1, 1].
2. Choose $\theta$ uniformly distributed on [0, 2 π).
3. Define $r = \sqrt{1−z^2}$.
4. Compute $x = r \cos \theta$.
5. Compute $y = r \sin \theta$

For derivation see the above references. Note that you cannot use the spherical coordinates straightforwardly because they do not establish equally sized surface elements on the sphere.

As soon as you have the above $(x, y, z)$, you can scale it with the required distance $D$ and add it componentwise to your center point:

$$(x_{new}, y_{new}, z_{new}) = (x_{center} + D\;x, y_{center} + D\;y, z_{center} + D\;z)$$

Your random point is located on a sphere centered on your original point, and whose radius $r$ is the given distance between your two points. So your problem is equivalent to choosing a random point on a sphere. You might want to try and use spherical coordinates to reformulate your problem.

There are several ways to define spherical coordinates, but they always involve a radius $r$ and two angles $\phi$ and $\theta$. In your problem, the radius $r$ is given, and the two angles $\phi$ and $\theta$ must be chosen in a random way.

The simplest way to think of the angles if you're not familiar with spherical coodinates is the way they are used as similar to the coordinates on the surface of the Earth: latitude, and longitude. Latitude is an angle betwen $-90^o$ and $+90^o$, $0$ being the equator, +/- an arbitrary distinction between the two hemispheres (you can choose which is positive), and $\pm90^o$ the latitudes of the poles. A second angle, longitude, between $0$ and $360^o$, will then tell you where you are on the circle of points of same latitude, starting at an arbitrary meridian.

To visualize spherical coordinates, you can also look at a simulation.

Note when investigating spherical coordinates more, you'll most often see angles defined in radian measure rather than degree measure, in which case you should replace $90^o$ and $360^o$ by respectively $\frac\pi2$ and $2\pi$.