A, c#实现
/* Vector3 Definition Created by Taotao Mans on 2016-4-12 Brief: Package three-bit vector class Vector3//Modify record: Date:add SetA () Change Geta (); */Using System;Using System.Collections.Generic;Using System.Linq;Using System.Text;Using System.Threading.Tasks;Namespaceceshi{PublicClassVector3 {Privatefloat x;PrivateFloat y;Privatefloat Z;PrivateConstfloat E =0.0000001f;PublicFloat X {set {x =Value }get {return x; } }PublicFloat Y {set {y =Value }get {return y; } }PublicFloat Z {set {z =Value }get {return z; } }PublicVector3(float x,Float y,Float z) {this.x = x;This.y = y;This.z = Z; }PublicVector3(Vector3 VCT) {This.x = Vct.x;This.y = Vct.y;This.z = vct.z; }vector additionPublicStatic Vector3operator + (Vector3 A, Vector3 b) {Vector3 result =New Vector3 (a.x + b.x, A.y + b.y, a.z +b.z);return result; }Vector subtractionPublicStatic Vector3Operator-(Vector3 A, Vector3 b) {Vector3 result =New Vector3 (a.x-b.x, A.y-b.y, a.z-b.z);return result; }Divide the vector by one numberPublicStatic Vector3Operator/(Vector3 A,Float b) {if (b! =0) {ReturnNew Vector3 (a.x/b, a.y/b, a.z/b); }else {ReturnNew Vector3 (0,0,0); } }Left multiply by one numberPublicStatic Vector3operator * (Float A, Vector3 b) {ReturnNew Vector3 (A * b.x, A * b.y, A * b.z); }Multiply right by one numberPublicStatic Vector3operator * (Vector3 A,Float b) {ReturnNew Vector3 (a.x * b, a.y * b, a.z * b); }Dot Multiplication of vectorsPublicStaticFloatoperator * (Vector3 A, Vector3 b) {Return a.x * b.x + a.y * b.y + a.z * B.Z; }Determine if two vectors are equalPublicStaticbooloperator = = (Vector3 A, Vector3 b) {if (Math.Abs (a.x-b.x) < e && Math.Abs (A.Y-B.Y) < e && Math.Abs (A.Z-B.Z) < e) {ReturnTrue }ElseReturnFalse }Judge two vectors to be equalPublicStaticboolOperator! = (Vector3 A, Vector3 b) {Return! (A = = B); }PublicOverrideboolEquals(Object obj) {ReturnBase. Equals (obj); }PublicOverrideIntGetHashCode() {ReturnBase. GetHashCode (); }PublicOverrideStringTostring() {ReturnBase. ToString (); }Vector Cross ProductPublicStatic Vector3Cross(Vector3 A, Vector3 b) {Returnnew Vector3 (A.Y * b.z-a.z * b.y, A.Z * b.x-a.x * b.z, a.x * b.y-a.y * b.x);} //vector of modulo public static float magnitude (Vector3 a) {return (float) MATH.SQRT (a.x * a.x + a.y * a.y + a.z * a.z); } //unit vector public static Vector3 normalize (Vector3 a) {float magnitude = magnitude (a); return new Vector3 (A.x/magnitude, a.y/magnitude, A.Z/ magnitude); } }}
Second, C + + implementation
Reprinted from: 3D Mathematical basic graphics and game development
#Include <math.h>Class vector3{Publicfloat x, y, Z;Default constructor Vector3 () {}Copy Constructor Vector3 (Const VECTOR3 &a): X (a.x), Y (A.Y), Z (a.z) {}//A constructor with parameters, with three values to complete the initialization of the Vector3 (Float NX,Float NY,FLOAT NZ): X (NX), Y (NY), Z (NZ) {}Standard object manipulationOverloads the operator and returns a reference to achieve Lvalue Vector3 &operator = (Const Vector3 &a) {x = a.x; y = a.y; z = a.z;return *this;}//Overloading the "= =" operatorbooloperator = = (Const VECTOR3 &a)const{return x = = a.x && y = = A.y && z = = a.z;}boolOperator! = (Const VECTOR3 &a)const{return x! = a.x | | Y! = A.y | | Z! = a.z;}Vector operationsSet to zero vectorvoidZero() {x = y = z =0.0f;}Overloads the unary "-" operator Vector3Operator-()const{Return Vector3 (-X,-y,-Z);}Overloading the two-dollar "+" and "-" operators Vector3operator + (Const VECTOR3 &a)const{return Vector3 (x + a.x, y + a.y, z + a.z);} Vector3Operator-(Const VECTOR3 &a)const{Return Vector3 (x-a.x, Y-A.Y, z-a.z);}The multiplication method of Vector3 with scalaroperator * (Float a)const{return Vector3 (x * A, Y * A, Z * a);} Vector3Operator/(Float a)const{float Oneovera =1.0f/a;return Vector3 (x * oneovera, y * oneovera, Z * oneovera);}Overloaded reflexive operator Vector3 &operator + = (Const Vector3 &a) {x + = a.x; y + = A.y; z + = A.Z;return *this;} Vector3 &Operator-= (Const Vector3 &a) {x = a.x; y-= A.y; z-= A.Z;return *this;} Vector3 &Operator *= (Float a) {x *= A; y *= A; z *= A;return *this;} Vector3 &Operator/= (Float a) {float Oneovera =1.0f/a;x *= Oneovera; Y *= Oneovera; Z *= oneovera;return *this;}Vector NormalizationvoidNormalize(){float MAGSQ = x * x + y * y + z * z;if (Magsq >0.0f) {float Oneovermag =1.0f/sqrt (MAGSQ); x *= oneovermag;y *= oneovermag;z *= Oneovermag;}}Vector point multiplication, overloaded standard multiply operatorFloatoperator * (Const VECTOR3 &a)const{return x * a.x + y * a.y + z * A.Z;}};Non-member functionsThe vector mode of seekingInlineFloatVectormag(Const Vector3 &a) {Returnsqrt (a.x * a.x + a.y * a.y + a.z * a.z);}Calculates the cross-multiplication of two vectorsInline Vector3Crossproduct(Const VECTOR3 &a,Const Vector3 &b) {Return Vector3 (A.Y * b.z-a.z * b.y, A.Z * b.x-a.x * b.z, a.x * b.y-a.y * b.x);}//Implement scalar left multiplicationInline Vector3operator * (float K, const Vector3 &V) {return Vector3 (k * v.x, K * v.y, K * v.z);} //calculates the distance between two points inline float distance ( Const VECTOR3 &a, const Vector3 &b) {float dx = a.x-b.x;< Span class= "Hljs-keyword" >float dy = a.y-b.y; float dz = a.x-b.z; return sqrt (DX * dx + dy * dy + dz * dz);} //provides a global 0 vector extern const Vector3 Kzerovector
C # Encapsulates a three-dimensional vector, and another look at someone else's C + + package