I’m presently attempting to write a few classes to aid me in writing various applications and/or games that are primarily 2D. One of the things I really needed (since the C# XNA “Point” class sucks) was an IntVector2 class, which is meant to be very similar to the standard XNA “Vector2” class.
Presently, I have this coding (more stuff after the break):
using System;
using Microsoft.Xna.Framework;
namespace LPG.Utilities
{
public class IntVector2
{
//Properties
public readonly int X = 0;
public readonly int Y = 0;
private float magnitude = 0;
//Special properties
public float Magnitude
{
get
{
if (this.magnitude == 0)
{
return (float)Math.Sqrt((Math.Abs(this.X ^ 2) + Math.Abs(this.Y ^ 2)));
}
else
{
return this.magnitude;
}
}
}
//Methods
public Vector2 Normalize()
{
return new Vector2(this.X / (float)this.Magnitude, this.Y / (float)this.Magnitude);
}
//Constructors
public IntVector2()
{
}
public IntVector2(int Both)
{
this.X = Both;
this.Y = Both;
}
public IntVector2(int X, int Y)
{
this.X = X;
this.Y = Y;
}
//Casts
public static implicit operator Vector2(IntVector2 From)
{
return new Vector2(From.X, From.Y);
}
public static implicit operator IntVector2(Vector2 From)
{
return new IntVector2((int)From.X, (int)From.Y);
}
public static implicit operator string(IntVector2 From)
{
return "(" + From.X + ", " + From.Y + ")";
}
//IntVector2-IntVector2 operators
public static IntVector2 operator +(IntVector2 First, IntVector2 Second)
{
return new IntVector2(First.X + Second.X, First.Y + Second.Y);
}
public static IntVector2 operator -(IntVector2 First, IntVector2 Second)
{
return new IntVector2(First.X - Second.X, First.Y - Second.Y);
}
public static IntVector2 operator *(IntVector2 First, IntVector2 Second)
{
return new IntVector2(First.X * Second.X, First.Y * Second.Y);
}
public static IntVector2 operator /(IntVector2 First, IntVector2 Second)
{
return new IntVector2(First.X / Second.X, First.Y / Second.Y);
}
//IntVector2-int operators:
public static IntVector2 operator +(IntVector2 First, int Second)
{
return new IntVector2(First.X + Second, First.Y + Second);
}
public static IntVector2 operator -(IntVector2 First, int Second)
{
return new IntVector2(First.X - Second, First.Y - Second);
}
public static IntVector2 operator *(IntVector2 First, int Second)
{
return new IntVector2(First.X * Second, First.Y * Second);
}
public static IntVector2 operator /(IntVector2 First, int Second)
{
return new IntVector2(First.X / Second, First.Y / Second);
}
}
}
What should I do to improve this class? I thought about using a struct instead of a class, but I’m not quite experienced enough to make a real determination.
Thanks in advance.
I would recommend a struct if you’re going to be newing it a lot, since then it won’t contribute to garbage collection, which if you’re developing on the xbox is a bit issue.
Your magnitude method returns a float, no need to convert it when used in Normalize.
You’re not storing the magnitude when you calculate it.
You could add methods for dot and cross product, however not sure they’d be useful for integer vectors.
You could create an override for
ToString(great for when debugging/printing to screen etc.), as well as==EqualsandGetHashCode.I’m not sure i like having an explicit conversion operator for
string. I think that should be whatToStringis for.