/// <summary> /// Checks whether the specified line segment intersects the passed one. /// </summary> /// <remarks> /// See http://www.wyrmtale.com/blog/2013/115/2d-line-intersection-in-c for details. /// </remarks> /// <param name="first">Line segment to check.</param> /// <param name="second">Line segment to check.</param> /// <param name="intersectionPoint">Intersection point, if found.</param> /// <returns> /// <c>true</c>, if both line segments intersect each other, and <c>false</c> otherwise. /// </returns> public static bool Intersects(this LineSegment2F first, LineSegment2F second, out Vector2F intersectionPoint) { // Get A,B,C of first line - points ps1 to pe1. var a1 = first.Q.Y - first.P.Y; var b1 = first.P.X - first.Q.X; var c1 = a1 * first.P.X + b1 * first.P.Y; // Get A,B,C of second line - points ps2 to pe2. var a2 = second.Q.Y - second.P.Y; var b2 = second.P.X - second.Q.X; var c2 = a2 * second.P.X + b2 * second.P.Y; // Get delta and check if the lines are parallel. var delta = a1 * b2 - a2 * b1; if (Math.Abs(delta) < float.Epsilon) { intersectionPoint = Vector2F.Zero; return false; } // Return intersection point. intersectionPoint = new Vector2F((b2 * c1 - b1 * c2) / delta, (a1 * c2 - a2 * c1) / delta); return first.Contains(intersectionPoint) && second.Contains(intersectionPoint); }
/// <summary> /// Checks whether the specified line segment intersects the passed one. /// </summary> /// <remarks> /// See http://jeffe.cs.illinois.edu/teaching/373/notes/x06-sweepline.pdf for details. /// </remarks> /// <param name="first">Line segment to check.</param> /// <param name="second">Line segment to check.</param> /// <returns> /// <c>true</c>, if both line segments intersect each other, and <c>false</c> otherwise. /// </returns> public static bool Intersects(this LineSegment2F first, LineSegment2F second) { // By definition, line segments that share a point can't intersect. if (first.P == second.P || first.P == second.Q || first.Q == second.P || first.Q == second.Q) { return false; } var a = first.P; var b = first.Q; var c = second.P; var d = second.Q; if (Vector2F.CounterClockwise(a, c, d) == Vector2F.CounterClockwise(b, c, d)) { return false; } if (Vector2F.CounterClockwise(a, b, c) == Vector2F.CounterClockwise(a, b, d)) { return false; } return true; }
public void TestPolygonDoesNotContainIntersectingLineSegment() { // ARRANGE. var line = new LineSegment2F(new Vector2F(2, 3), new Vector2F(6, 3)); // ASSERT. Assert.IsFalse(this.polygon.Contains(line)); }
public void TestPolygonContainsLineSegment() { // ARRANGE. var line = new LineSegment2F(new Vector2F(2, 3), new Vector2F(4, 3)); // ASSERT. Assert.IsTrue(this.polygon.Contains(line)); }
public void GetLength() { var s = new LineSegment2F { Point1 = new Vector2F(1, 2), Point2 = new Vector2F(-1, 9), }; Assert.IsTrue(Numeric.AreEqual((s.Point2 - s.Point1).Length, s.GetLength(0, 1, 100, Numeric.EpsilonF))); Assert.IsTrue(Numeric.AreEqual((s.Point2 - s.Point1).Length * 0.3f, s.GetLength(0.6f, 0.3f, 100, Numeric.EpsilonF))); Assert.IsTrue(Numeric.AreEqual((s.Point2 - s.Point1).Length * 0.3f, s.GetLength(0.1f, 0.4f, 100, Numeric.EpsilonF))); }
public void GetTangent() { var s = new LineSegment2F { Point1 = new Vector2F(1, 2), Point2 = new Vector2F(-1, 9), }; Assert.IsTrue(Vector2F.AreNumericallyEqual(s.Point2 - s.Point1, s.GetTangent(0))); Assert.IsTrue(Vector2F.AreNumericallyEqual(s.Point2 - s.Point1, s.GetTangent(0.3f))); Assert.IsTrue(Vector2F.AreNumericallyEqual(s.Point2 - s.Point1, s.GetTangent(1))); }
public void Test() { var s = new LineSegment2F { Point1 = new Vector2F(1, 2), Point2 = new Vector2F(-1, 9), }; Assert.IsTrue(Vector2F.AreNumericallyEqual(s.Point1, s.GetPoint(0))); Assert.IsTrue(Vector2F.AreNumericallyEqual(s.Point2, s.GetPoint(1))); Assert.IsTrue(Vector2F.AreNumericallyEqual(s.Point1 * 0.7f + s.Point2 * 0.3f, s.GetPoint(0.3f))); }
public void Flatten() { var s = new LineSegment2F { Point1 = new Vector2F(1, 2), Point2 = new Vector2F(-1, 9), }; var points = new List<Vector2F>(); s.Flatten(points, 1, 1); Assert.AreEqual(2, points.Count); Assert.IsTrue(points.Contains(s.Point1)); Assert.IsTrue(points.Contains(s.Point2)); }
public void Flatten() { var s = new LineSegment2F { Point1 = new Vector2F(1, 2), Point2 = new Vector2F(-1, 9), }; var points = new List <Vector2F>(); s.Flatten(points, 1, 1); Assert.AreEqual(2, points.Count); Assert.IsTrue(points.Contains(s.Point1)); Assert.IsTrue(points.Contains(s.Point2)); }
public void TestLineIntersectsCircle() { // ARRANGE. var line = new LineSegment2F(new Vector2F(-2.0f, 0.0f), new Vector2F(2.0f, 0.0f)); var circle = CircleF.UnitCircle; Vector2F?first; Vector2F?second; // ACT. var intersects = line.Intersects(circle, out first, out second); // ASSERT. Assert.IsTrue(intersects); Assert.IsNotNull(first); Assert.IsNotNull(second); Assert.AreEqual(new Vector2F(1.0f, 0.0f), first); Assert.AreEqual(new Vector2F(-1.0f, 0.0f), second); }
public void TestLineIntersectsCircle() { // ARRANGE. var line = new LineSegment2F(new Vector2F(-2.0f, 0.0f), new Vector2F(2.0f, 0.0f)); var circle = CircleF.UnitCircle; Vector2F? first; Vector2F? second; // ACT. var intersects = line.Intersects(circle, out first, out second); // ASSERT. Assert.IsTrue(intersects); Assert.IsNotNull(first); Assert.IsNotNull(second); Assert.AreEqual(new Vector2F(1.0f, 0.0f), first); Assert.AreEqual(new Vector2F(-1.0f, 0.0f), second); }
/// <summary> /// Checks whether the specified rectangle intersects the passed circle. /// </summary> /// <param name="rectangle"> /// Rectangle to check. /// </param> /// <param name="circle"> /// Circle to check. /// </param> /// <returns> /// <c>true</c>, if rectangle and circle intersect each other, and <c>false</c> otherwise. /// </returns> public static bool Intersects(this RectangleF rectangle, CircleF circle) { // Check if rectangle contains center. if (rectangle.Contains(circle.Center)) { return true; } // Check each edge. var topLeft = new Vector2F(rectangle.X, rectangle.Y); var bottomLeft = new Vector2F(rectangle.X, rectangle.MaxY); var topRight = new Vector2F(rectangle.MaxX, rectangle.Y); var bottomRight = new Vector2F(rectangle.MaxX, rectangle.MaxY); var left = new LineSegment2F(topLeft, bottomLeft); var right = new LineSegment2F(topRight, bottomRight); var top = new LineSegment2F(topLeft, topRight); var bottom = new LineSegment2F(bottomLeft, bottomRight); return left.Intersects(circle) || right.Intersects(circle) || top.Intersects(circle) || bottom.Intersects(circle); }
public void TestPolygonDoesNotContainOutsideLineSegment() { // ARRANGE. var line = new LineSegment2F(new Vector2F(2, 7), new Vector2F(4, 7)); // ASSERT. Assert.IsFalse(this.polygon.Contains(line)); }