* Vector2 now uses the new operator overloading

* sync: Rect changes to width/height style

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1515 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
trass3r 2010-04-20 20:50:15 +00:00
parent eada18b7e5
commit b4ff671e6e
4 changed files with 98 additions and 161 deletions

View file

@ -27,74 +27,56 @@
module dsfml.system.vector2;
/**
* Vector2 is an utility class for manipulating 2 dimensional
* vectors. Template parameter defines the type of coordinates
* (integer, float, ...)
*/
* Vector2 is an utility class for manipulating 2 dimensional
* vectors. Template parameter defines the type of coordinates
* (integer, float, ...)
*/
struct Vector2(T)
{
T x;
T y;
/// unary (-) overload
Vector2 opNeg()
/// negate the vector
Vector2 opUnary(string op : "-")()
{
return Vector2!(T)(-x, -y);
}
/// (+=) overload
Vector2 opAddAssign(Vector2 other)
/// dot product
T opBinary(string op : "*", U:Vector2)(U v)
{
x += other.x;
y += other.y;
return this;
return x*v.x + y*v.y;
}
/// (-=) overload
Vector2 opSubAssign(Vector2 other)
/// element-wise operations, +, -,
Vector2 opBinary(string op, U:Vector2)(U v)
if (op != "*")
{
x -= other.x;
y -= other.y;
return this;
}
/// (+) overload
Vector2 opAdd(Vector2 other)
{
return Vector2!(T)( cast(T)(x + other.x), cast(T)(y + other.y) );
}
/// (-) overload
Vector2 opSub(Vector2 other)
{
return Vector2!(T) ( cast(T)(x - other.x), cast(T)(y - other.y) );
// pragma(msg, "opBinary!"~op);
mixin("return Vector2!(T)( cast(T)(x " ~ op ~ " v.x), cast(T)(y " ~ op ~ " v.y) );");
}
/// (*) overload
Vector2 opMul(int i)
/// operations with a scalar
Vector2 opBinary(string op)(int i)
{
return Vector2!(T) ( cast(T)(x * i), cast(T)(y * i) );
mixin("return Vector2!(T) ( cast(T)(x " ~ op ~ " i), cast(T)(y " ~ op ~ " i) );");
}
/// (*=) overload
Vector2 opMulAssign(int i)
/// element-wise assign operations, +=, -=, ...
Vector2 opOpAssign(string op, U:Vector2)(U v)
{
x *= i;
y *= i;
mixin("x " ~ op ~ " v.x;");
mixin("y " ~ op ~ " v.y;");
return this;
}
/// (/) overload
Vector2 opDiv(int i)
{
return Vector2!(T) ( cast(T)(x / i), cast(T)(y / i));
}
/// (/=) overload
Vector2 opDivAssign(int i)
/// (*=) overload
Vector2 opOpAssign(string op)(int i)
{
x /= i;
y /= i;
mixin("x "~op~" i;");
mixin("y "~op~" i;");
return this;
}
@ -110,44 +92,41 @@ struct Vector2(T)
}
}
version (UnitTest)
unittest
{
unittest
{
Vector2f main = Vector2f(10f, 10f);
Vector2f other = Vector2f(10f, 10f);
Vector2f result;
result = -main;
assert (result == Vector2f(-10.f, -10.f) );
result = main;
result += other;
assert (result == Vector2f(20.f, 20.f));
result = main;
result -= other;
assert (result == Vector2f(0.f, 0.f));
result = main + other;
assert (result == Vector2f(20.f, 20.f));
result = main - other;
assert (result == Vector2f(0.f, 0.f));
result = main * 10;
assert (result == Vector2f(100.f, 100.f));
result *= 2;
assert (result == Vector2f(200.f, 200.f));
result = main / 2;
assert (result == Vector2f(5.f, 5.f));
result = main;
result /= 2;
assert (result == Vector2f(5.f, 5.f));
}
Vector2f main = Vector2f(10f, 10f);
Vector2f other = Vector2f(10f, 10f);
Vector2f result;
result = -main;
assert (result == Vector2f(-10.f, -10.f) );
result = main;
result += other;
assert (result == Vector2f(20.f, 20.f));
result = main;
result -= other;
assert (result == Vector2f(0.f, 0.f));
result = main + other;
assert (result == Vector2f(20.f, 20.f));
result = main - other;
assert (result == Vector2f(0.f, 0.f));
result = main * 10;
assert (result == Vector2f(100.f, 100.f));
result *= 2;
assert (result == Vector2f(200.f, 200.f));
result = main / 2;
assert (result == Vector2f(5.f, 5.f));
result = main;
result /= 2;
assert (result == Vector2f(5.f, 5.f));
}
/// Aliases