3D Vector class for graphics development (C #)

Source: Internet
Author: User

Using system;using system.collections.generic;using system.linq;using system.text;namespace VectorDemo{//<        summary>///3D vector class///</summary> public class Vector3 {public float X {get; set;}        Public float Y {get; set;}        Public float Z {get; set;}        <summary>///default constructor, do nothing///</summary> public Vector3 () {}        <summary>///The constructor of the copy vector///</summary>//<param name= "a" ></param> Public Vector3 (Vector3 a) {this.            X = a.x; This.            Y = A.Y; This.        Z = A.Z;        }///<summary>///with the parameter constructor, the initialization is done with three values.        </summary>//<param name= "NX" ></param>//<param name= "NY" ></param> <param name= "NZ" ></param> public Vector3 (float NX, float NY, float NZ) {This .           X = NX; This.            Y = NY; This.        Z = NZ; } #region operator public static bool operator = = (Vector3 v1, Vector3 v2) {return v1. X = = v2. X && v1. Y = = v2. Y && v1. Z = = v2.        Z } public static bool operator! = (Vector3 v1, Vector3 v2) {return v1. X! = v2. X | | V1. Y! = v2. Y | | V1. Z! = v2.        Z }///<summary>//Overloaded unary "-" operator///</summary>//<param name= "V" ></param >//<returns></returns> public static Vector3 operator-(Vector3 v) {RET        Urn New Vector3 (-v.x,-v.y,-v.z); }///<summary>//Heavy Duty two "+" operator///</summary>//<param name= "v1" ></para m>//<param name= "v2" ></param>///<returns></returns> public static Vec Tor3 operator + (Vector3 v1, Vector3 v2) {return new Vector3 (v1. X + v2. X, v1. Y + v2.Y, v1. Z + v2.        Z); }///<summary>//Heavy Two "-" operator///</summary>//<param name= "v1" ></para m>//<param name= "v2" ></param>///<returns></returns> public static Vec Tor3 operator-(Vector3 v1, Vector3 v2) {return new Vector3 (v1. X-v2. X, v1. Y-v2. Y, v1. Z-v2.        Z);        }///<summary>///scalar multiplication///</summary>//<param name= "V" ></param> <param name= "A" ></param>///<returns></returns> public static Vector3 op        Erator * (Vector3 V, float a) {return new Vector3 (v.x * A, V.Y * A, v.z * a);        }///<summary>///<param name= "V" ></param> <param name= "A" ></param>///<returns></returns> public static Vector3 op Erator/(Vector3 V, float a) {float Oneovera = 1.0f/a;//: This does not check the return new Vector3 (v.x * Oneovera, v) except for 0.        Y * Oneovera, v.z * oneovera);        }//+ = operator is not explicitly overloaded, will be overloaded with the + operator and the implicit overload/-= operator is not explicitly overloaded, will be overloaded with the-operator and the implicit overload//*= operator is not explicitly overloaded, and is implicitly overloaded with the overloading of the * operator        The/= operator is not explicitly overloaded, overloading the standard multiplication operator with an overload of the/operator and implicitly overloading//<summary>/////vector point multiplication.        </summary>//<param name= "v1" ></param>//<param name= "v2" ></param>            <returns></returns> public static float operator * (Vector3 v1, Vector3 v2) { Return v1. X * v2. X + v1. Y * v2. Y + v1. Z * v2.        Z public override bool Equals (Object v) {if (!) (            V is Vector3)) {return false;        } return this = = (v as Vector3);        }///<summary>//vector left///</summary>//<param name= "a" ></param> &lT;param name= "V" ></param>///<returns></returns> public static Vector3 operator * (Floa        t A, Vector3 v) {return new Vector3 (A * v.x, A * v.y, A * v.z);        } #endregion///<summary>///Zero trace//</summary> public void Zero ()        {X = Y = Z = 0.0f;            }///<summary>///vector normalization///</summary> public void Normalize () {            float MAGSQ = x * x + y * y + z * z;                if (magsq > 0.0f)//check except 0 {float Oneovermag = (float) (1.0f/math.sqrt (MAGSQ));                X *= Oneovermag;                Y *= Oneovermag;            Z *= Oneovermag; }}///<summary>/////</summary>//&LT;RETURNS&GT;&LT;/RETURNS&G        T        public float Vectormag () {return (float) (MATH.SQRT (x * x + y * y + z * z));  }      <summary>///calculation of two vectors fork///</summary>//<returns></returns>                               Public Vector3 corssproduct (Vector3 v) {return new Vector3 (Y * v.z-z * v.y,        Z * v.x-x * v.z, X * v.y-y * v.x); }///<summary>//Calculate the distance between two points///</summary>//<param name= "V" &GT;&LT;/PARAM&G        T            <returns></returns> Public float Distance (Vector3 v) {float dx = x-v.x;            float dy = y-v.y;            float dz = z-v.z;        return (float) (math.sqrt (DX * dx + dy * dy + dz * dz));        } public override String ToString () {return String.Format ("X:{0},y:{1},z:{2}", X, Y, Z); }    }}

3D Vector class for graphics development (C #)

Related Article

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.