C + + implementation of space vector class Vector3

Source: Internet
Author: User

Feed

Two years ago I wrote this article-the implementation of the Vector class. Now it seems that the code is a little ugly.

So, write it again.

Refer to the interface of the Vector3 class of Unity3d. Words don't say much, on the code.

Compiler: GCC 4.6.3


Code Listing

Vector3.h

#ifndef vector3_h#define vector3_h#include <math.h> #include <assert.h> #include <iostream> #define        PI 3.14159265using namespace Std;class vector3{public:float x;        Float y;        float Z;        Vector3 ();        Vector3 (float x, float y, float z);        ~vector3 ();        BOOL Hasnans () const;        void set (float _x, float _y, float _z);        Static functions static Vector3 back ();        Static Vector3 down ();        Static Vector3 forward ();        Static Vector3 left ();        Static Vector3 one ();        Static Vector3 right ();        Static Vector3 up ();        Static Vector3 Zero ();        float magnitude () const;        float sqrtmagnitude () const;        Vector3 normalized ();        Operators//add, Vectors Vector3 operator+ (const VECTOR3 &v) const;        Subtracts one vector from another Vector3 operator-(const VECTOR3 &v) const;        Vector3 operator* (float num) const; Vector3 Operator/(float num) const;        Vector3 &operator+= (const VECTOR3 &AMP;V);        Vector3 &operator-= (const VECTOR3 &AMP;V);        Vector3 &operator*= (float num);        Vector3 &operator/= (float num);        Returns true if vectors different.        BOOL Operator!= (Vector3 &v) const; Friend std::ostream& operator<< (std::ostream& output,const vector3& v) {output<&lt ;"            ("<<v.x<<", "<<v.y<<", "<<v.z<<") "<<" \ n ";        return output;        }//static Functions//calculate angle between, vectors.            static float Angle (const VECTOR3 &from, const Vector3 &to) {double C, D;            c = from.x*to.x + From.y*to.y + from.z*to.z;            D = sqrt (from.x*from.x + from.y*from.y + from.z*from.z) * sqrt (to.x*to.x + to.y*to.y + to.z*to.z);        Return ACOs (C/D) * 180.0/PI;       }//get dot result of Vector2. Static inline float Dot (const VECTOR3 &AMP;V1, const Vector3 &v2) {return v1.x * v2.x + v1.y * v2.        Y + v1.z * V2.Z;        }//get Cross result of Vector2.  Static inline Vector3 Cross (const VECTOR3 &AMP;V1, const Vector3 &v2) {return Vector3 ((V1.Y * v2.z)  -(V1.Z * v2.y), (V1.Z * v2.x)-(v1.x * v2.z), (v1.x * v2.y)-(v1.y        * v2.x));        }//get a normalized Vector3 from a given Vector3.            Static inline Vector3 Normalize (const Vector3 &v1) {Float tmplength = v1.magnitude ();        Return Vector3 (V1.x/tmplength, V1.y/tmplength, v1.z/tmplength); }//construct a coordinatesystem by a given Vector3.        V2 and v3 are output. static inline void CoordinateSystem (const Vector3 &AMP;V1, Vector3 *v2, Vector3 *v3) {if (FABSF (v1.x) &G T FABSF (V1.Y)) {Float Invlen = 1.0f/sqrtf (v1. x*v1.x + v1.z*v1.z);            *v2 = Vector3 (-v1.z * Invlen, 0.0f, v1.x * invlen);                }else {Float Invlen = 1.0f/sqrt (v1.y*v1.y + v1.z*v1.z);            *v2 = Vector3 (0.0f, V1.z * invlen,-V1.Y * invlen);        } *v3 = Vector3::cross (v1, *v2);        } static Vector3 clampmagnitude ();        Static Vector3 lerp (const VECTOR3 &AMP;V1, const VECTOR3 &AMP;V2);    Static Vector3 orthonormalize (); Protected:private:}; #endif//Vector3_h

Vector3.cpp

#include "vector3.h" Vector3::vector3 () {x = y = z = 0.0f;} Vector3::vector3 (float _x, float _y, float _z): X (_x), Y (_y), Z (_z) {ASSERT (! Hasnans ());} Vector3::~vector3 () {//dtor}bool Vector3::hasnans () const{return isNaN (x) | | isNaN (y) | | isNaN (z);} Vector3 Vector3::one () {return Vector3 (1, 1, 1);} Float vector3::magnitude () const{return sqrt (x*x + y*y + z*z);} Float vector3::sqrtmagnitude () const{return x*x + Y*y + z*z;}    void Vector3::set (float _x, float _y, float _z) {x = _x;    y = _y; z = _z;} Vector3 vector3::operator+ (const VECTOR3 &v) const{return Vector3 (x + v.x, y+ v.y, z+ v.z);} Vector3 vector3::operator-(const VECTOR3 &v) const{return Vector3 (x-v.x, Y-v.y, z-v.z);} Vector3 vector3::operator* (float num) const{return Vector3 (num*x, Num*y, num*z);} Vector3 vector3::operator/(float num) const{return Vector3 (X/num, Y/num, z/num);}    vector3& vector3::operator*= (float num) {x *= num; y *= num; z *= num; return *this;} Vector3&    vector3::operator/= (float num) {x/= num; y/= num; z/= num; return *this;}    vector3& vector3::operator+= (const Vector3 &v) {x + = v.x; y + = V.y; z + = v.z; return *this;}    vector3& vector3::operator-= (const Vector3 &v) {x-= v.x; y-= V.y; z-= v.z; return *this;} BOOL Vector3::operator!= (Vector3 &v) const{return x==v.x && y==v.y && z==v.z;}    Vector3 vector3::normalized () {Float tmplength = this->magnitude (); Return Vector3 (X/tmplength, Y/tmplength, z/tmplength);}

Test code

#include <iostream> #include "vector3.h" using namespace Std;int Main () {    Vector3 v1,v2,v3;    V1=vector3 (1.0,-1.0,0.0);    V2=vector3 ( -1.0,-1.0,0.0);    cout<< "v1:" <<v1<<endl;    V2=V2/2;    cout<< "V2:" <<v2<<endl;    cout<< "Angle:" <<vector3::angle (v1, v2) <<endl;    V3=vector3::cross (V1,V2);    Float A=vector3::D ot (V1,V2);    cout<< "V1 Dotmul v2:" <<a<<endl;    cout<< "v3. Length: "<<v3.magnitude () <<endl;    Vector3 v4 = v3.normalized ();    cout<< "v3 after Nomarlize:" <<v4<<endl;    return 0;}

Run results



Code description

With respect to the previous version of the Code, the new version of Vector3 first consisted of basic vector operations, while the following improvements were made:

1. The << symbol is overloaded so that the contents of the vector can be printed directly;

2. The basic operation symbol is overloaded, including returning the reference and returning the object, the returned object will not change itself, and the returned reference will modify the province at the same time;

3. Added some static inline methods for some calculations of vectors, about static inline, which can be understood as a static function, plus the properties of inline. Most of this function behaves like a normal static function, except that when called, GCC expands its sink code at its invocation to compile without generating a separate assembly code for the function.

C + + implementation of space vector class Vector3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.