Question: This is my assignment Create a Vector3 class that represents a point or direction in 3D space. Your Vector3 must overload the following operators: Vector3
This is my assignment
Create a Vector3 class that represents a point or direction in 3D space. Your Vector3 must overload the following operators:
Vector3 + Vector3 Add two Vector3 objects together. The result is a new Vector3 object whose x, y, and z are the sums of the left-hand and right-hand Vector3 objects x, y, and z respectively. Example: (1, 2, 3) + (1, 1, 1) = (2, 3, 4)
Vector3 - Vector3 Subtract a Vector3 from another. The result is a new Vector3 object whose x, y, and z are the differences of the left-hand and right-hand Vector3 objects x, y, and z respectively. Example: (1, 2, 3) - (1, 1, 1) = (0, 1, 2)
Vector3 * float Multiply a Vector3 by a scalar value (make sure to support the operands in either order). The result is a new Vector3 object whose x, y, and z are equal to the original Vector3 objects x, y, and z multiplied by the float value. Example: (1, 2, 3) * 2.5 = (2.5, 5.0, 7.5)
- Vector3 Negate a Vector3 to change the direction. This is the equivalent of multiplying a Vector3 object by -1. Example: -(1, 1, 1) = (-1, -1, -1)
std::ostream << Vector3 Allows your Vector3 object to get passed to std::cout. Should print Vector: X,Y,Z where X, Y, and Z are replaced by the Vector3s x, y, and z values respectively. Must return the reference to the std::ostream object.
Please also define the following public function in your Vector3 class:
float GetLength() Returns the length of the vector. The length is equal to x * x + y * y + z * z Example: (1, 2, 3).GetLength() == 1 * 1 + 2 * 2 + 3 * 3 = 14
This is what ive got
My Header file Vector3.h
#ifndef Vector3_H #define Vector3_H #include
using namespace std;
std::ostream& operator<<(std::ostream& left, const Vector3& v1) { cout << "Vector : " << "(" << v1.x << "," << v1.y << "," << v1.z << ")" << endl; return left; }
class Vector3 { public: // Contructor Vector3(float _x, float _y, float _z = 0); // Deconstructor ~Vector3(); float x, y, z; // Function declartion void Print(); // Function declartion for each operator overload Vector3 operator+(const Vector3 &xyz) const; Vector3 operator-(const Vector3 &xyz) const; Vector3 operator*(float scalar) const; Vector3 operator/(float scalar); Vector3 operator-(void) const; void Normalize(); // Function to get length of vector float GetLength(float); }; #endif
My Vector3.cpp
#include "Vector3.h" // Be sure to include your header file in your class cpp #include
std::ostream& operator<<(std::ostream& left, const Vector3& v1) { cout << "Vector : " << "(" << v1.x << "," << v1.y << "," << v1.z << ")" << endl; return left; }
// Makes it easier as to not have to type std::cout each time you want to print to console using namespace std; // Constructor which initalizes our variables Vector3::Vector3(float _x, float _y, float _z) { x = _x; y = _y; z = _z;
} // This is the deconstructor Vector3::~Vector3(){ } // Operator overload for addition Vector3 Vector3::operator+(const Vector3 &xyz) const { return Vector3(x + xyz.x, y + xyz.y, z + xyz.z); } // Operator overload for subtraction Vector3 Vector3::operator-(const Vector3 &xyz) const { return Vector3(x - xyz.x, y - xyz.y, z - xyz.z); } // Operator overload for multiplication Vector3 Vector3::operator*(float scalar) const { return Vector3(x * scalar, y * scalar, z * scalar); } // Operator to negate our vector Vector3 Vector3::operator-(void) const { return Vector3(-x, -y, -z); } // Operator to for normalization of a vector Vector3 Vector3::operator/(float scalar) { return Vector3(x /= scalar, y /= scalar, z /= scalar); } // Print function to print out to the console void Vector3::Print() { cout << "(" << x << ", " << y << ", " << z << ")" << endl; } // Function to get the length of the vector float Vector3::GetLength(float) { return sqrt(x*x + y*y + z*z); } // Function to normalize a vector void Vector3::Normalize() { float len = GetLength(float ());
x /= len; y /= len; z /= len; }
My Main.cpp
#include #include"Vector3.h"
using namespace std;
int main() { Vector3 p1(1, 2, 3); Vector3 p2(1, 1, 1); Vector3 total1 = p2 + p1;// Addtion Vector3 total2 = p2 - p1;// Subtraction Vector3 total3 = p1 * 2.5f;// Multiply Vector3 total4 = p2 * -1;// Negate the vector Vector3 Normalize(); total1.Print(); total2.Print(); total3.Print(); total4.Print();
system("pause"); return 0; }
I seem to having issues with my GetLenght(); I am not sure as to what I am doing wrong and I am also wondering if my std::ostream operator<< is done correctly let me know what changes need made if any.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
