vector.d 801 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. module vector;
  2. import std.format;
  3. import std.exception;
  4. struct Vector {
  5. int x;
  6. int y;
  7. this(int x, int y) {
  8. enforce(x >= 0, format("X value %s must not be negative", x));
  9. enforce(y >= 0, format("Y value %s must not be negative", y));
  10. this.x = x;
  11. this.y = y;
  12. }
  13. this(int size) {
  14. enforce(size >= 0, format("Size value %s must not be negative", size));
  15. this.x = size;
  16. this.y = size;
  17. }
  18. string toString() const {
  19. return format("(%s, %s)", x, y);
  20. }
  21. static Vector block(int x, int y) {
  22. return Vector(x * 2, y);
  23. }
  24. void scale(int scalar) {
  25. enforce(scalar >= 0, format("Scalar value %s must not be negative", scalar));
  26. this.x *= scalar;
  27. this.y *= scalar;
  28. }
  29. }