# Get random position on surface

I'd like to get a random position on a surface of an object, and also follow it's normals.

Example, let's say I have a sphere, I can get all the face, normal and vertex positions and well as their normals, however, I want to randomly get a vector (x,y,z) to positon the object on the surface randomly, as well as orient this object to the facing normals of the closest component.

I'm using Maya for this, it's not necessarily going to ever be a spherical surface either, however I can gather an array of vertex/edge/face information to use.

• Sounds like you don't really have a sphere. You have a bunch of polygons that approximate a sphere. That chnages the problem, somewhat. – bubba Aug 20 '13 at 7:32

$$\int_{S}{\rm P}\left(\vec{r}\right)\,{\rm d}S = {\rm P}\left(\vec{r}\right)\int_{S}{\rm d}S = {\rm P}\left(\vec{r}\right)\,4\pi a^{2} = 1 \quad\Longrightarrow\quad {\rm P}\left(\vec{r}\right)= {1 \over 4\pi a^{2}}$$

$$1 = \int_{S}{1 \over 4\pi a^{2}}\,{\rm d}S = \int_{0}^{\pi}{1 \over 2}\,\sin\left(\theta\right)\,{\rm d}\theta \int_{0}^{2\pi}{1 \over 2\pi}\,{\rm d}\phi$$

$$\begin{array}{rcl} {1 \over 2}\,\sin\left(\theta\right)\,{\rm d}\theta & = & {\rm d}\xi_{\theta} \quad\Longrightarrow\quad \theta = 2\arcsin\left(\xi_{\theta}^{1/2}\right) \\ {1 \over 2\pi}\,{\rm d}\phi & = & {\rm d}\xi_{\phi} \quad\Longrightarrow\quad \phi = 2\pi\xi_{\phi} \end{array}$$

With $\sin\left(\theta\right) = 2\sin\left(\theta \over 2\right)\cos\left(\theta \over 2\right) = 2\sqrt{\xi_{\theta}\left(1 - \xi_{\theta}\right)}$ and $\cos\left(\theta\right) = 1 - 2\sin^{2}\left(\theta \over 2\right) = 1 - 2\xi_{\theta}$

Generate random numbers $\xi_{\theta}$ and $\xi_{\phi}$ in $\left\lbrack 0, 1\right)$. You get a random vector $x\,\hat{x} + y\,\hat{y} + z\,\hat{z}$ in the surface of a sphere of radius $a$:

$$\left\lbrace% \begin{array}{rcl} x & = & a\sin\left(\theta\right)\cos\left(\phi\right) = 2a\,\sqrt{\,\xi_{\theta}\left(1 - \xi_{\theta}\right)\,}\,\cos\left(2\pi\xi_{\phi}\right) \\[2mm] y & = & a\sin\left(\theta\right)\sin\left(\phi\right) = 2a\,\sqrt{\,\xi_{\theta}\left(1 - \xi_{\theta}\right)\,}\,\sin\left(2\pi\xi_{\phi}\right) \\[2mm] z & = & a\cos\left(\theta\right) = a\left(1 - 2\xi_{\theta}\right) \end{array}\right.$$

• Wow, this looks incredibly complex! However will this work on any shape as my description states? Not just a sphere? – Shannon Hochkins Aug 20 '13 at 23:57
• Just for the sphere. It's the idea. – Felix Marin Aug 21 '13 at 0:50
• It's very cool, however I did state that I needed it for any surface :( – Shannon Hochkins Aug 21 '13 at 0:53
• @ShannonHochkins For any surface, it's better to know it to look for symmetries, etc… – Felix Marin Sep 6 '13 at 1:46