Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module util.vec2;
- import std.string;
- import std.stdio;
- class Vec2
- {
- public:
- double x = 0;
- double y = 0;
- unittest {
- assert(new Vec2(2, 2) == new Vec2(2, 2));
- assert(new Vec2(0, 0) != new Vec2(2, 2));
- Vec2 testVec = new Vec2(2, 1);
- Vec2 testVecMod = -testVec;
- assert(testVecMod == new Vec2(-2, -1));
- testVecMod = +testVec;
- assert(testVecMod == testVec);
- assert(!new Vec2(double.nan, double.nan).valid);
- assert(!new Vec2(double.nan, 0).valid);
- assert(new Vec2(0, 0).valid);
- testVec += testVecMod;
- assert(testVec.x == 4 && testVec.y == 2);
- Vec2 assign = [1.0, 2.0];
- }
- this() {
- }
- this(double x1, double y1) {
- x = x1;
- y = y1;
- }
- public Vec2 opUnary(string s)() if (s == "-") {
- assert(valid);
- return new Vec2(-x, -y);
- }
- public Vec2 opUnary(string s)() if (s == "+") {
- assert(valid);
- return new Vec2(x, y);
- }
- public override bool opEquals(Object b) {
- Vec2 bVec = cast(Vec2)b;
- return x == bVec.x && y == bVec.y;
- }
- public void opAssign(double[] vecArr) {
- assert(vecArr.length == 2);
- x = vecArr[0];
- y = vecArr[1];
- }
- void opOpAssign(string op)(Vec2 vec2)
- {
- mixin("x " ~ op ~ "= vec2.x;");
- mixin("y " ~ op ~ "= vec2.y;");
- }
- void opOpAssign(string op)(double[] vecArr)
- {
- assert(vecArr.length == 2);
- mixin("x " ~ op ~ "= vecArr[0];");
- mixin("y " ~ op ~ "= vecArr[1];");
- }
- override string toString() {
- return format("[x = %f, y = %f]", x, y);
- }
- @property {
- bool valid() {
- return !(x is x.nan) && !(y is y.nan);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement