# Maths - Affine Conversion - Euler/Vector To Matrix

Definition of terms:

It you want to consider the rotation only then the code for this is shown here.

Alternatively if we want to consider the possibility that there is also a translation from the centre, and that the rotation may not be about the centre but may be about some arbitrary point, then we need to extend the notation as follows:

• A 3D vector has to be added to the euler angles so that together they can specify the position and orientation.
• The matrix has to be increased from 3x3 to 4x4 so that it can specify the position and orientation. (as explained here).

For the derivation of how to rotate about a point see this page.

```void setRotate(Point3d A, // rotate about a line centred on A
double theta,
double phi,
double alpha)
{ double cosAlpha, sinAlpha, cosPhi, sinPhi,
cosTheta, sinTheta, cosPhi2, sinPhi2,
cosTheta2, sinTheta2, pi, c, a1,a2,a3;
if (A == null) {
a1=a2=a3=0;
} else {
a1 = A.x; a2 = A.y; a3 = A.z;
}
cosPhi = Math.cos(phi); sinPhi = Math.sin(phi);
cosPhi2 = cosPhi * cosPhi; sinPhi2 = sinPhi * sinPhi;
cosTheta = Math.cos(theta);
sinTheta = Math.sin(theta);
cosTheta2 = cosTheta * cosTheta;
sinTheta2 = sinTheta * sinTheta;
cosAlpha = Math.cos(alpha);
sinAlpha = Math.sin(alpha);
c = 1.0 - cosAlpha;
m00 = cosTheta2 * (cosAlpha * cosPhi2 + sinPhi2)
+ cosAlpha * sinTheta2;
m10 = sinAlpha * cosPhi + c * sinPhi2 * cosTheta * sinTheta;
m20 = sinPhi * (cosPhi * cosTheta * c - sinAlpha * sinTheta);
m01 = sinPhi2 * cosTheta * sinTheta * c - sinAlpha * cosPhi;
m11 = sinTheta2 * (cosAlpha * cosPhi2 + sinPhi2)
+ cosAlpha * cosTheta2;
m21 = sinPhi * (cosPhi * sinTheta * c + sinAlpha * cosTheta);
m02 = sinPhi * (cosPhi * cosTheta * c + sinAlpha * sinTheta);
m12 = sinPhi * (cosPhi * sinTheta * c - sinAlpha * cosTheta);
m22 = cosAlpha * sinPhi2 + cosPhi2;
m03 = a1 - a1 * m00 - a2 * m01 - a3 * m02;
m13 = a2 - a1 * m10 - a2 * m11 - a3 * m12;
m23 = a3 - a1 * m20 - a2 * m21 - a3 * m22;
}
```

## Rotation about a point - using Euler

```/** rotate about a line centred on A */
void setRotate(sfvec3f A,double theta,double phi,double alpha){
double cosAlpha, sinAlpha, cosPhi, sinPhi,
cosTheta, sinTheta, cosPhi2, sinPhi2,
cosTheta2, sinTheta2, pi, c, a1,a2,a3;
if (A == null) {
a1=a2=a3=0;
} else {
a1 = A.x; a2 = A.y; a3 = A.z;
}
cosPhi = Math.cos(phi); sinPhi = Math.sin(phi);
cosPhi2 = cosPhi * cosPhi; sinPhi2 = sinPhi * sinPhi;
cosTheta = Math.cos(theta);
sinTheta = Math.sin(theta);
cosTheta2 = cosTheta * cosTheta;
sinTheta2 = sinTheta * sinTheta;
cosAlpha = Math.cos(alpha);
sinAlpha = Math.sin(alpha);
c = 1.0 - cosAlpha;
m00 = cosTheta2 * (cosAlpha * cosPhi2 + sinPhi2) + cosAlpha * sinTheta2;
m10 = sinAlpha * cosPhi + c * sinPhi2 * cosTheta * sinTheta;
m20 = sinPhi * (cosPhi * cosTheta * c - sinAlpha * sinTheta);
m01 = sinPhi2 * cosTheta * sinTheta * c - sinAlpha * cosPhi;
m11 = sinTheta2 * (cosAlpha * cosPhi2 + sinPhi2) + cosAlpha * cosTheta2;
m21 = sinPhi * (cosPhi * sinTheta * c + sinAlpha * cosTheta);
m02 = sinPhi * (cosPhi * cosTheta * c + sinAlpha * sinTheta);
m12 = sinPhi * (cosPhi * sinTheta * c - sinAlpha * cosTheta);
m22 = cosAlpha * sinPhi2 + cosPhi2;
m03 = a1 - a1 * m00 - a2 * m01 - a3 * m02;
m13 = a2 - a1 * m10 - a2 * m11 - a3 * m12;
m23 = a3 - a1 * m20 - a2 * m21 - a3 * m22;
m30 = m31 = m32 = 0.0;
m33 = 1.0;
}
```

## Rotation about a point - using Axis Angle

```public void setRotate(sfrotation rot,sfvec3f A) {

double c = Math.cos(rot.angle);
double s = Math.sin(rot.angle);
double t = 1.0 - c;
m00 = c + rot.x*rot.x*t;
m11 = c + rot.y*rot.y*t;
m22 = c + rot.z*rot.z*t;

double tmp1 = rot.x*rot.y*t;
double tmp2 = rot.z*s;
m10 = tmp1 + tmp2;
m01 = tmp1 - tmp2;

tmp1 = rot.x*rot.z*t;
tmp2 = rot.y*s;
m20 = tmp1 - tmp2;
m02 = tmp1 + tmp2;

tmp1 = rot.y*rot.z*t;
tmp2 = rot.x*s;
m21 = tmp1 + tmp2;
m12 = tmp1 - tmp2;

double a1,a2,a3;
if (A == null) {
a1=a2=a3=0;
} else {
a1 = A.x; a2 = A.y; a3 = A.z;
}

m03 = a1 - a1 * m00 - a2 * m01 - a3 * m02;
m13 = a2 - a1 * m10 - a2 * m11 - a3 * m12;
m23 = a3 - a1 * m20 - a2 * m21 - a3 * m22;
m30 = m31 = m32 = 0.0;
m33 = 1.0;
}```