[.net] vector

Started by
9 comments, last by gp_s 16 years, 1 month ago
I am using VB.NET 2005 and I need to use a Vector. Since it is not in .NET framework 2 I am trying to make my own structure similar to the one which is in .NET framework 3 and 3.5 but I dont know how to define the operators for +,-,*,/ and MOD. Can anyone help?
Advertisement
See this article: http://www.developer.com/net/vb/article.php/3512311.
Hope it helps.
Do you mean "vector" as in the linear algebra, direction-and-distance vector, or as in the variable-length container vector? It's rather confusing that they have the same name!
I think he means something like this.. http://msdn2.microsoft.com/en-us/library/bb324325.aspx

public struct Vector3{	public float X;	public float Y;	public float Z;	public Vector3 ()	public Vector3 (float valueX, float valueY, float valueZ)	public static Vector3 Empty	public override int GetHashCode ()	public override bool Equals (object compare)	public static bool operator == (Vector3 left, Vector3 right)	public static bool operator != (Vector3 left, Vector3 right)	public override string ToString ()	public static Vector3 operator - (Vector3 vec)	public static Vector3 operator + (Vector3 left, Vector3 right)	public static Vector3 operator - (Vector3 left, Vector3 right)	public static Vector3 operator * (float right, Vector3 left)	public static Vector3 operator * (Vector3 left, float right)	public static Vector3 Multiply (Vector3 source, float f)	public void Multiply (float s)	public float Length ()	public static float Length (Vector3 source)	public float LengthSq ()	public static float LengthSq (Vector3 source)	public static float Dot (Vector3 left, Vector3 right)	public static Vector3 Cross (Vector3 left, Vector3 right)	public void Add (Vector3 source)	public static Vector3 Add (Vector3 left, Vector3 right)	public void Subtract (Vector3 source)	public static Vector3 Subtract (Vector3 left, Vector3 right)	public void Minimize (Vector3 source)	public static Vector3 Minimize (Vector3 left, Vector3 right)	public void Maximize (Vector3 source)	public static Vector3 Maximize (Vector3 left, Vector3 right)	public void Scale (float scalingFactor)	public static Vector3 Scale (Vector3 source, float scalingFactor)	public static Vector3 Lerp (Vector3 left, Vector3 right, float interpolater)	public void Normalize ()	public static Vector3 Normalize (Vector3 source)	public static Vector3 Hermite (Vector3 position, Vector3 tangent, Vector3 position2, Vector3 tangent2, float weightingFactor)	public static Vector3 CatmullRom (Vector3 position1, Vector3 position2, Vector3 position3, Vector3 position4, float weightingFactor)	public static Vector3 BaryCentric (Vector3 v1, Vector3 v2, Vector3 v3, float f, float g)	public static Vector4[] Transform (Vector3[] vector, Matrix sourceMatrix)	public static Vector4 Transform (Vector3 source, Matrix sourceMatrix)	public void TransformCoordinate (Matrix sourceMatrix)	public static Vector3[] TransformCoordinate (Vector3[] vector, Matrix sourceMatrix)	public static Vector3 TransformCoordinate (Vector3 source, Matrix sourceMatrix)	public void TransformNormal (Matrix sourceMatrix)	public static Vector3[] TransformNormal (Vector3[] vector, Matrix sourceMatrix)	public static Vector3 TransformNormal (Vector3 source, Matrix sourceMatrix)	public void Project (object viewport, Matrix projection, Matrix view, Matrix world)	public static Vector3 Project (Vector3 v, object viewport, Matrix projection, Matrix view, Matrix world)	public void Unproject (object viewport, Matrix projection, Matrix view, Matrix world)	public static Vector3 Unproject (Vector3 v, object viewport, Matrix projection, Matrix view, Matrix world)}
I meant something similar to http://msdn2.microsoft.com/en-us/library/system.windows.vector.aspx but thats in .net framework 3 and I am using .net framework 2 so I am trying to make my own similar to it.

[Edited by - gp_s on March 19, 2008 12:17:31 AM]
I really wouldn't recommend rolling your own unless you are pretty confident with linear algebra. That said -- you can always get previous versions of the DirectX SDK (so you can have the managed stuff you pointed at, which was August 2005). Meanwhile, you could always try slimDX that was created by some gamedev regulars after MDX went belly-up :)

hth

~Shiny
------------'C makes it easy to shoot yourself in the foot. C++ makes it harder, but when you do, it blows away your whole leg.' -Bjarne Stroustrup
I am trying the Vector2 in DirectX, I assume thats similar to Vector in .net framework 3? I do have some problems, I couldn't divided a vector2 with another number so I jut multiplied it with 1/ the number which worked but now I have a much more difficult problem, I cant MOD two vector2s.
What I did when I needed to roll my own vector and matrix classes was look at the math used by established libraries such as ogre (or better yet axiom, since it's already c#).

Quote:Original post by gp_s
I meant something similar to http://msdn2.microsoft.com/en-us/library/bb324325.aspx but thats in .net framework 3 and I am using .net framework 2 so I am trying to make my own similar to it.


Where are you getting the notion that this class is part of the .net 3.0 framework? That's the Vector3 class from managed directx. You can use it from .net 1.1 on.

[Edited by - gharen2 on March 18, 2008 12:10:46 PM]
woops! I meant something similar to http://msdn2.microsoft.com/en-us/library/system.windows.vector.aspx

edited the other post now...
Not quite sure what mod means for vector maths? Anyway, I'm sure there are a million of these kicking around and at least 750,000 of those will be better than this one, but here's one I slung together when I needed some of this stuff:
	public struct Vector {		public double X, Y, Z; // in 1/1000				public Vector(double x, double y, double z){			X = x; Y = y; Z = z;		}				public override string ToString(){			return "[" + X + ", " + Y + ", " + Z + "]";		}				public static Vector[] MakeArray(Vector v, int len){			Vector[] r = new Vector[len];			for(int i = 0 ; i < len; i++) r = v;			return r;		}				public static Vector Add(Vector v1, Vector v2, double c1, double c2){ return Add(new Vector[]{v1}, new Vector[]{v2}, c1, c2)[0]; }		public static Vector[] Add(Vector[] v1, Vector v2, double c1, double c2){ return Add(v1, MakeArray(v2, v1.Length), c1, c2); }		public static Vector[] Add(Vector v1, Vector[] v2, double c1, double c2){ return Add(v2, MakeArray(v1, v2.Length), c2, c1); }		public static Vector[] Add(Vector[] v1, Vector[] v2, double c1, double c2){			Vector[] r = new Vector[v1.Length];			for(int i = 0; i < r.Length; i++){				r = new Vector((c1 * v1.X + c2 * v2.X),			                  (c1 * v1.Y + c2 * v2.Y),			                  (c1 * v1.Z + c2 * v2.Z));			}			return r;		}						public static Vector CrossProduct(Vector v1, Vector v2){			return new Vector(			                  (v1.Y * v2.Z) - (v1.Z * v2.Y),			                  (v1.Z * v2.X) - (v1.X * v2.Z),			                  (v1.X * v2.Y) - (v1.Y * v2.X)			                 );		}				public static double DotProduct(Vector v1, Vector v2){			return (v1.X * v2.X) + (v1.Y * v2.Y) + (v1.Z * v2.Z);		}				public double Magnitude {			get {				return Math.Sqrt((X*X) + (Y*Y) + (Z*Z));			}			set {				double fac = value / Magnitude;				X *= fac; Y *= fac; Z *= fac;			}		}				public static Vector operator +(Vector v1, Vector v2){ return Add(v1, v2, 1, 1); }		public static Vector operator -(Vector v1, Vector v2){ return Add(v1, v2, 1, -1); }		public static Vector operator *(double d, Vector v){ return new Vector(d*v.X, d*v.Y, d*v.Z); }		public static Vector operator *(Vector v, double d){ return new Vector(d*v.X, d*v.Y, d*v.Z); }		public static Vector operator /(Vector v, double d){ return new Vector(v.X/d, v.Y/d, v.Z/d); }	}A MagSquared is also useful in many cases but apparently in this implementation I didn't need one of those.

This topic is closed to new replies.

Advertisement