|
using System;
namespace Bornander.IconMenu
{
public class Vector
{
public static readonly Vector Empty = new Vector();
private float x = 0.0f;
private float y = 0.0f;
public Vector()
{
}
public Vector(float x, float y)
{
this.x = x;
this.y = y;
}
public Vector(Vector vector)
{
this.x = vector.x;
this.y = vector.y;
}
public void Normalize()
{
float length = Length;
if (length > 0.0f)
{
x /= length;
y /= length;
}
else
{
x = 0.0f;
y = 0.0f;
}
}
public void Set(int x, int y)
{
this.x = x;
this.y = y;
}
#region Public properties
public float Length
{
get { return (float)Math.Sqrt(x * x + y * y); }
}
public float X
{
get { return x; }
set { x = value; }
}
public float Y
{
get { return y; }
set { y = value; }
}
#endregion
#region Public static methods/operators
public static Vector Normalize(Vector vector)
{
float length = vector.Length;
if (length > 0.0f)
return new Vector(vector.x / length, vector.y / length);
else
return Vector.Empty;
}
public static Vector operator +(Vector lhs, Vector rhs)
{
return new Vector(lhs.x + rhs.x, lhs.y + rhs.y);
}
public static Vector operator -(Vector lhs, Vector rhs)
{
return new Vector(lhs.x - rhs.x, lhs.y - rhs.y);
}
public static Vector operator *(Vector lhs, float rhs)
{
return new Vector(lhs.x * rhs, lhs.y * rhs);
}
public static float Dot(Vector lhs, Vector rhs)
{
return lhs.x * rhs.x + lhs.y * rhs.y;
}
public static float DistanceBetween(Vector lhs, Vector rhs)
{
return (lhs - rhs).Length;
}
public static explicit operator Vector(System.Drawing.Point point)
{
return new Vector(point.X, point.Y);
}
public static explicit operator System.Drawing.Point(Vector vector)
{
return new System.Drawing.Point((int)vector.x, (int)vector.y);
}
#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.
Article videos
Oakmead Apps Android Games
21 Feb 2014: Best VB.NET Article of January 2014 - Second Prize
18 Oct 2013: Best VB.NET article of September 2013
23 Jun 2012: Best C++ article of May 2012
20 Apr 2012: Best VB.NET article of March 2012
22 Feb 2010: Best overall article of January 2010
22 Feb 2010: Best C# article of January 2010