# Rotate point around vector

This topic is 2407 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I hope I can explain what I need to do, I also attach this image, which should be helpful:

I have a point A (with coordinates A.x, A.y, A.z) and a point B (B.x, B.y, B.z).

I'd like to rotate point B around the local X axis (1,0,0) of point A.

Is this clear enough?

Thanks again for any kind of support!

EDIT: just posted a better image, the previous was a bit cryptic...

##### Share on other sites
If we knew how to rotate around the origin, we could solve your problem like this:

B'' = A + rotate_around_origin(B' - A)

So I'll just concentrate on rotating a point P around the origin, giving P' as result.

Now, how would we go about rotating around the X axis? Well, this is particularly simple because we'll just leave the x coordinate alone and do a 2D rotation on coordinates y and z

P'.x = P.x
P'.y = cos(alpha)*P.y - sin(alpha)*P.z
P'.z = sin(alpha)*P.y + cos(alpha)*P.z

If you want to rotate around a general axis, it gets trickier. You can do it using quaternions, which are not easy to understand but worth the trouble. Here's some code you can use.

##### Share on other sites
Thank you very much Alvaro, that link you posted, about another thread where you helped with a similar problem, it's very interesting.

##### Share on other sites
I tried the quaternion solution posted by Alvaro, related to point rotation around vector and it works (tried some debug values).

However, if you take a look at the attached picture (it's updated), I need to rotate point B around the X local axis of point A (or general axis, as Alvaro calls it).

What I tried to do, is to Normalize vector A, and calculate the cross product of such normalized vector with (0,1,0). That way, I get the perpendicular vector to those; however this vector is normalized, and a rotation of point B around it will be done relative to origin (0,0,0), and not point A.
So how'd I account for that?

##### Share on other sites
Read the first two lines of my original response.

EDIT: Wait, what do you mean by "the X local axis of point A"? In your diagram, that doesn't seem to be parallel to the x axis...

##### Share on other sites
Sorry for the mess: I made a new image that should explain better.

Say you have a line in 3D space made of 4 points, ABCD. You want to rotate it so that A is the origin, and more precisely you want to rotate it around the local X axis of point A (the one represented as a yellow line). And finally get A'B'C'D'.

I included both a perspective and a top view that should show better how points are disposed in 3D space...

##### Share on other sites
You are still not explaining what "the local X axis of point A" means. I asked for clarification and you simply used the exact same expression again.

##### Share on other sites
You're right, sorry. The "local X axis of point A", represented by the yellow line in the above picture, is a vector perpendicular to AB and 0,1,0 (so the cross product of those two vector). It's the axis that I'd like to use to rotate point B,C,D around.
I hope this is more clear...

##### Share on other sites
True, the quaternion solution used to define a rotation worked perfectly.
The problem subsequent to that, and that I couldn't quite explain was how to position back the rotated point (since the rotation was performed around a normalized vector). A simple vector sum did it.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633297
• Total Posts
3011248
• ### Who's Online (See full list)

There are no registered users currently online

×