Vec.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. namespace Day23;
  2. public record struct Vec
  3. {
  4. public static Vec Zero { get; } = new Vec(0, 0);
  5. public static Vec Up { get; } = new Vec(0, -1);
  6. public static Vec Down { get; } = new Vec(0, 1);
  7. public static Vec Left { get; } = new Vec(-1, 0);
  8. public static Vec Right { get; } = new Vec(1, 0);
  9. public long X { get; }
  10. public long Y { get; }
  11. public bool IsHorizontal => Y == 0;
  12. public bool IsVertical => X == 0;
  13. public Vec(long x, long y)
  14. {
  15. X = x;
  16. Y = y;
  17. }
  18. public Vec RotatePositive()
  19. {
  20. // clockwise
  21. return new Vec(-1 * Y, X);
  22. }
  23. public Vec RotateNegative()
  24. {
  25. // counter-clockwise
  26. return new Vec(Y, -1 * X);
  27. }
  28. public Vec Add(Vec other)
  29. {
  30. return new Vec(X + other.X, Y + other.Y);
  31. }
  32. public Vec Subtract(Vec other)
  33. {
  34. return new Vec(X - other.X, Y - other.Y);
  35. }
  36. public Vec Multiply(int f)
  37. {
  38. return new Vec(f * X, f * Y);
  39. }
  40. public long Cross(Vec other)
  41. {
  42. return X * other.Y - other.X * Y;
  43. }
  44. }