Line.cs 695 B

12345678910111213141516
  1. namespace Day24;
  2. public record Line(Vec Position, Vec Velocity)
  3. {
  4. public Intersection IntersectionWith(Line other)
  5. {
  6. var a = (Position.Y - other.Position.Y) / (double)other.Velocity.Y;
  7. var b = (other.Position.X - Position.X) * ((double)Velocity.Y / (Velocity.X * other.Velocity.Y));
  8. var c = (other.Velocity.X * Velocity.Y) / (double)(Velocity.X * other.Velocity.Y);
  9. var t2 = (a + b) / (1 - c);
  10. var t1 = ((other.Position.X - Position.X) + t2 * other.Velocity.X) / (double)Velocity.X;
  11. var v2 = other.Velocity.Multiply(t2);
  12. return new Intersection(t1, t2, other.Position.X + v2.X, other.Position.Y + v2.Y);
  13. }
  14. }