I have a point, let's say p(0.0, 0.0, 20.0) which I want to rotate about point a(0.0, 0.0, 10.0) in XZ plane. What is the simplest way to do it? I am using Qt with QVector3D and QMatrix4x4 to perform transformations. Everything I can think of is something like that:
QVector3D p(0.0, 0.0, 20.0);
QVector3D a(0.0, 0.0, 10.0);
QMatrix4x4 m;
m.translate(-a.x(), -a.y(), -a.z());
p = m*p;
m.setToIdentity();
m.rotate(180, 0.0, 1.0, 0.0);
p = m*p;
m.setToIdentity();
m.translate(a.x(), a.y(), a.z());
p = m*p;
But it seems conspiciously complex to me and I wonder if there are any simpler or more elegant solutions?
A rotation is a transformation in a plane that turns every point of a preimage through a specified angle and direction about a fixed point. The fixed point is called the center of rotation . The amount of rotation is called the angle of rotation and it is measured in degrees.
You can simplify the code by using simple vector subtraction/addition instead of the multiplication with a translation matrix:
QVector3D p(0.0, 0.0, 20.0);
QVector3D a(0.0, 0.0, 10.0);
QMatrix4x4 m;
p-=a;
m.rotate(180, 0.0, 1.0, 0.0);
// 3D vector has no overload for multiplying with a 4x4 matrix directly
p = m*p;
p+=a;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With