Calculating 3D rotation angle to set one coordinate to 0 - math

I am trying to solve a problem, but I am not sure how, or if there is even a solution.
I have a vector in 3D space with coordinates x, y, z. I want to rotate this vector around the z-axis such that the y coordinate becomes 0. I know from trigonometry the following for rotation around the z-axis:
|cos θ −sin θ 0| |x| |x cos θ − y sin θ| |x'|
|sin θ cos θ 0| |y| = |x sin θ + y cos θ| = |y'|
| 0 0 1| |z| | z | |z'|
For my problem I know that z' = z (rotation around an axis does not change that axis' coordinate). I want find a θ for y' = 0.
I got as far as this:
y' = x sin θ + y cos θ = 0
x sin θ = y cos θ
sin θ / cos θ = y / x
My math is rusty and I don't know how to go from here or if this is even possible. Is this solvable, and if so, how?

You can find θ angle using arctangent function (perhaps math.atan2 in your language)
θ = arctan2(y, x)

Related

3d rotation about arbitrary axis from 3D math primer for graphics

The steps are for rotating the vetor v around the vector n by an angle theta so that v lands at v', can someone please elaborate how he got the perpendicular component for v', thank you
If we were doing a rotation in 2d you have a rotation matrix
R = ( cos theta -sin theta )
( sin theta cos theta )
And the rotation of a vector v is R v. Using i, j for our two basis vectors the result of rotating x i + y j is
(x cos theta - y sin theta) i + (x sin theta + y sin theta) j
in particular the rotation of a vector along the i axis x i is
(x cos theta) i + (x sin theta) j
Now in the 3D case you can consider this as basically a rotation in the plane spanned by v_perp and w. Let i be a unit length vector in the v_perp direction and j be a unit length vector in the w direction. Let x be the length of v_perp, so v_perp = x i. The rotation of this is
(x cos theta) i + (x sin theta) j
= cos theta v_perp + sin theta w
As w has been set to be a vector of length x. This is the formula given.

Converting 2D projection rotation angles to 3D object

I am projecting a 3D matrix of density values into 3 2D planes (ZX,ZY,XY). I then rotate each projection by 3 different angles: Pzx, Pzy, Pxy using the rotation matrix below:
How do I convert these 3 separate angles so that I can apply them to a 3D transformation matrix which will rotate the 3D object about X,Y,Z (or Z,Y,X) such as the rotation matrix below:
To be clear, I do not wish to apply angles Pzx, Pzy, Pxy to the 3D object, but instead calculate what those individual rotations in 2D would translate to in 3D.
This problem yields a system of equations. Let R_3d be the rotation in 3d space, R_xy the rotation in the xy plane, and [*]_xy be the projection of * onto the xy plane. Then for any point v:
I: [R_3d v]_zx = R_zx [v]_zx
II: [R_3d v]_zy = R_zy [v]_zy
III: [R_3d v]_xy = R_xy [v]_xy
We see that every coordinate is present in two equations. Let's check the relevant equations for the x-coordinate:
a := alpha, b := beta, c := gamma
I: cos b cos c x - cos b sin c y + sin b z = sin Pzx z + cos Pzx x
III: cos b cos c x - cos b sin c y + sin b z = cos Pxy x - sin Pxy y
We see that the following relation mus hold for any v (right hand side of both equations):
sin Pzx z + cos Pzx x = cos Pxy x - sin Pxy y
Similar equations exist for the other two coordinates. Only if these conditions are met, an exact 3d rotation can exist. If I'm not mistaken, that's only the case if Pzx=Pzy=Pxy=0. In general, an approximate solution can be calculated. I would suggest a least-squares solution based on the following energy:
E(a, b, c) = Sum { for all v in data set } ( || [R_3d v]_zx - R_zx [v]_zx ||^2
+ || [R_3d v]_zy - R_zy [v]_zy ||^2
+ || [R_3d v]_xy - R_xy [v]_xy ||^2 )
And the optimal rotation parameters are:
{a, b, c}* = arg min {a, b, c} E(a,b,c)
This solution will minimize the distance of the two projections of corresponding points.
Unfortunately, the problem is not a linear least-squares problem which would be easy to solve. Instead, iterative methods can solve this problem (e.g. Levenberg–Marquardt). Look for an implementation of that algorithm in your programming language, plug in the energy and solve for the optimal rotation parameters.

Finding a line making an angle θ with a known line

I have a line from (a, b) to (x, y), and I would like to draw a line starting at (x, y), with length ℓ, that makes an angle of θ with the original line.
How do I compute the coordinates of the endpoint of this new line? See the diagram:
It's nearly always simpler to use vector algebra for this kind of thing, rather than Cartesian coordinates. Let's start by labelling the points:
Let R(θ) be the matrix that rotates by θ radians counter-clockwise:
Then compute:
v = B − A (the vector from A to B)
v̂ = v / |v| (the unit vector in the direction of v)
ŵ = R(−θ) v̂ (the unit vector in the direction of BC; your rotation is clockwise, so we need R(−θ) here, not R(θ))
w = ℓ ŵ (the vector of length ℓ in the direction of BC)
C = B + w
This approach avoids the need to compute an arctangent, which would need some care (if done naïvely, it runs into trouble when B is vertically above or below A; but most languages have a function like atan2 for handling this case).
In any sensible programming language with a vector library you should be able to write this as a one-liner, perhaps like this:
C = B + (B - A).unit().rotate(-theta) * l
OK, so after a lot of scribbling, I came up with this:
The dashed lines represent lines parallel to the x- and y-axes.
m = x − a
n = y − b
α = tan−1 (n / m)
β = α − θ
p = ℓ cos β
q = ℓ sin β
c = x + p
d = y + q

How to calculate a vector from an angle with another vector in 2D?

I know I should know this, but I just can't figure it out/find the solution. I can do it by hand, but I can't put it in an algorithm... (working in c++, but pseudocode should be fine).
I have a vector, and I want to find another vector based on the angle with it.
v is known, angle alpha is known and the magnitude of w is known. How can I find w?
Thanks!
To rotate a vector v = (x, y) by an angle alpha clockwise about the origin, you can multiply by the matrix:
[ cos alpha sin alpha ]
[ -sin alpha cos alpha ]
Thus the rotated vector with the same magnitude will be
(x cos alpha + y sin alpha, -x sin alpha + y cos alpha).
To change the magnitude from |v| to |w|, multiply both co-ordinates by |w|/|v|.
vector(w) = vector(v) / cos (alpha) to find the direction of w.
You must multiply by magnitude(w)/magnitude(v) to set the magnitude

Plotting a point on the edge of a sphere

So coming from a flash background I have an OK understanding of some simple 2D trig. In 2d with I circle, I know the math to place an item on the edge given an angle and a radius using.
x = cos(a) * r;
y = sin(a) * r;
Now if i have a point in 3d space, i know the radius of my sphere, i know the angle i want to position it around the z axis and the angle i want to position it around, say, the y axis. What is the math to find the x, y and z coordinates in my 3d space (assume that my origin is 0,0,0)? I would think i could borrow the Math from the circle trig but i can't seem to find a solution.
Your position in 3d is given by two angles (+ radius, which in your case is constant)
x = r * cos(s) * sin(t)
y = r * sin(s) * sin(t)
z = r * cos(t)
here, s is the angle around the z-axis, and t is the height angle, measured 'down' from the z-axis.
The picture below shows what the angles represent, s=theta in the range 0 to 2*PI in the xy-plane, and t=phi in the range 0 to PI.
The accepted answer did not seem to support negative x values (possibly I did something wrong), but just in case, using notation from ISO convention on coordinate systems defined in this Wikipedia entry, this system of equations should work:
import math
x = radius * sin(theta) * cos(phi)
y = radius * sin(theta) * sin(phi)
z = radius * cos(theta)
radius = math.sqrt(math.pow(x, 2) + math.pow(y, 2) + math.pow(z, 2))
phi = math.atan2(y, x)
theta = math.acos((z / radius))

Resources