You are on page 1of 38

Maths Assignment 1 – Matrices

Contents:

1. Obtaining the vertices of a 2D Image


a. Drawing the vertices in 3DS Max
b. Exporting the vertices to Excel
2. 2D Transformation
a. Translation
b. Scaling
c. Shearing
d. Rotation around an arbitrary point
e. Reflection across the X and Y axis
f. Reflection across an arbitrary axis
3. 3D Transformations
a. 1 Point Perspective
(i). Vanishing Points
b. 2 Point Perspective
c. 3 Point Perspective
d. General Perspective
e. Orthographic
f. Scaling
g. Shearing
h. Rotation around an axis
i. Rotation around an arbitrary axis
j. Reflection in a arbitrary plane
1. Obtaining the vertices of a 2D Image

1a. Drawing the vertices in 3DS Max


To begin this assignment, I had to first plot the 2D vertices of a chosen
vehicle. I decided to use a helicopter. I decided to use the following side
view image of a helicopter to plot my co-ordinates.

To plot my co-ordinates I imported the above image into 3D Studio Max. I


then used the line tool to trace around the image, plotting my co-
ordinates. Below are a couple of images showing the process.

1b. Exporting the Vertices to Excel


After I had drawn each part of the helicopter as a separate line I had to
export these co-ordinates into excel. To do this, I selected the part of the
helicopter I wanted the co-ordinates for and chose to export it as a
Wavefront Object. This produced an .Obj file, which contained the X, Y and
Z co-ordinates of the helicopter part and was also readable in any
common text reader such as notepad. This was good as it allowed me to
easily copy the co-ordinates into excel without too much trouble. The
reason for each part of the helicopter being split up is because when
trying to draw the helicopter as a graph in excel, if there was no split then
all parts would be joined.
A Wavefront (.Obj) file open in Notepad
After I had exported the helicopter from 3D Studio Max and imported all
the X, Y and Z data into excel I plotted the co-ordinates on a scatter graph
and joined the lines. Below is the final result.
2. 2D Transformations

The first section of this assignment is 2D transformations. This section will


see the following transformations being implemented:
• Translation
• Scaling
• Shear
• Rotation
• Reflection
Screenshots as well as short descriptions will be produced to explain how I
achieved each transformation. In the screenshots, the blue helicopter will
represent the original position, whereas the red helicopter will represent
the helicopter with the transformation applied.
2a. Translation
The first transformation I applied to the helicopter was a translation.
Translation is used to move an object along the X and Y axis. In 2D, the
matrix to achieve this is a 3x3 matrix as shown below.

Where: a=distance along the X axis


b=distance along the Y axis

Example:
If I wanted to move the helicopter 150 units along the X axis and 200 units
along the Y axis the matrix would look like:

The result would look like the following:


2b. Scaling
The next transformation I applied was scaling. Scaling is used to expand
or shrink and object. The matrix to achieve scaling is shown below.

Where: a=scaling along the X axis


b=scaling along the Y axis

However, unlike translation, scaling is factor based. This means that the
current length and height of the helicopter, will be multiplied by the
values entered for a and b.

Example
If I entered the value of 2 into a and b, both the height and width of the
helicopter would be multiplied by 2. This would create a helicopter twice
as big as the original. The matrix to achieve this is shown below.

The result would look like the following:

Values less than 1 can also be applied to the scaling matrix. The result of this is a
smaller object. The image below, is a result of entering 0.5 into both a and b.
2c. Shearing
Shearing produces a distortion of the object along a specific axis
proportional to the opposite axis. The matrix to achieve shearing is shown
below.

Where: a=shearing along the X axis


b=shearing along the Y axis

Just like scaling, shearing is also factor based.

Example
If I entered the value of 0.5 into both a and b then the helicopter would be
sheared by a factor of 0.5 along the X and Y axis. The matrix to achieve
this is shown below.

The result would look like the following:


2d. Rotation around an arbitrary point
Rotation around an arbitrary point is the first transformation that requires
using multiple types of transformations. The process of rotating around an
arbitrary point requires 3 matrices. One matrix translates the point to the
origin, one performs the actual rotation and the final matrix translates the
point back to its original position. The set of matrices below shows the
process.

Where: a=translation along the X axis


b =translation along the Y axis
θ=angle of rotation (in radians)

The reason for the layout being backwards is due to the way that the
matrices need to be multiplied. For them to work correctly, they need to
be multiplied in the opposite order.
The above 3 matrices would then be multiplied together to create 1 final
matrix. The coordinates of the 2D helicopter could then my multiplied
together with this final matrix to produce the coordinates of the rotated
helicopter.

Example:
If I wanted to rotate the helicopter 45° around the point (160,100) I would
substitute these values into the above matrices to produce the following
matrices. However, I would make sure I entered the degrees into the
rotation matrix as radians. 45°= 0.785398 Radians.

I would then multiply together these matrices to produce the final matrix. I
would then multiply the coordinates for each part of the helicopter to
produce the coordinates for the rotated helicopter. I can then re-plot this
rotated helicopter onto a graph as shown below.
2e. Reflection across the X and Y axis
Reflection across either the X or Y axis requires the use of 1 matrix. The matrix
used is very similar to the 3x3 identity matrix. The matrix below shows which 2
parameters affect the reflection of an object.

To perform a reflection across an axis, -1 in placed in either the a position,


b position or both. The matrices below show where to specifically place
the -1 for the desired reflection.

Reflect across the Y Reflect across the X Reflect across both


Axis Axis Axis

The result of applying these matrices is shown below.

(Across the Y Axis)


(Across the X Axis)

(Across both Axes)


2f. Reflection across an arbitrary axis
Reflection across an arbitrary axis is most likely the hardest 2d
transformation to achieve. The process of reflecting across an arbitrary
axis requires 5 matrices. The set of matrices required are shown below.

Just like rotating around an arbitrary point, these matrices are shown in
this order because they have to be multiplied together in this order. From
right to left the matrices above perform the following actions:

1. Translate the arbitrary axis down so it passes through the origin.


2. Rotate the arbitrary axis down so it lies on the X axis.
3. Reflect across the X axis.
4. Rotate the arbitrary axis back to its original angle.
5. Translate the arbitrary axis back up to its original position.

The following calculation is performed in excel to calculate the angle


required (in radians) to rotate the arbitrary axis to the X axis and then
back up again.

=ATAN((b1-b)/(a1-a)

Where: a & b = X and Y coordinates of the first point of the arbitrary


axis
a1 & b1 = X and Y coordinates of the second point of the
arbitrary axis

Example
To reflect the helicopter across an arbitrary axis equal to (0,0) (400,400)
you would replace a and b with the X and Y values of the first point. In this
case it is 0, but could be anything. The above formula is then calculated to
get the correct angle. In this example the calculation will look like the
following.
=ATAN((400-0)/(400-0)
This gives the angle:
Radians = 0.785398
Degrees = 45°

These numbers can now be substituted into the original group of 5


matrices to produce the following set.
The above set of matrices can then all be multiplied together to create 1
final matrix. This final matrix is then multiplied by each part of the
helicopter to achieve the transformed helicopter. The image below is proof
of this example working. The turquoise line shows the arbitrary axis.
3. 3D Transformations

The final section of this assignment is 3D transformations. This section will


see the following transformations being implemented:
• 1 Point Perspective
• 2 Point Perspective
• 3 Point Perspective
• General Perspective
• Orthographic
• Scaling
• Shearing
• Rotation around an axis
• Rotation around an arbitrary axis
• Reflection in an arbitrary plane

Screenshots as well as short descriptions will be produced to explain how I


achieved each transformation. In the screenshots, the blue structure will
represent the original position, whereas the red structure will represent
the structure with the transformation applied.
The first 5 transformations in this section will be methods to achieve
different perspectives and ways to view a 3D object.
3a. 1 Point Perspective
The first type of perspective is 1 point perspective. A fundamental
characteristic of this perspective is that it only has 1 vanishing point.
There are 2 matrices involved to achieve this perspective. One is a
translation matrix which moves the object around the X, Y and Z axis. The
second is the projection matrix which creates the perspective. The
required matrices are shown below.

Where: d = Distance
x & y = Translation variables for the X and Y axis
Example
If I wanted to show my 3D object in 1 point perspective at a distance of 40
and position it 10 units along the X and Y axis I would substitute the d, x
and y placeholders with these numbers accordingly. The matrices would
then look like below.

These 2 matrices would then be multiplied together to create 1 final


matrix. When all points of the 3D object are multiplied by this final matrix
the result will be like the image below.
3a(i). Vanishing Points

To find out where the vanishing points are, a few more calculations are
required on top of the original perspective matrices. The final matrix
needs to be multiplied by a vanishing point matrix 4*3 in size. The matrix
is shown below.

The result of multiplying the above matrix by the final matrix will produce
a result in the format shown below.

To then find the homogenous coordinates of each vanishing point the


following calculations are performed.

1st Point - X = a/g


Y = d/g

2nd Point - X = b/h


Y = e/h

3rd Point - X = c/i


Y = f/i

Obviously there will only be 2 vanishing points in 2 point perspective and


3 vanishing points in 3 point perspective. Therefore, only calculate the
vanishing points that you require. The vanishing point for the 1 point
perspective example was worked out to be:
• (0,0).
3b. 2 Point Perspective
The second type of perspective is 2 point perspective. This type of
perspective has 2 vanishing points. There are 3 matrices involved to
achieve this perspective. One is a translation matrix which moves the
object around the X, Y and Z axis. The second is a rotation around the Y
axis. This is what adds the 2nd vanishing point. The 3rd and final matrix
required is the projection matrix. The matrices described above are shown
below.

Where: d = Distance
x & y = Translation variables for the X and Y axis
θ = Angle to rotate object around the Y axis
(Radians)

Example
If I wanted to show my 3D object in 2 point perspective at a distance of 40 and
position it 10 units along the X and -100 units along the Y axis I would substitute
the d, x and y placeholders with these numbers accordingly. I would also enter
the value of 0.785398 Radians (45 Degrees) into the rotation matrix. I chose 45°
as I feel it best represents 2 point perspective. The matrices with the required
data would then look like below.

These 3 matrices would then be multiplied together to create 1 final


matrix. When all points of the 3D object are multiplied by this final matrix
the result will be like the image below.
Using the previous section to calculate the vanishing points, in this example the
2 vanishing points are:
• (40,0)
• (-40,0)
3c. 3 Point Perspective
The third type of perspective is 3 point perspective. This type of
perspective has 3 vanishing points. There are 4 matrices involved to
achieve this perspective, 1 more than 2 point perspective. This
perspective is exactly the same as 2 point perspective, apart from a
rotation around the X axis is also applied. Instead of showing the full blank
set of matrices, I will just show a blank X rotation matrix. The matrix that
is shown below will slot in just before the Y axis rotation in the final matrix
chain.

Example
If I wanted to show my 3D object in 3 point perspective at a distance of -100 and
position it 10 units along the X and Y axis I would substitute the d, x and y
placeholders with these numbers accordingly. I would also enter the value of
0.785398 Radians (45 Degrees) into the Y axis rotation matrix and 0.523599
Radians (30 Degrees) into the X axis rotation matrix. I chose 45° and 30° as I feel
it best represents 3 point perspective. The matrices with the required data would
then look like below.

These 4 matrices would then be multiplied together to create 1 final


matrix. When all points of the 3D object are multiplied by this final matrix
the result will be like the image below.

Using the section 3a(i) to calculate the vanishing points, this example gave the
following 3 vanishing points:
• (-115.47,-57.735)
• (0 ,173.2051)
• (115.4701,-57.735)
3d. General Perspective
The fourth type of perspective is general perspective. This is the main
type of perspective I have used throughout my assignment to display
most of the 3D transformations. The reason for this is that I feel it shows
the best viewpoint and does not look distorted like 1, 2 and 3 point
perspective. To achieve this type of perspective only one matrix is
needed. This 4x4 matrix contains 3 configurable parts which effectively
control where the viewpoint is positioned on the X, Y and Z axis. The
matrix below shows the layout and each configurable part.

Where: x = Distance along the X axis


y = Distance along the Y axis
z= Distance along the Z axis
Example
To view the 3D object I would substitute in the x, y and z distances of my
choice. For this example I will show general perspective with an x distance
of 50, a y distance of 100 and a z distance of -300. When all these values
are substituted into the x, y and z positions the matrix will appear like the
one below.

This matrix can then be multiplied by the various parts of the 3D object to
create the view perspective shown below.
3e. Orthographic
The fifth and final type of perspective is orthographic (parallel).
Orthographic perspective is often widely used in blue prints for various
structures. One of the main features of orthographic perspective is that it
keeps all angles and length in proportion. To show orthographic texture
there is only 1 matrix involved. This matrix is used to rotate around the X
axis. When the angle of rotation is 90°, the view will be top down. When the
angle of rotation is 0, the view will be side on. The matrix below shows the X axis
rotation matrix.

Example
Top down View:
The view the 3D structure top down, a rotation angle of 90° (1.570796
Radians) will be entered into the matrix above. This matrix will then be
multiplied by each part of the 3D structure. The result is shown below.

Side View:
The view the 3D structure side on, a rotation angle of 0° (Identity Matrix)
will be entered into the matrix above. As this will produce an identity
matrix, side on view is the default view for orthographic. This matrix will
then be multiplied by each part of the 3D structure. The result is shown
below.
3f. Scaling
The first transformation after perspectives I applied was scaling. Scaling is
used to expand or shrink and object. The matrix to achieve scaling in 3D is
very similar to the 2D scaling matrix. Apart from it is 4x4 in size, and has
an extra variable, the Z axis. Te matrix to scale in 3D is shown below.

Where: x=scaling along the X axis


y=scaling along the Y axis
z=scaling along the Z axis
Like in 2D, scaling in 3D is also factor based.

Example
If I entered the value of 2 into both x and z and 0.5 into z both the length
and depth of the 3D object would be multiplied by 2. However, the height
of the 3D object would be divided by 2 due to the 0.5. This would create a
3D object twice as big as the original along the X and Z axis, but half the
size along the Y axis. The matrix to achieve this is shown below.

The result would look like the following.


3g. Shearing
In 3D, shearing produces a distortion of the object along a specific plane
proportional to the opposite planes/axis. There are 3 different matrices to
perform 3D shearing, 1 for each plane.

Shear along the XY Shear along the XZ Shear along the YZ


Plane Plane Plane

Where: x=shearing along the X axis


y=shearing along the Y axis
z=shearing along the Z axis

Just like scaling, shearing is also factor based.

Example
XY Shearing:
To show an example of shearing along the XY plane I shall substitute the x
and y values in the first matrix to create a matrix that will shear by a
specified factor.

This matrix is then multiplied together with the projection matrix which
produces the following object.
XZ Shearing:
To show an example of shearing along the XZ plane I shall substitute the x
and z values in the first matrix to create a matrix that will shear by a
specified factor.

This matrix is then multiplied together with the projection matrix which
produces the following object.

YZ Shearing:
To show an example of shearing along the YZ plane I shall substitute the y
and z values in the first matrix to create a matrix that will shear by a
specified factor.

This matrix is then multiplied together with the projection matrix which
produces the following object.
3h. Rotation around an axis
In 3D, rotation around the X, Y or Z axis is relatively easy to achieve. To
perform all 3 at once, it requires 3 different matrices. Each fed with a
specific angle in radians. Below are the 3 different rotation matrices.

Rotation around the X Rotation around the Y Rotation around the Z


axis axis axis

Example
X Rotation:
To show an example of rotation around the X axis I shall substitute an
angle into the matrix. I shall enter 45° (0.785398 Radians). Below is the
result.

Y Rotation:
To show an example of rotation around the Y axis I shall substitute an
angle into the matrix. I shall enter 45° (0.785398 Radians). Below is the
result.
Z Rotation:
To show an example of rotation around the Z axis I shall substitute an
angle into the matrix. I shall enter 45° (0.785398 Radians). Below is the
result.
3i. Rotation around an arbitrary axis
Rotation around an arbitrary axis is where 3D transformations begin to get
hard and quite long. It requires 8 matrices, 6 of which are relatively
similar. These matrices perform the following.
1. Translate the arbitrary axis so it passes through the origin
2. Rotate the arbitrary axis around the Y axis so it lies in the YZ plane
3. Rotate the arbitrary axis around the X axis so it lies on the Y axis
4. Perform the main rotation around the z axis
5. Rotate the arbitrary axis around the X axis back to its original
position
6. Rotate the arbitrary axis around the Y axis back to its original
position
7. Translate the arbitrary axis back to its original position
8. Project everything

The best way to explain this process would be to go straight to an


example using real data.

Example
For this example, I shall rotate around the line (0,0,0)(100,100,100) by 45°
(0.785398 Radians).
Step 1:
To perform step 1 a translation matrix is required which will translate the
line down to the origin. To do this the first coordinates will be inserted into
the following matrix.

With values substituted >


Due to the first coordinates being (0,0,0) this translation matrix just
becomes a identity matrix, however if the line was anything other than
(0,0,0) this matrix would make the arbitrary axis pass through the origin.
Step 2:
The next step is to rotate the arbitrary axis so it lies on the YZ plane.
However, the angle to do this rotation is unknown. To calculate the angle
the following calculation is performed.
First the dot product needs to be found for the 2nd set of coordinates
for the arbitrary axis (100,100,100) and the Z axis. The image below
shows the calculation that is performed.

A B
In this example A.B = 1000

The reason the Y value for the A set of coordinates is set to 0 is because
the line needs to be projected onto the XZ plane for the correct angle to
be found.
Next the modulus for both A and B needs to be found. This can be
calculated in excel with the following formula.
|A| = SQRT(Ax^2+Ay^2+Az^2) = 141.4214
|B| = SQRT(Bx^2+By^2+Bz^2) = 10

Finally the angle can be worked out by performing the following


calculation in excel.

Angle (Radians) =ACOS((A.B)/(|A|*|B|))


= 0.785398 Radians or 45°
This angle can then be substituted into the correct rotation matrix. The
correct rotation matrix for this step is a rotation around the Y axis like
shown below.

Step 3:
In this step, the exact same is done that is done in step 2 apart from 2
small changes. These changes are that instead of reaping the calculations
with A and B, the calculation needs to be done with the arbitrary axis in
the new position after the Y axis rotation (let’s call it D). Also this time,
instead of using the Z axis to find the dot product, the Y axis is used this
time like shown below.

D C
In this example D.C = 1000
The rest of the calculation can now be performed just like in step 2
|D| = SQRT(Dx^2+Dy^2+Dz^2) = 173.2051
|C| = SQRT(Cx^2+Cy^2+Cz^2) = 10

Finally the angle can be worked out by performing the following


calculation in excel.

Angle (Radians) =ACOS((D.C)/(|D|*|C|))


= -0.95532 Radians or -54.7356°
This angle can then be substituted into the correct rotation matrix. The
correct rotation matrix for this step is a rotation around the X axis like
shown below.

Step 4:
Step 4 is a very simple step and just involved choosing how much you
want to rotate the 3D object around the arbitrary axis. In this example I
have chosen 0.785398 Radians or 45°. The correct rotation matrix for this
step is a rotation around the Y axis like shown below.

Step 5:
Step 5 basically performs the inverse of step 3, there is no calculation
involved. A (-) sign is added to the front of the values that are entered into
the X axis rotation matrix. Due to all the values within this matrix being
negative, they instantly become positive inverting the result of the matrix.
The matrix for this step is shown below.

Step 6:
This step is exactly the same as step 5, but instead performs the inverse
for the matrix used in step 2 to rotate around the Y axis. The matrix for
this step is shown below.
Step 7:
This step performs the inverse of step 1 to translate the arbitrary axis
back up to its original position. However, because like in step 1 the
arbitrary axis already crosses the origin this matrix does nothing and
becomes an identity matrix. Due to this, I will not show the matrix used
for this step.

Step 8:
The final step is to project the result into a suitable perspective. In this
example I used the general perspective matrix described in section 3d.
Next, all 8 of the matrices used to perform this transformation
needed to be multiplied together in reverse order, starting with the
projection matrix and ending with the first translation matrix. This created
the final matrix as shown below.

This matrix can then be multiplied with the various parts of my 3D object
to perform the following transformation.
3j. Reflection in an arbitrary plane
Reflection in an arbitrary plane is very similar to rotation around an
arbitrary axis. However there are 2 main differences. The first difference is
that the normal of the arbitrary plane becomes the arbitrary axis.
However, there is a relatively long process in calculating the normal of a
plane. The second difference is that instead of rotating around the Y axis
like in step 4, you simply reflect the object in the Y axis.
As this process is very similar to section 3i, I will only explain how to
calculate these 2 differences.

Finding the normal of an arbitrary plane:


A plane has to consist of at least 3 points positioned anywhere in 3d
space. Once 3 points have been defined, the normal of this plane can be
found.
First, one of the points of the arbitrary plane must pass through the
origin. To do this it is translated down to the origin. This process is
described many times in the sections above, so will not be described this
time. Once the plane is lying on the origin there should now be 2 other
vectors coming from the origin to points in 3D space.
For the sake of this example let’s label the first vertex A and the 2nd
vertex B. To find the normal the following calculation must be done.

((Aj*Bk)-(Bj*Ak))i – ((Ai*Bk)-(Bi*Ak))j + ((Ai*Bj)-(Bi*Aj))k


The result of the above calculation should give a new vector consisting of
3 numbers in terms of i, j and k. This vector can then be imported to the
set of matrices in section 3i to achieve the desired result.

Example:
Now for a proper example with real numbers, let’s use the following
coordinates for the plane.

Due to this plane already having a point on the origin there is no need to
translate it, therefore we can move straight onto finding the normal. By
applying the method shown above the image and formula below show
how it works with real numbers.
((0*30)-(0*0))i – ((30*30)-(0*0))j + ((30*30)-(0*0))k
=
0i -900j 900k

Reflection in the y axis:


As described in the introduction to section 3j, it mentioned that step 4
from section 3i needed to be replaced with a reflection in the Y axis for the
reflection in an arbitrary plane to work correctly. The correct matrix to
achieve this is shown below.

The finished product:


With all the calculations and matrices correctly made from both section 3i
and 3j the reflection in an arbitrary plane should correctly work. The
image below shows it successfully working with the 3 coordinates of the
plane as
• (0,0,0)
• (30,0,0)
• (0,30,30)