示例#1
0
		public static bool Intersect(Segment s0, Segment s1, out Intersection2D interObj)
		{ 
		//    (Ay-Cy)(Dx-Cx)-(Ax-Cx)(Dy-Cy)
		//r = -----------------------------  (eqn 1)
		//    (Bx-Ax)(Dy-Cy)-(By-Ay)(Dx-Cx)

		//    (Ay-Cy)(Bx-Ax)-(Ax-Cx)(By-Ay)
		//s = -----------------------------  (eqn 2)
		//    (Bx-Ax)(Dy-Cy)-(By-Ay)(Dx-Cx)

            double den = (s0.P1.X - s0.P0.X) * (s1.P1.Y - s1.P0.Y) - (s0.P1.Y - s0.P0.Y) * (s1.P1.X - s1.P0.X);
            double r = (s0.P0.Y - s1.P0.Y) * (s1.P1.X - s1.P0.X) - (s0.P0.X - s1.P0.X) * (s1.P1.Y - s1.P0.Y);
            double s = (s0.P0.Y - s1.P0.Y) * (s0.P1.X - s0.P0.X) - (s0.P0.X - s1.P0.X) * (s0.P1.Y - s0.P0.Y);

			// If the denominator in eqn 1 is zero, AB & CD are parallel
			if (System.Math.Abs(den) < MathFunctions.EpsilonF)
			{
				// If the numerator in eqn 1 is also zero, AB & CD are collinear.
				if (System.Math.Abs(r) < MathFunctions.EpsilonF)
				{
					Interval i0 = new Interval(Interval.Type.Closed, System.Math.Min(s0.P0.X, s0.P1.X), System.Math.Max(s0.P0.X, s0.P1.X));
					Interval i1 = new Interval(Interval.Type.Closed, System.Math.Min(s1.P0.X, s1.P1.X), System.Math.Max(s1.P0.X, s1.P1.X));
					
					// check if interval overlaps
					if ((i0.Max < i1.Min) || (i1.Max < i0.Min))
					{
						interObj = new Intersection2D();
						return false;
					}
					else
					{
						Interval i2 = new Interval(Interval.Type.Closed, System.Math.Max(i0.Min, i1.Min), System.Math.Min(i0.Max, i1.Max));
						interObj = new Intersection2D(new Segment());
						return true;
					}
				}
				else
				{
					interObj = new Intersection2D();
					return false;
				}
			}
			else
			{ 
				r /= den;
				s /= den;
				// Let P be the position vector of the intersection point, then
				// P=A+r(B-A)
				if (0 <= r && r <= 1 && 0 <= s && s <= 1)
				{
					interObj = new Intersection2D(new Vector2D(s0.P0+r*(s0.P1-s0.P0)));
					return true;
				}
			}
			interObj = new Intersection2D();
			return false;
		}
示例#2
0
		/// <summary>
		/// Initializes a new instance of the <see cref="Interval"/> class using a given <see cref="Interval"/> instance.
		/// </summary>
		/// <param name="interval">An <see cref="Interval"/> instance.</param>
		public Interval(Interval interval)
		{
			_type = interval.IntervalType;
			_min = interval.Min;
			_max = interval.Max;
		}