Vec.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. namespace Day18;
  2. public record struct Vec
  3. {
  4. public long X { get; }
  5. public long Y { get; }
  6. public bool IsHorizontal => Y == 0;
  7. public bool IsVertical => X == 0;
  8. public Vec(long x, long y)
  9. {
  10. X = x;
  11. Y = y;
  12. }
  13. public Vec RotatePositive()
  14. {
  15. // clockwise
  16. return new Vec(-1 * Y, X);
  17. }
  18. public Vec RotateNegative()
  19. {
  20. // counter-clockwise
  21. return new Vec(Y, -1 * X);
  22. }
  23. public Vec Add(Vec other)
  24. {
  25. return new Vec(X + other.X, Y + other.Y);
  26. }
  27. public Vec Multiply(int f)
  28. {
  29. return new Vec(f * X, f * Y);
  30. }
  31. public long Cross(Vec other)
  32. {
  33. return X * other.Y - other.X * Y;
  34. }
  35. public static Vec Direction(char c)
  36. {
  37. switch (c)
  38. {
  39. case 'R':
  40. return new Vec(1, 0);
  41. case 'L':
  42. return new Vec(-1, 0);
  43. case 'U':
  44. return new Vec(0, -1);
  45. case 'D':
  46. return new Vec(0, 1);
  47. }
  48. return default;
  49. }
  50. }