views:

60

answers:

1

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?

+1  A: 

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);
p*=m;
p+=a;
Greg S