Hi.
The problem is not in your code, or rather, the problems in your code are inherited from the problems in your math.
Lets look at what happens:
double y[] = { 10, 0, pi/4 };double x[] = { 10, y[1] + pi/2, y[2] + pi }
These are indeed perpendicular, and all is good. Now lets tilt y by pi/4:
y[1] += pi/4;x[1] = y[1] + pi/2;x[2] = y[2] + pi;
Now:
y = { 10, pi/4, pi/4 }x = { 10, 3pi/4, 5pi/4 }
Lets convert them to cartesian:
y = { 5, 5, 7.071 }x = { -5, -5, -7.071 }
So we see the problem. In fact x == -y (this is coincidental by the way), which is not what we wanted.
The problem arises from the use of spherical coordinates. When you tilt y, x also tilts correctly. However when you rotate x, it rotates around the z-axis, and not around the y-vector, which is what you want, I believe.
As for how to fix this, I can't tell you. From what little I know about rotations in 3d I think you need to look in to Quaternions or Rotation Matrices.