// Code from: http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/
        public Vector? Intersection(LineSegment that)
        {
            double d =
             (that.V2.Y - that.V1.Y) * (this.V2.X - this.V1.X)
             -
             (that.V2.X - that.V1.X) * (this.V2.Y - this.V1.Y);

              double n_a =
             (that.V2.X - that.V1.X) * (this.V1.Y - that.V1.Y)
             -
             (that.V2.Y - that.V1.Y) * (this.V1.X - that.V1.X);

              double n_b =
             (this.V2.X - this.V1.X) * (this.V1.Y - that.V1.Y)
             -
             (this.V2.Y - this.V1.Y) * (this.V1.X - that.V1.X);

              // parallel lines
              if (d == 0)
             return null;

              double ua = n_a / d;
              double ub = n_b / d;

              if (ua >= 0d && ua <= 1d && ub >= 0d && ub <= 1d)
              {
             double x = this.V1.X + (ua * (this.V2.X - this.V1.X));
             double y = this.V1.Y + (ua * (this.V2.Y - this.V1.Y));
             return Vector.FromCart(x, y);
              }
              return null;
        }
Example #2
0
 internal Tuple<Player, Vector> intersect(LineSegment ls1, IEnumerable<Tuple<Player, IEnumerable<LineSegment>>> tanks)
 {
     return (from tank in tanks
        from ls2 in tank.Item2
        let intersection = ls1.Intersection(ls2)
        where intersection != null
        select new Tuple<Player, Vector>(tank.Item1, (Vector)intersection)
       ).FirstOrDefault();
 }