After finishing the below tasks, run the following commands to see if your code is correct. Note that your code has to compile successfully in order for the tests to be used.
mkdir build && cd build
cmake ..
make -j4
make test
As an alternative to make test
(or ctest
) you can run the individual
test_*
executables.
Write library code for representing three dimensional euclidean vectors
(not to be confused with a C++ std::vector
). In engineering,
a euclidean vector or simply a vector is a geometric object that has magnitude
(or length) and direction.
Such vectors have support many basic operations including addition, subtraction and scalar multiplication. There are also additional binary operations like the dot product and the cross product.
It is your duty to implement a data structure for storing and modifying vectors and the most common operations on such vectors.
The public interface of the class must support API laid out in the UML class
diagram below. You have to correctly declare the member functions' const
-ness
in order to allow the provided auto-tests to compile. Place all your
declarations and definitions inside the appropriate namespace
(the namespace name must match the namespace used in the provided test cases).
You may store any additional internal properties or add additional private or public member functions.
classDiagram
class Vector {
+ double x
+ double y
+ double z
+ Vector()
+ Vector(double x, double y, double z)
+ plus(const Vector& o) Vector
+ minus(const Vector& o) Vector
+ times(double s) Vector
+ times(const Vector& o) double
+ dot(const Vector& o) double
+ length() double
+ magnitude() double
+ cross(const Vector& o) Vector
+ print(std::ostream& os)
}
The length
or magnitude
The dot product of two vectors
Using column vectors, the dot product can be represented as follows
This task will primarily be graded using autotests. You also need to maintain a consistent coding style. In particular, all identifiers and comments have to be in English!