Geometry: What is being calculated here?

141 Views Asked by At

Context: I am a computer graphics programmer looking at a code-implementation. I need help understanding a function that has neither been documented properly nor commented.

Given a circle with circumference $c$ in a Cartesian coordinate space. Now each point on this circle is defined by Cartesian coordinates $(x, y)$.

I need some explanations as to what the following calculations achieve, please excuse the improper use of symbols:


$$ \begin{align} nX = \frac {x}{c}\\ nY = \frac {y}{c} \end{align} $$

I believe $n$ stands for 'normal' or vector. then...


$$ \begin{align} rdX = nX \cdot 2\pi\\ rdY = nY \cdot \pi \end{align} $$

I assume 'rd' stands for radians. What really puzzles me here is why nX is multiplied with $2\pi$, while $nY$ is only multiplied by $\pi$.


$$ \begin{align} \sin Y = \sin(rdY + \pi) \end{align} $$

At this point I'm completely lost...


now finally: $$ \begin{align} a = 2\pi\cdot\sin(rdX)\cdot\sin Y\\ b = 2\pi\cdot\cos(rdX)\cdot\sin Y\\ d = 2\pi\cdot\cos(rdy) \end{align} $$


Very simple question; What is being calculated here? What do a,b and d represent? At first i figured that this was a conversion from Cartesian coordinates to spherical coordinates. But given a closer look, this is not at all how I would calculate them. What am I looking at here?


EDIT:

I will include the source of these calculations to clarify their context. They are part of a library that provides functions for working with Simplex Noise.

They appear in functions that sample a generated noise field. This noise field can be sampled in n-dimensions. This sampling means that I provide a set of parameters (usually coordinates) to the noise functions, which return a noise value, eg:

var noiseValue = simplexNoise.get2DNoise( x, y )
var noiseValue = simplexNoise.get3DNoise( x, y, z )
var noiseValue = simplexNoise.get4DNoise( x, y, z, w )

An example:

If I generate a grid of points in a plane dimension (two dimensions), and then sample noise values of those points using their coordinates:

z = simplexNoise.get2DNoise( x, y )

Then now I suddenly have a third dimension. This to say i started with this, and ended up sampling my z values to result in this. The noise function assures me that I do not have completely random values.


Now however, I want to sample noise on a spherical surface. And I encounter these two functions:

FastSimplexNoise.prototype.getSpherical2DNoise = function (c, x, y) {
  var nx = x / c;
  var ny = y / c;
  var rdx = nx * 2 * Math.PI;
  var rdy = ny * Math.PI;
  var sinY = Math.sin(rdy + Math.PI);
  var sinRds = 2 * Math.PI;
  var a = sinRds * Math.sin(rdx) * sinY;
  var b = sinRds * Math.cos(rdx) * sinY;
  var d = sinRds * Math.cos(rdy);

  return this.get3DNoise(a, b, d);
};

FastSimplexNoise.prototype.getSpherical3DNoise = function (c, x, y, z) {
  var nx = x / c;
  var ny = y / c;
  var rdx = nx * 2 * Math.PI;
  var rdy = ny * Math.PI;
  var sinY = Math.sin(rdy + Math.PI);
  var sinRds = 2 * Math.PI;
  var a = sinRds * Math.sin(rdx) * sinY;
  var b = sinRds * Math.cos(rdx) * sinY;
  var d = sinRds * Math.cos(rdy);

  return this.get4DNoise(a, b, d, z);
};

Note in particular that getSpherical3DNoise(c, x, y) ends up sampling the three-dimensional pointvector $(a, b, d)$, given only an $( x,y )$ coordinate and circumference $c$

The second function, getSpherical3DNoise(c, x, y, z) seems like an incomprehensible follow-up to the previous function by sampling a four-dimensional vector $(a, b, d, z)$, $z$ being the Cartesian coordinate along the $z$-axis

These functions behave strangely to say the least. So they are either incomprehansably cleverly written. Or they warrant a rewrite.

2

There are 2 best solutions below

6
On BEST ANSWER

Note that $\sin Y = -\sin rdY$, so $Y$ is redundant. Write down your formulae in clear, eliminating all the intermediate variables:

$$\begin{eqnarray*} a = -2 \pi \sin \frac {2 \pi x} c \sin \frac {\pi y} c \\ b = -2 \pi \cos \frac {2 \pi x} c \sin \frac {\pi y} c \\ d = 2 \pi \cos \frac {\pi y} c . \end{eqnarray*}$$

Note that $a^2 + b^2 + d^2 = 4 \pi ^2$ and that the mapping $(x,y) \mapsto (a,b,d)$ is injective. Therefore, what these formulae do is to map the square $[0,c) \times [0,c)$ (points in it being represented by $(x,y)$ and $c$ being some physical, non-mathematical parameter) onto some part of the sphere centered in $(0,0,0)$ and of radius $2 \pi$. (Note that when I say "square" I mean the "full", "solid" square, but when I say "sphere" I mean just the surface, not the solid body.)

I have deleted the previous version of my answer, it was needlessly detailed. Please note, though, that the programmer chose to work with a certain convention regarding the orientation of the axes and the measurement of the angles in $\Bbb R ^3$ that is not among the several ones commonly used in mathematics.

3
On

It is all very strange! Suppose $(x,y)=(r x',ry')$ so that $(x',y')=(\cos\theta,\sin\theta)$ is some point on the unit circle. Then we have $$ \begin{align} a&=2\pi\cdot\cos(x'-\pi/2)\cdot\cos(-y'/2-\pi/2)\\ b&=2\pi\cdot\sin(x'-\pi/2)\cdot\cos(-y'/2-\pi/2)\\ d&=2\pi\cdot\sin(-y'/2-\pi/2) \end{align} $$ This can be interpreted as a point on a $2\pi$-radius sphere in the $(a,b,d)$-plane, first rotated by an angle of $(x'-\pi/2)$ radians, then rotated away from the $(a,b)$-plane into the third dimension by an angle of $(-y'/2-\pi/2$. Strange!


I tried plotting what it does - it forms an 8-shaped curve on a sphere of radius $2\pi$. I promised to add a plot, and here it is:

enter image description here

The point $A=(x',y')$ traverses the unit circle and makes the point $B=(a,b,d)$ traverse an 8-shaped curve on the $2\pi$-sphere.