This article uses C + + to implement a simple Vector3 class function, the temporary function is: 1 +-*/arithmetic operation 2 The number of vectors, also called: Dot multiply 3 vector product, also known as: Cross by 4 vector Unit (normalization)
//Vecotr3.h#pragmaOnceextern Const DoubleUzero; classVector3 {floatx, y, Z; Public: Vector3 (): X (0), Y (0), Z (0) {} Vector3 (floatX1,floatY1,floatz1): X (x1), Y (y1), Z (z1) {} Vector3 (ConstVector3 &v); ~Vector3 (); void operator=(ConstVector3 &v); Vector3operator+(ConstVector3 &v); Vector3operator-(ConstVector3 &v); Vector3operator/(ConstVector3 &v); Vector3operator*(ConstVector3 &v); Vector3operator+(floatf); Vector3operator-(floatf); Vector3operator/(floatf); Vector3operator*(floatf); floatDotConstVector3 &v); floatlength (); voidnormalize (); Vector3 Crossproduct (ConstVector3 &v); voidPRINTVEC3 (); };
//Vector3.cpp#include"Plane.h"#include<iostream>Const DoubleUzero = 1e-6; //copy constructor, you must reference the parameter for a constant, or the compilation does not passVector3::vector3 (ConstVector3 &v): X (v.x), Y (v.y), Z (v.z) {} Vector3::~Vector3 () {}voidVector3::operator=(ConstVector3 &v) {x=v.x; Y=v.y; Z=v.z; } Vector3 Vector3::operator+(ConstVector3 &v) {returnVector3 (x+v.x, Y+V.Y, z+v.z); } Vector3 Vector3::operator-(ConstVector3 &v) {returnVector3 (x-v.x, Y-V.Y, Zv.z); } Vector3 Vector3::operator/(ConstVector3 &v) {if(FABSF (v.x) <= Uzero | | FABSF (V.Y) <= Uzero | | FABSF (V.Z) <=Uzero) {Std::cerr<<"Over flow!\n"; return* This; } returnVector3 (x/v.x, Y/V.Y, z/v.z); } Vector3 Vector3::operator*(ConstVector3 &v) {returnVector3 (x*v.x, Y*V.Y, z*v.z); } Vector3 Vector3::operator+(floatf) {returnVector3 (X+f, Y+f, z+f); } Vector3 Vector3::operator-(floatf) {returnVector3 (x-f, y-f, Zf); } Vector3 Vector3::operator/(floatf) {if(FABSF (f) <Uzero) {Std::cerr<<"Over flow!\n"; return* This; } returnVector3 (x/f, y/f, z/f); } Vector3 Vector3::operator*(floatf) {returnVector3 (X*f, Y*f, z*f); } floatVector3::d OT (ConstVector3 &v) {returnx*v.x + y*v.y + z*v.z; } floatvector3::length () {returnSQRTF (Dot (* This)); } voidvector3::normalize () {floatLen =length (); if(Len < Uzero) Len =1; Len=1/Len; X*=Len; Y*=Len; Z*=Len; } /*cross product fork multiplication Formula AXb = | I, j, K | | a.x A.Y a.z| | b.x B.y b.z| = (A.X*B.Z-A.Z*B.Y) i + (a.z*b.x-a.x*b.z) j + (a.x+b.y-a.y*b.x) K*/Vector3 vector3::crossproduct (ConstVector3 &v) {returnVector3 (Y * v.z-z *V.y, Z* V.x-x *v.z, x* V.y-y *v.x); } voidVector3::p rintVec3 () {std::cout<<"("<<x<<", "<<y<<", "<<z<<")"<<Std::endl; }
To test the main program:
#include <iostream>#include<vector>#include"Vector3.h" using namespacestd; intMain () {Vector3 v31; Vector3 V32 (2.0f,3.0f,4.0f); Vector3 v33 (v32-1.0f); cout<<"We have original vector3s:\n"; V31.PRINTVEC3 (); V32.PRINTVEC3 (); V33.PRINTVEC3 (); cout<<"v32 crossproduct v33 is:\n"; Vector3 v3233=v32.crossproduct (V33); V3233.PRINTVEC3 (); cout<<"Now we normalize them:\n"; V31.normalize (); V32.normalize (); V33.normalize (); V3233.normalize (); V31.PRINTVEC3 (); V32.PRINTVEC3 (); V33.PRINTVEC3 (); V3233.PRINTVEC3 (); System ("Pause"); return 0; }
Result of Operation:
[C + +] [Code Base] Vector3 Space Vector class