Click here to Skip to main content
Click here to Skip to main content
Articles » Multimedia » GDI+ » General » Downloads
 
Add your own
alternative version
Go to top

Fun With Gravity

, 28 Jul 2008
A gravity simulation particle system.
using System;
using System.Xml.Serialization;

namespace ParticleSwarm {
	public class Vector {
		#region Private members
		private float m_Xcoord;

		private float m_Ycoord;

		private float m_Zcoord;
		#endregion

		#region Constructors
		public Vector() {
		}

		public Vector(float inX, float inY, float inZ) {
			m_Xcoord = inX;
			m_Ycoord = inY;
			m_Zcoord = inZ;
		}

		public Vector(float[] coordination) {
			m_Xcoord = coordination[0];
			m_Ycoord = coordination[1];
			m_Zcoord = coordination[2];
		}

		public Vector(Vector vector) {
			m_Xcoord = vector.X;
			m_Ycoord = vector.Y;
			m_Zcoord = vector.Z;
		}
		#endregion

		#region Public properties
		[XmlAttribute("x")]
		public float X {
			get { return m_Xcoord; }
			set { m_Xcoord = value; }
		}

		[XmlAttribute("y")]
		public float Y {
			get { return m_Ycoord; }
			set { m_Ycoord = value; }
		}

		[XmlAttribute("z")]
		public float Z {
			get { return m_Zcoord; }
			set { m_Zcoord = value; }
		}

		public static Vector Zero {
			get { return new Vector(0.0f, 0.0f, 0.0f); }
		}

		public static Vector XAxis {
			get { return new Vector(1.0f, 0.0f, 0.0f); }
		}

		public static Vector YAxis {
			get { return new Vector(0.0f, 1.0f, 0.0f); }
		}

		public static Vector ZAxis {
			get { return new Vector(0.0f, 0.0f, 1.0f); }
		}
		#endregion

		#region Methods
		public static Vector Add(Vector vector1, Vector vector2) {
			if ((object)vector1 == null || (object)vector2 == null)
				return null;
			return new Vector(vector1.X + vector2.X, vector1.Y + vector2.Y, vector1.Z + vector2.Z);
		}

		public static Vector Subtract(Vector vector1, Vector vector2) {
			if ((object)vector1 == null || (object)vector2 == null)
				return null;
			return new Vector(vector1.X - vector2.X, vector1.Y - vector2.Y, vector1.Z - vector2.Z);
		}

		public static Vector Negate(Vector vector) {
			if ((object)vector == null)
				return null;
			return new Vector(-vector.X, -vector.Y, -vector.Z);
		}

		public static Vector Multiply(Vector vector, float val) {
			if ((object)vector == null)
				return null;
			return new Vector(vector.X * val, vector.Y * val, vector.Z * val);
		}

		public static Vector Divide(Vector vector, float val) {
			if ((object)vector == null)
				return null;
			return new Vector(vector.X / val, vector.Y / val, vector.Z / val);
		}
		#endregion

		#region Operators
		public static bool operator ==(Vector vector1, Vector vector2) {
			if ((object)vector1 == null || (object)vector2 == null)
				return false;
			return ((vector1.X.Equals(vector2.X))
				&& (vector1.Y.Equals(vector2.Y))
				&& (vector1.Z.Equals(vector2.Z)));
		}

		public static bool operator !=(Vector vector1, Vector vector2) {
			if ((object)vector1 == null || (object)vector2 == null)
				return false;
			return ((!vector1.X.Equals(vector2.X)) 
				&& (!vector1.Y.Equals(vector2.Y))
				&& (!vector1.Z.Equals(vector2.Z)));
		}

		public static Vector operator+(Vector vector1, Vector vector2) {
			if ((object)vector1 == null || (object)vector2 == null)
				return null;
			return Vector.Add(vector1, vector2);
		}

		public static Vector operator-(Vector vector1, Vector vector2) {
			if ((object)vector1 == null || (object)vector2 == null)
				return null;
			return Vector.Subtract(vector1, vector2);
		}

		public static Vector operator-(Vector vector) {
			if ((object)vector == null)
				return null;
			return Vector.Negate(vector);
		}

		public static Vector operator*(Vector vector, float val) {
			if ((object)vector == null)
				return null;
			return Vector.Multiply(vector, val);
		}

		public static Vector operator/(Vector vector, float val) {
			if ((object)vector == null)
				return null;
			return Vector.Divide(vector, val);
		}
		#endregion

		#region Overides
		public override bool Equals(object obj) {
			Vector vector = obj as Vector;
			if ((Object)vector != null)
				return (m_Xcoord.Equals(vector.X)) 
					&& (m_Ycoord.Equals(vector.Y))
					&& (m_Zcoord.Equals(vector.Z));
			return false;
		}

		public override string ToString() {
			return string.Format("({0}, {1}, {2})", m_Xcoord, m_Ycoord, m_Zcoord);
		}

		public override int GetHashCode() {
			return m_Xcoord.GetHashCode() ^ m_Ycoord.GetHashCode() ^ m_Zcoord.GetHashCode();
		}
		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

BoneSoft
Software Developer (Senior) BoneSoft Software
United States United States
I've been in software development for more than a decade now. Originally with ASP 2.0 and VB6. I worked in Japan for a year doing Java. And have been with C# ever since.
 
In 2005 I founded BoneSoft Software where I sell a small number of developer tools.
Group type: Organisation (No members)



| Advertise | Privacy | Mobile
Web01 | 2.8.140921.1 | Last Updated 28 Jul 2008
Article Copyright 2007 by BoneSoft
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid