Problem Set 1-1
Problem Set 1-1
Siti Hawa
Marker's comments:
1 26
2 98
3 32
Total 156
Extension certification:
Signature of Convener:
1
COS30008 Semester August 2024 Ms. Siti Hawa
Problem 1
Start with the solution of tutorial 3 in which we implemented the class Vector3D.
In this problem, we which to extend Vector3D with a toString() method. This method
has to return a textual representation of a 3D vector. For example, toString() applied to
Vector3D( 1.0f, 2.0f, 3.0f ) has to yield a string “[1,2,3]” as textual
representation.
To support the new member function, class Vector3D is extended as follows
#pragma once
#include "Vector2D.h"
#include <string>
class Vector3D
{
private:
Vector2D fBaseVector;
float fW;
public:
Do not edit the provided files. To implement the required features, create a new source file,
say Vector3D_PS1.cpp, and define the new feature here. It is not strictly required, but it
helps to separate the definitions from the provided code. You have to include Vector3D.h
in the new source for the code to compile.
Use std::stringstream to implement the toString() method. The class
std::stringstream provides a memory stream. You can use formatted output (i.e., the
operator <<) to send data to this stream and at the end, use the method str() to obtain
the resulting string that toString() has to return.
Numerical data must be rounded to 10-4, that is, four positions after the period.
The file Main.cpp contains a test function to check your implementation of the new matrix
features. The code sequence
2
COS30008 Semester August 2024 Ms. Siti Hawa
void runP1()
{
gCount++;
Floating point values are printed with standard precision for type float.
3
COS30008 Semester August 2024 Ms. Siti Hawa
Problem 2
Start with the solution of tutorial 3 in which we implemented the classes Vector3D and
Matrix3x3 to perform vector transformations in 2D.
In this problem, we wish to extend the definition of class Matrix3x3 with some additional
matrix operations. In particular, we extend class Matrix3x3 with
• Matrix multiplication [26 marks]:
Two matrices F and G can be multiplied, provided that the number of columns in F is
equal to the number of rows in G. If F is n x m matrix and G is an m x p matrix, then
the product FG is an n x p matrix whose (i, j) entry is given by
X
m
(FG)ij = FikGkj
k=1
In the implementation, every row must be accessed once via calls to row(). You can
declare local variables. Computing the result does not require loops. The row vectors
are of type Vector3D which provides an index operator to access the corresponding
column entry.
• The transpose of a matrix [8 marks]:
The transpose of an n x m matrix M, denoted by MT, is an m x n matrix for which the
(i,j) entry is equal to Mji. For
2 2
M 11 M 12 M 13 M 11 M 21 M 31
M3x3 = 4 M 21 M 22 M 23 5 M3x3 = 4 M 12 M 22
T M 32 5
M 31 M 32 M 33 M 13 M 23 M 33
the transpose is .
In the implementation, every column must be accessed once via calls to column(). You
may use local variables, but it is not strictly necessary.
4
COS30008 Semester August 2024 Ms. Siti Hawa
In the implementation, every row must be accessed once via calls to row(). You can
declare local variables.
The implementation would need to compute the determinate of M and verify that it is
not zero. Use the given formula for calculation.
• Output operator for Matrix3x3 [14 marks]:
We can rely on the newly defined toString() method in Vector3D for this purpose.
To accommodate these operations, class Matrix3x3 is extended as follows
#pragma once
#include <ostream>
#include "Vector3D.h"
class Matrix3x3
{
private:
Vector3D fRows[3];
public:
Matrix3x3() noexcept;
Matrix3x3( const Vector3D& aRow1, const Vector3D& aRow2, const Vector3D& aRow3 ) noexcept;
Do not edit the provided files. To implement the required features, create a new source file,
say Matrix3x3_PS1.cpp, and define the new features here. It is not strictly required, but
it helps to separate the definitions from the provided code. You have to include Matrix3x3.h
in the new source for the code to compile.
5
COS30008 Semester August 2024 Ms. Siti Hawa
The file Main.cpp contains a test function to check your implementation of the new matrix
features. The code sequence
void runP2()
{
gCount++;
Matrix3x3 M ( Vector3D( 25.0f, -3.0f, -8.0f ),
Vector3D( 6.0f, 2.0f, 15.0f ),
Vector3D( 11.0f, -3.0f, 4.0f ) );
// test multiplication
// test determinate
// test hasInverse
cout << "Has M an inverse? " << (M.hasInverse() ? "Yes" : "No") << endl;
// test transpose
cout << "transpose of M:" << endl;
cout << M.transpose() << endl;
// test inverse
cout << "inverse of M:" << endl;
cout << M.inverse() << endl;
Floating point values are printed with standard precision for type float.
6
COS30008 Semester August 2024 Ms. Siti Hawa
Problem 3
Carl Friedrich Gauss and Carl Gustav Jacob Jacobi invented the trapezoid formula to calculate
the area of a trapezoid in the 18th century. We can use the trapezoid formula to determine
the both the area of a polygon and the orientation of the vertices of the polygon. If the vertices
are ordered in clockwise order, then the area is negative. If the vertices are arranged in
counterclockwise order, then the area is positive as shown in Figure 1. In computer graphics,
we use the orientation of a polygon to determine whether the polygon faces the viewer or
not. The latter allows for a process called back face culling that means, we do not have to
draw the polygon. Only if the polygon faces the viewer (even partly) do we have to draw it.
-1
Figure 1: Parallelogram with signed area -16, dinosaur with signed area 38.5.
7
COS30008 Semester August 2024 Ms. Siti Hawa
#pragma once
#include "Vector2D.h"
#include "Matrix3x3.h"
class Polygon
{
private:
Vector2D fVertices[MAX_VERTICES];
size_t fNumberOfVertices;
public:
Polygon() noexcept;
As in 0, do not edit the provided files. Rather create a new source file, called
Polygon_PS1.cpp, to implement the new methods getSignedArea() and
transform().
There are two sample files that you can use to test your solution: Parallelogram.txt and
Data.txt. The file Main.cpp contains a test function, testProblem2(), to verify your
implementation. For the input file Parallelogram.txt the test function outputs
Signed area: -16
Signed area of rotated polygon: -16
Polygon transformation successful.
The vertices in the parallelogram are arranged in clockwise order. The area of a polygon does
not change when it is simply rotated around the origin.
For the input file Data.txt the test function outputs
Signed area: 38.5
Signed area of rotated polygon: 38.5
Polygon transformation successful.