Thread: My 3D Vector class

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949

    My 3D Vector class

    I'm working on a test 3D engine, and have started on a 3D vector class. I'm looking for opinions, feel free to use it in your code if you wish (probably not enough there to use anyway :p ) . *NOTE: If this is in the wrong place, please tell me where to move it :D .* Also, this is not completed, so please don't yell at me for that :) . If something is wrong, PLEASE correct me. Anyway, here it is:

    Code:
    /*
    The Vector3D class: represents a 3 dimensional vector. Only has 3 variables: x, y, and z for displacement.
    */
    
    class Vector3D {
     public:
    
    /*Variables:
     I didn't make x, y, and z private, as the programmer should already know the inner workings of a vector,
    and would figure that there would be an x, y, and z variables. Plus, I hate the look of setX(int x) and getX() :D .
    */
      double x, y, z;
    
    /*Constructors:
     Vector3D v: x, y, and z are all set to 0.
     Vector3D v2(1, 2, 3): x, y, and z are set to 1, 2, and 3, respectivly.
     Vector3D v3(Vector3D v4): v3.x, v3.y, and v3.z are all set to v4.x, v4.y, and v4.z, respectivly.
    */
      Vector3D() {
       x = y = z = 0;
      }
    
      Vector3D(double a, double b, double c) {
       x = a;
       y = b;
       z = c;
      }
    
      Vector3D(const Vector3D &v) {
       x = v.x;
       y = v.y;
       z = v.z;
      }
    
    /*Operator overloadings:
     Vector3D operator+(Vector v): x, y, and z are added to v.x, v.y, and v.z, respectivly.
     Vector3D operator-(Vector3D v): x, y, and z are subtracted from v.x, v.y, and v.z, respectivly.
     Vector3D operator*(double w): x, y, and z are all multiplied by w.
     Vector3D operator/(double w): x, y, and z are all divided by w.
     Vector3D operator=(Vector3D v): this vector is set to v.
    */
      Vector3D operator+(Vector3D v);
      Vector3D operator-(Vector3D v);
      Vector3D operator*(double w);
      Vector3D operator/(double w);
      Vector3D operator=(Vector3D v);
    
    /*Other functions:
     void negate(): negate the vector - x, y, and z are set to -x, -y, and -z, respectivly
     double length(): returns the length of the vector.
     void normalize(): normalize the vector, vector / length of vector
    */
      void negate();
      double length();
      void normalize();
    };
    
    Vector3D Vector3D::operator+(Vector3D v) {
     return Vector3D((x + v.x), (y + v.y), (z + v.z));
    }
    
    Vector3D Vector3D::operator-(Vector3D v) {
     return Vector3D((x - v.x), (y - v.y), (z - v.z));
    }
    
    Vector3D Vector3D::operator*(double w) {
     return Vector3D((x * w), (y * w), (z * w));
    }
    
    Vector3D Vector3D::operator/(double w) {
     return Vector3D((x / w), (y / w), (z / w));
    }
    
    Vector3D Vector3D::operator=(Vector3D v) {
     x = v.x;
     y = v.y;
     z = v.z;
     return *this;
    }
    
    void Vector3D::negate() {
     x = -x;
     y = -y;
     z = -z;
    }
    
    double Vector3D::length() {
     return sqrt((x * x) + (y * y) + (z * z));
    }
    
    void Vector3D::normalize() {
     x /= length();
     y /= length();
     z /= length();
    }
    Last edited by Lurker; 11-09-2003 at 08:47 PM.
    Do not make direct eye contact with me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3D Network Analysis Tool
    By durban in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 11-08-2005, 06:33 PM
  2. A 3D Program for my 3D models...?
    By Rune Hunter in forum C++ Programming
    Replies: 26
    Last Post: 08-19-2004, 10:04 AM
  3. 3D starfield
    By VirtualAce in forum Game Programming
    Replies: 6
    Last Post: 06-26-2003, 12:40 PM
  4. 3D SDK for C++ programmers
    By chand in forum Game Programming
    Replies: 2
    Last Post: 05-20-2003, 07:38 AM
  5. 3d engines
    By Unregistered in forum Game Programming
    Replies: 7
    Last Post: 12-17-2001, 11:19 AM