示例#1
0
        public bool withinExpandedObject(PointF pointToExplore)
        {
            IList <PointF> points = getObstacleExpandedPoints();

            if (points.Contains(pointToExplore))
            {
                return(false);
            }
            float  maxX    = points.Max(s => s.X) + 2;
            int    counter = 0;
            PointF last    = points.Last();

            foreach (PointF next in points)
            {
                if (LineIntersection.SegmentsIntersect(
                        new LineSegment(pointToExplore, new PointF(maxX, pointToExplore.Y)),
                        new LineSegment(last, next)))
                {
                    counter++;
                }
                last = next;
            }
            if (counter % 2 == 0)
            {
                return(false);
            }
            return(true);
        }
示例#2
0
        public void Intersections()
        {
            var l1p1         = new PointF(0.0f, 0.0f);
            var l1p2         = new PointF(10.0f, 10.0f);
            var l2p1         = new PointF(0.0f, 10.0f);
            var l2p2         = new PointF(10.0F, 0.0f);
            var intersection = LineIntersection.FindIntersectionPoint(l1p1, l1p2, l2p1, l2p2);

            Assert.IsTrue(intersection.HasValue);
            Assert.AreEqual(5.0f, intersection.Value.X);
            Assert.AreEqual(5.0f, intersection.Value.Y);

            l1p1         = new PointF(0.0f, 0.0f);
            l1p2         = new PointF(10.0f, 10.0f);
            l2p1         = new PointF(0.0f, 30.0f);
            l2p2         = new PointF(30.0F, 0.0f);
            intersection = LineIntersection.FindIntersectionPoint(l1p1, l1p2, l2p1, l2p2);
            Assert.IsFalse(intersection.HasValue);

            l1p1         = new PointF(50.0f, 50.0f);
            l1p2         = new PointF(60.0f, 60.0f);
            l2p1         = new PointF(0.0f, 10.0f);
            l2p2         = new PointF(10.0F, 0.0f);
            intersection = LineIntersection.FindIntersectionPoint(l1p1, l1p2, l2p1, l2p2);
            Assert.IsFalse(intersection.HasValue);

            l1p1         = new PointF(0.0f, 0.0f);
            l1p2         = new PointF(10.0f, 10.0f);
            l2p1         = new PointF(1.0f, 0.0f);
            l2p2         = new PointF(11.0F, 10.0f);
            intersection = LineIntersection.FindIntersectionPoint(l1p1, l1p2, l2p1, l2p2);
            Assert.IsFalse(intersection.HasValue);
        }
        private static Dictionary <Point, List <Line> > getExpectedIntersections(List <Line> lines)
        {
            var result = new Dictionary <Point, HashSet <Line> >(new PointComparer());

            for (int i = 0; i < lines.Count; i++)
            {
                for (int j = i + 1; j < lines.Count; j++)
                {
                    var intersection = LineIntersection.Find(lines[i], lines[j]);

                    if (intersection != null)
                    {
                        var existing = result.ContainsKey(intersection) ?
                                       result[intersection] : new HashSet <Line>();

                        if (!existing.Contains(lines[i]))
                        {
                            existing.Add(lines[i]);
                        }

                        if (!existing.Contains(lines[j]))
                        {
                            existing.Add(lines[j]);
                        }

                        result[intersection] = existing;
                    }
                }
            }

            return(result.ToDictionary(x => x.Key, x => x.Value.ToList()));
        }
        private static int CountIntersections(List <Node> layer1, List <Node> layer2)
        {
            int intersections = 0;

            foreach (var pair1 in GetPairsBetweenLayers(layer1, layer2))
            {
                foreach (var pair2 in GetPairsBetweenLayers(layer1, layer2))
                {
                    if (pair1.Item1 == pair2.Item1 && pair1.Item2 == pair2.Item2)
                    {
                        continue;                                                           // Skip when they're the same.
                    }
                    if (LineIntersection.LinesIntersect(
                            new Vector2(pair1.Item1.X, pair1.Item1.Layer),
                            new Vector2(pair1.Item2.X, pair1.Item2.Layer),
                            new Vector2(pair2.Item1.X, pair2.Item1.Layer),
                            new Vector2(pair2.Item2.X, pair2.Item2.Layer), 0.1f))
                    {
                        intersections++;
                    }
                }
            }

            return(intersections / 2); // Each intersection will be counted twice.
        }
示例#5
0
        public void FuzzyZeroCheck()
        {
            var value = 0.1f * Constants.Epsilon;

            var result = LineIntersection.IsFuzzyZero(value);

            Assert.True(result);
        }
示例#6
0
        public void IsPointOnLineSegment_ConfirmFailsOK()
        {
            var point = new Vector2(1.0f, 10.0f);
            var v0    = new Vector2(-10.0f, 1.0f);
            var v1    = new Vector2(12.0f, 1.0f);

            var result = LineIntersection.IsPointOnLineSegment(point, v0, v1);

            Assert.False(result);
        }
示例#7
0
        public void TestPerpDistanceFromLine_NegativeLeftSide()
        {
            var p          = new Vector2(-50.0f, 50.0f);
            var dir        = new Vector2(0.0f, 1.0f);
            var pointOnRay = new Vector2(0.0f, 0.0f);

            var result = LineIntersection.PerpendicularDistanceOfPointFromInfiniteLineCWNormalIsPositiveDirection(p, dir, pointOnRay);

            Assert.Equal(-50.0f, result);
        }
示例#8
0
        public void IsPointProjectedWithinLineSegment_False()
        {
            var p  = new Vector2(101.0f, 0.0f);
            var p1 = new Vector2(-100.0f, 10.0f);
            var p2 = new Vector2(100.0f, 10.0f);

            var result = LineIntersection.IsPointProjectedWithinLineSegment(p, p1, p2);

            Assert.False(result);
        }
示例#9
0
        public double GetLineIntersectionWithRect(Line line, Rect frame)
        {
            LineIntersection helper = new LineIntersection();

            // Sides of Rect.
            //frame.X1, frame.Y1, frame.X1, frame.Y2
            //frame.X1, frame.Y2, frame.X2, frame.Y2
            //frame.X2, frame.Y2, frame.X2, frame.Y1
            //frame.X2, frame.Y1, frame.X1, frame.Y1

            Tuple <IntersectionType, double, double> left
                = helper.FindIntersectionPoint(line.RootX, line.RootY, line.EndX, line.EndY,
                                               frame.X1, frame.Y1, frame.X1, frame.Y2);
            Tuple <IntersectionType, double, double> top
                = helper.FindIntersectionPoint(line.RootX, line.RootY, line.EndX, line.EndY,
                                               frame.X1, frame.Y2, frame.X2, frame.Y2);
            Tuple <IntersectionType, double, double> right
                = helper.FindIntersectionPoint(line.RootX, line.RootY, line.EndX, line.EndY,
                                               frame.X2, frame.Y2, frame.X2, frame.Y1);
            Tuple <IntersectionType, double, double> bottom
                = helper.FindIntersectionPoint(line.RootX, line.RootY, line.EndX, line.EndY,
                                               frame.X2, frame.Y1, frame.X1, frame.Y1);

            bool foundNoIntersection
                = left.Item1 == IntersectionType.DONT_INTERSECT &&
                  top.Item1 == IntersectionType.DONT_INTERSECT &&
                  right.Item1 == IntersectionType.DONT_INTERSECT &&
                  bottom.Item1 == IntersectionType.DONT_INTERSECT;

            if (foundNoIntersection)
            {
                return(0);
            }

            bool foundAtleastOneCollinear
                = left.Item1 == IntersectionType.COLLINEAR ||
                  top.Item1 == IntersectionType.COLLINEAR ||
                  right.Item1 == IntersectionType.COLLINEAR ||
                  bottom.Item1 == IntersectionType.COLLINEAR;

            if (foundAtleastOneCollinear)
            {
                // Handle collinear cases
            }
            else
            {
                // At least one intersection is true.
                if (left.Item1 == IntersectionType.DO_INTERSECT)
                {
                }
            }

            throw new NotImplementedException();
        }
示例#10
0
        public IEnumerable <LineSegment> getVisibilityGraph()
        {
            List <LineSegment>    collisionLines    = new List <LineSegment>();
            List <PointF>         potentialVertices = new List <PointF>();
            HashSet <LineSegment> validPaths        = new HashSet <LineSegment>();

            // build list of obstacles/potential vertices
            foreach (IGraphObject gobj in _polygons)
            {
                if (gobj is Wall)
                {
                    addWallLines(collisionLines, gobj.getPoints().ToList());
                }
                else
                {
                    IList <PointF> expandedPoints = gobj.getObstacleExpandedPoints();
                    addObstacleLines(collisionLines, expandedPoints.ToList());
                    potentialVertices.AddRange(expandedPoints);
                }
            }
            // check for vertices within objects...
            foreach (Obstacle gobj in _polygons.Where(s => !(s is Wall)))
            {
                potentialVertices = potentialVertices.Where(s => !gobj.withinExpandedObject(s)).ToList();
            }
            potentialVertices.Add(_start);
            potentialVertices.Add(_goal);
            // compare all potential lines for collisions with obstacles
            foreach (PointF i in potentialVertices)
            {
                foreach (PointF j in potentialVertices)
                {
                    if (i == j)
                    {
                        continue;
                    }
                    LineSegment path      = new LineSegment(i, j);
                    bool        collision = false;
                    foreach (LineSegment line in collisionLines)
                    {
                        if (LineIntersection.SegmentsIntersect(path, line))
                        {
                            collision = true;
                            break;
                        }
                    }
                    if (!collision)
                    {
                        validPaths.Add(path);
                    }
                }
            }
            return(validPaths);
        }
示例#11
0
        public void Test_LinesIntersectTest_Colinear()
        {
            double positionX, positionY;
            bool   linesAreColinear;

            // Check colinear lines fail with the coliner flag set
            Assert.False(LineIntersection.LinesIntersect(0.0, 0.0, 10.0, 0.0,
                                                         0.0, 10.0, 10.0, 10.0, out positionX, out positionY, false, out linesAreColinear),
                         "LinesIntersect call returned true");

            Assert.True(linesAreColinear, "Lines are not colinear as expected.");
        }
示例#12
0
        public void Test_LinesIntersectTest_Intersecting()
        {
            double positionX, positionY;
            bool   linesAreColinear;

            // Check lines intersection at origin do intersect there
            Assert.True(LineIntersection.LinesIntersect(-10.0, -10.0, 10.0, 10.0,
                                                        -10.0, 10.0, 10.0, -10.0, out positionX, out positionY, false, out linesAreColinear),
                        "LinesIntersect call returned false");
            Assert.False(linesAreColinear, "Lines are unexpectedly colinear");
            Assert.True(positionX == 0.0 && positionY == 0.0, $"Intersection location incorrect = ({positionX}, {positionY})");
        }
示例#13
0
        public void FastIntersect_ConfirmParralellOverlapingNotDetected()
        {
            var a0 = new Vector2(0.0f, 2.0f);
            var a1 = new Vector2(1.0f, 2.0f);

            var b0 = new Vector2(0.1f, 2.0f);
            var b1 = new Vector2(1.2f, 2.0f);

            var result = LineIntersection.FastLineIntersect(a0, a1, b0, b1);

            Assert.False(result);
        }
示例#14
0
        public void FastIntersect_NotIntersecting()
        {
            var a0 = new Vector2(0.0f, 0.0f);
            var a1 = new Vector2(1.0f, 0.0f);

            var b0 = new Vector2(1.5f, -1.0f);
            var b1 = new Vector2(1.5f, 1.0f);

            var result = LineIntersection.FastLineIntersect(a0, a1, b0, b1);

            Assert.False(result);
        }
示例#15
0
        public void LineIntersection_Smoke_Test()
        {
            var pointComparer = new PointComparer();

            var line1 = new Line(new Point(1, 1), new Point(10, 1));
            var line2 = new Line(new Point(1, 2), new Point(10, 2));

            Assert.IsTrue(pointComparer.Equals(null, LineIntersection.Find(line1, line2)));

            line1 = new Line(new Point(10, 0), new Point(0, 10));
            line2 = new Line(new Point(0, 10), new Point(10, 10));

            Assert.IsTrue(pointComparer.Equals(new Point(0, 10), LineIntersection.Find(line1, line2)));

            line1 = new Line(new Point(0, 0), new Point(10, 10));
            line2 = new Line(new Point(0, 10), new Point(10, 10));

            Assert.IsTrue(pointComparer.Equals(new Point(10, 10), LineIntersection.Find(line1, line2)));


            line1 = new Line(new Point(10, 0), new Point(0, 10));
            line2 = new Line(new Point(0, 0), new Point(10, 10));

            Assert.IsTrue(pointComparer.Equals(new Point(5, 5), LineIntersection.Find(line1, line2)));

            line1 = new Line(new Point(-5, -5), new Point(0, 0));
            line2 = new Line(new Point(1, 1), new Point(10, 10));

            Assert.IsTrue(pointComparer.Equals(default(Point), LineIntersection.Find(line1, line2)));

            line1 = new Line(new Point(3, -5), new Point(3, 10));
            line2 = new Line(new Point(0, 5), new Point(10, 5));

            Assert.IsTrue(pointComparer.Equals(new Point(3, 5), LineIntersection.Find(line1, line2)));

            line1 = new Line(new Point(0, 5), new Point(10, 5));
            line2 = new Line(new Point(3, -5), new Point(3, 10));

            Assert.IsTrue(pointComparer.Equals(new Point(3, 5), LineIntersection.Find(line1, line2)));

            line1 = new Line(new Point(0, 5), new Point(10, 5));
            line2 = new Line(new Point(3, -5), new Point(5, 15));

            Assert.IsTrue(pointComparer.Equals(new Point(4, 5), LineIntersection.Find(line1, line2)));

            line1 = new Line(new Point(0, -5), new Point(0, 5));
            line2 = new Line(new Point(-3, 0), new Point(3, 0));

            Assert.IsTrue(pointComparer.Equals(new Point(0, 0), LineIntersection.Find(line1, line2)));
        }
示例#16
0
        public void RobustIntersect_TestingCollisionIfSharePoints()
        {
            var a0 = new Vector2(0.0f, 1.0f);
            var a1 = new Vector2(1.0f, 1.0f);

            var b0 = new Vector2(1.0f, 1.0f);
            var b1 = new Vector2(2.5f, 0.0f);

            var result = LineIntersection.RobustLineIntersectAndResult(a0, a1, b0, b1);

            Assert.True(result.Intersecting);
            Assert.False(result.CoLinear);
            Assert.Equal(1.0f, result.IntersectPoint.X);
            Assert.Equal(1.0f, result.IntersectPoint.Y);
        }
示例#17
0
        public void RobustIntersect_CoLinear_NotOverlapping_FireOnTrue()
        {
            var a0 = new Vector2(0.0f, 0.0f);
            var a1 = new Vector2(1.0f, 0.0f);

            var b0 = new Vector2(1.5f, 0.0f);
            var b1 = new Vector2(2.5f, 0.0f);

            var result = LineIntersection.RobustLineIntersectAndResult(a0, a1, b0, b1, true);

            Assert.False(result.Intersecting);
            Assert.True(result.CoLinear);
            Assert.Equal(0.0f, result.IntersectPoint.X);
            Assert.Equal(0.0f, result.IntersectPoint.Y);
        }
示例#18
0
        public void RobustIntersect_FailOnZeroLengthLines()
        {
            var a0 = new Vector2(0.0f, 0.0f);
            var a1 = new Vector2(0.0f, 0.0f);

            var b0 = new Vector2(1.0f, 0.0f);
            var b1 = new Vector2(1.0f, 0.0f);

            var result = LineIntersection.RobustLineIntersectAndResult(a0, a1, b0, b1);

            Assert.False(result.Intersecting);
            Assert.False(result.CoLinear);
            Assert.Equal(0.0f, result.IntersectPoint.X);
            Assert.Equal(0.0f, result.IntersectPoint.Y);
        }
示例#19
0
        public void RobustIntersect_NotIntersectingBasic()
        {
            var a0 = new Vector2(0.0f, 0.0f);
            var a1 = new Vector2(1.0f, 0.0f);

            var b0 = new Vector2(1.5f, -1.0f);
            var b1 = new Vector2(1.5f, 1.0f);

            var result = LineIntersection.RobustLineIntersectAndResult(a0, a1, b0, b1);

            Assert.False(result.Intersecting);
            Assert.False(result.CoLinear);
            Assert.Equal(0.0f, result.IntersectPoint.X);
            Assert.Equal(0.0f, result.IntersectPoint.Y);
        }
        public void LineIntersection_Smoke_Test()
        {
            var line1 = new Line(new Point(1, 1), new Point(10, 1));
            var line2 = new Line(new Point(1, 2), new Point(10, 2));

            Assert.AreEqual(null, LineIntersection.FindIntersection(line1, line2));

            line1 = new Line(new Point(10, 0), new Point(0, 10));
            line2 = new Line(new Point(0, 10), new Point(10, 10));

            Assert.AreEqual(new Point(0, 10), LineIntersection.FindIntersection(line1, line2));

            line1 = new Line(new Point(0, 0), new Point(10, 10));
            line2 = new Line(new Point(0, 10), new Point(10, 10));

            Assert.AreEqual(new Point(10, 10), LineIntersection.FindIntersection(line1, line2));


            line1 = new Line(new Point(10, 0), new Point(0, 10));
            line2 = new Line(new Point(0, 0), new Point(10, 10));

            Assert.AreEqual(new Point(5, 5), LineIntersection.FindIntersection(line1, line2));

            line1 = new Line(new Point(-5, -5), new Point(0, 0));
            line2 = new Line(new Point(1, 1), new Point(10, 10));

            Assert.AreEqual(default(Point), LineIntersection.FindIntersection(line1, line2));

            line1 = new Line(new Point(3, -5), new Point(3, 10));
            line2 = new Line(new Point(0, 5), new Point(10, 5));

            Assert.AreEqual(new Point(3, 5), LineIntersection.FindIntersection(line1, line2));

            line1 = new Line(new Point(0, 5), new Point(10, 5));
            line2 = new Line(new Point(3, -5), new Point(3, 10));

            Assert.AreEqual(new Point(3, 5), LineIntersection.FindIntersection(line1, line2));

            line1 = new Line(new Point(0, 5), new Point(10, 5));
            line2 = new Line(new Point(3, -5), new Point(5, 15));

            Assert.AreEqual(new Point(4, 5), LineIntersection.FindIntersection(line1, line2));

            line1 = new Line(new Point(0, -5), new Point(0, 5));
            line2 = new Line(new Point(-3, 0), new Point(3, 0));

            Assert.AreEqual(new Point(0, 0), LineIntersection.FindIntersection(line1, line2));
        }
        private static List <Point> getExpectedIntersections(List <Line> lines)
        {
            var result = new List <Point>();

            for (int i = 0; i < lines.Count; i++)
            {
                for (int j = i + 1; j < lines.Count; j++)
                {
                    var intersection = LineIntersection.FindIntersection(lines[i], lines[j]);

                    if (intersection != null)
                    {
                        result.Add(intersection);
                    }
                }
            }

            return(result);
        }
示例#22
0
        public static bool IsInside(Polygon polygon, Point point)
        {
            //a imaginary ray line from point to right infinity
            var rayLine = new Line(point, new Point(double.MaxValue, point.Y));

            var intersectionCount = 0;

            for (int i = 0; i < polygon.Edges.Count - 1; i++)
            {
                var edgeLine = polygon.Edges[i];

                if (LineIntersection.Find(rayLine, edgeLine) != null)
                {
                    intersectionCount++;
                }
            }

            //should have odd intersections if point is inside the polygon
            return(intersectionCount % 2 != 0);
        }
示例#23
0
        /// <summary>
        /// Determines if the bounds of a sub grid intersects a given triangle
        /// </summary>
        public static bool SubGridIntersectsTriangle(BoundingWorldExtent3D extents, XYZ h1, XYZ h2, XYZ v)
        {
            // If any of the triangle vertices are in the cell extents then 'yes'
            if (extents.Includes(h1.X, h1.Y) || extents.Includes(h2.X, h2.Y) || extents.Includes(v.X, v.Y))
            {
                return(true);
            }

            // If any of the sub grid corners sit in the triangle then 'yes'
            {
                if (XYZ.PointInTriangle(h1, h2, v, extents.MinX, extents.MinY) ||
                    XYZ.PointInTriangle(h1, h2, v, extents.MinX, extents.MaxY) ||
                    XYZ.PointInTriangle(h1, h2, v, extents.MaxX, extents.MaxY) ||
                    XYZ.PointInTriangle(h1, h2, v, extents.MaxX, extents.MinY))
                {
                    return(true);
                }
            }

            // If any of the extent and triangle lines intersect then also 'yes'
            if (LineIntersection.LinesIntersect(extents.MinX, extents.MinY, extents.MaxX, extents.MinY, h1.X, h1.Y, h2.X, h2.Y, out _, out _, false, out _) ||
                LineIntersection.LinesIntersect(extents.MaxX, extents.MinY, extents.MaxX, extents.MaxY, h1.X, h1.Y, h2.X, h2.Y, out _, out _, false, out _) ||
                LineIntersection.LinesIntersect(extents.MinX, extents.MaxY, extents.MaxX, extents.MaxY, h1.X, h1.Y, h2.X, h2.Y, out _, out _, false, out _) ||
                LineIntersection.LinesIntersect(extents.MinX, extents.MinY, extents.MinX, extents.MaxY, h1.X, h1.Y, h2.X, h2.Y, out _, out _, false, out _) ||

                LineIntersection.LinesIntersect(extents.MinX, extents.MinY, extents.MaxX, extents.MinY, h1.X, h1.Y, v.X, v.Y, out _, out _, false, out _) ||
                LineIntersection.LinesIntersect(extents.MaxX, extents.MinY, extents.MaxX, extents.MaxY, h1.X, h1.Y, v.X, v.Y, out _, out _, false, out _) ||
                LineIntersection.LinesIntersect(extents.MinX, extents.MaxY, extents.MaxX, extents.MaxY, h1.X, h1.Y, v.X, v.Y, out _, out _, false, out _) ||
                LineIntersection.LinesIntersect(extents.MinX, extents.MinY, extents.MinX, extents.MaxY, h1.X, h1.Y, v.X, v.Y, out _, out _, false, out _) ||

                LineIntersection.LinesIntersect(extents.MinX, extents.MinY, extents.MaxX, extents.MinY, v.X, v.Y, h2.X, h2.Y, out _, out _, false, out _) ||
                LineIntersection.LinesIntersect(extents.MaxX, extents.MinY, extents.MaxX, extents.MaxY, v.X, v.Y, h2.X, h2.Y, out _, out _, false, out _) ||
                LineIntersection.LinesIntersect(extents.MinX, extents.MaxY, extents.MaxX, extents.MaxY, v.X, v.Y, h2.X, h2.Y, out _, out _, false, out _) ||
                LineIntersection.LinesIntersect(extents.MinX, extents.MinY, extents.MinX, extents.MaxY, v.X, v.Y, h2.X, h2.Y, out _, out _, false, out _))
            {
                return(true);
            }

            // Otherwise 'no'
            return(false);
        }
示例#24
0
        /// <summary>
        /// Calculates all intercepts between the profile line and horizontal lines separating rows of cells
        /// </summary>
        /// <param name="startStation"></param>
        private void CalculateHorizontalIntercepts(double startStation)
        {
            // Find intersections of all horizontal grid rows
            int VGridLineStartIndex = (int)Math.Truncate(StartY / CellSize);
            int VGridLineEndIndex   = (int)Math.Truncate(EndY / CellSize);
            int Increment           = Math.Sign(VGridLineEndIndex - VGridLineStartIndex);

            // To find a match, tell the intersection matching method that each
            // horizontal grid line starts 'before' the StartX parameter and 'ends'
            // after the EndX parameter - use the CellSize as an arbitrary value for this.
            // This gets around an issue where with a perfectly vertical profile line,
            // no intersections were being determined.
            double HGridStartX = StartX;
            double HGridEndX   = EndX;

            if (HGridStartX > HGridEndX)
            {
                MinMax.Swap(ref HGridEndX, ref HGridStartX);
            }

            HGridStartX = HGridStartX - CellSize;
            HGridEndX   = HGridEndX + CellSize;

            int IntersectionCount = Math.Abs(VGridLineEndIndex - VGridLineStartIndex) + 1;

            while (IntersectionCount > 0 && HzIntercepts.Count < kMaxHzVtGridInterceptsToCalculate)
            {
                if (LineIntersection.LinesIntersect(StartX, StartY, EndX, EndY,
                                                    HGridStartX, VGridLineStartIndex * CellSize,
                                                    HGridEndX, VGridLineStartIndex * CellSize,
                                                    out double IntersectX, out double IntersectY, true, out bool _))
                {
                    HzIntercepts.AddPoint(IntersectX, IntersectY,
                                          startStation + MathUtilities.Hypot(StartX - IntersectX, StartY - IntersectY));
                }

                VGridLineStartIndex += Increment;
                IntersectionCount--;
            }
        }
示例#25
0
        public override void Render(Graphics graphics)
        {
            if (ShouldRender)
            {
                BoardPoint observerPosition = GetEffectiveObserverPosition();
                observerPosition = new BoardPoint(observerPosition.X * Board.ZoomFactor, -observerPosition.Y * Board.ZoomFactor);
                Vector2 screenVector = new Vector2(0, -1).Rotate(Bearing);
                Line2   bearingLine  = new Line2(observerPosition, screenVector);

                // since the bearing line is infinitely long, we'll test the intersection of the line against all four sides of the clipping
                // rectangle, and draw the line to the furthest intersection
                BoardRect  clipRect    = new BoardRect(graphics.VisibleClipBounds);
                BoardPoint endPoint    = BoardPoint.Invalid;
                double     maxDistance = 0;
                for (int i = 0; i < 4; i++)
                {
                    Line2            edge         = clipRect.GetEdge(i);
                    LineIntersection intersection = bearingLine.GetIntersectionInfo(edge);
                    if (intersection.OnSecond)
                    {
                        double distance = observerPosition.DistanceTo(intersection.Point);
                        // find the closest point on the segment to ensure that we're only considering intersections in the forward direction of the
                        // vector
                        if (distance > maxDistance && bearingLine.ClosestPointOnSegment(intersection.Point) != bearingLine.Start)
                        {
                            endPoint    = intersection.Point;
                            maxDistance = distance;
                        }
                    }
                }

                if (endPoint.IsValid)
                {
                    graphics.DrawLine(Pen, observerPosition.ToPointF(), endPoint.ToPointF());
                    graphics.DrawCircle(Pen, observerPosition.ToPointF(), 3);
                    RenderTime(graphics, (observerPosition + (endPoint - observerPosition) * 0.5).ToPointF());
                }
            }
        }
示例#26
0
        internal IList <Point> GetCorners(IList <Line> lines, GrayscaleStandardImage image)
        {
            ConcurrentBag <Point> bestCorners = new ConcurrentBag <Point>();
            var potentialCorners = new List <Point>();

            // GET BEST PAIR OF LINE
            for (int i = 0; i < lines.Count - 1; i++)
            {
                for (int j = i + 1; j < lines.Count; j++)
                {
                    var intersection = new LineIntersection(lines[i], lines[j]);

                    if (intersection.IntersectionPoint.X > 0 && intersection.IntersectionPoint.X < image.Width &&
                        intersection.IntersectionPoint.Y > 0 &&
                        intersection.IntersectionPoint.Y < image.Height)
                    {
                        bestCorners.Add(intersection.IntersectionPoint);
                    }
                }
            }

            return(bestCorners.ToList());
        }
示例#27
0
    //Adds a verticies to the triangulation
    private void AddVertexToTriangulation()
    {
        //Find a Random verticie from the todo list
        int choice = pseudoRandom.Next(0, nodesToAddList.Count);

        //set next node to selected verticies
        nextNode = nodesToAddList[choice];

        //remove selected verticies from todo list
        nodesToAddList.Remove(nextNode);

        //stores triangles created during the loop to be appended to main list after loop
        List <DTTriangle> tempTriList = new List <DTTriangle>();

        //All edges are clean at this point. Remove any that may be left over from previous loop
        edgesToCheck.Clear();

        float count = -1;

        foreach (DTTriangle aTri in triangleList)
        {
            List <DTEdge> triEdges = aTri.GetEdges();
            count++;
            //Find which triangle the current vertex being add is located within
            if (LineIntersection.PointInTraingle(nextNode.getNodePosition(), triEdges[0].getNodeA().getNodePosition(),
                                                 triEdges[0].getNodeB().getNodePosition(), triEdges[1].getNodeB().getNodePosition()))
            {
                //cache the triangle we are in so we can delete it after loop
                inTriangle = aTri;

                //create three new triangles from each edge of the triangle vertex is in to the new vertex
                foreach (DTEdge aEdge in aTri.GetEdges())
                {
                    DTTriangle nTri1 = new DTTriangle(new DTEdge(nextNode, aEdge.getNodeA()),
                                                      new DTEdge(nextNode, aEdge.getNodeB()),
                                                      new DTEdge(aEdge.getNodeB(), aEdge.getNodeA()));

                    //cache created triangles so we can add to list after loop
                    tempTriList.Add(nTri1);

                    //mark the edges of the old triangle as dirty
                    edgesToCheck.Add(new DTEdge(aEdge.getNodeA(), aEdge.getNodeB()));
                }

                break;
            }
        }

        //add the three new triangles to the triangle list
        foreach (DTTriangle aTri in tempTriList)
        {
            triangleList.Add(aTri);
        }

        //delete the old triangle that the vertex was inside of
        if (inTriangle != null)
        {
            triangleList.Remove(inTriangle);
            inTriangle.StopDraw();
            inTriangle = null;
        }

        CheckEdges(edgesToCheck);
    }
 public bool CheckOverlap2D(RoadSection testRoad)
 {
     return(LineIntersection.DoIntersect(Points, testRoad.Points));
 }
示例#29
0
        public void LineIntersection_Smoke_Test()
        {
            var line1 = new Line {
                x1 = 1, y1 = 1, x2 = 10, y2 = 1
            };
            var line2 = new Line {
                x1 = 1, y1 = 2, x2 = 10, y2 = 2
            };

            Assert.AreEqual(default(Point), LineIntersection.FindIntersection(line1, line2));

            line1 = new Line {
                x1 = 10, y1 = 0, x2 = 0, y2 = 10
            };
            line2 = new Line {
                x1 = 0, y1 = 10, x2 = 10, y2 = 10
            };

            Assert.AreEqual(new Point()
            {
                x = 0, y = 10
            }, LineIntersection.FindIntersection(line1, line2));


            line1 = new Line {
                x1 = 0, y1 = 0, x2 = 10, y2 = 10
            };
            line2 = new Line {
                x1 = 0, y1 = 10, x2 = 10, y2 = 10
            };

            Assert.AreEqual(new Point()
            {
                x = 10, y = 10
            }, LineIntersection.FindIntersection(line1, line2));

            line1 = new Line {
                x1 = 10, y1 = 0, x2 = 0, y2 = 10
            };
            line2 = new Line {
                x1 = 0, y1 = 0, x2 = 10, y2 = 10
            };

            Assert.AreEqual(new Point()
            {
                x = 5, y = 5
            }, LineIntersection.FindIntersection(line1, line2));

            line1 = new Line {
                x1 = -5, y1 = -5, x2 = 0, y2 = 0
            };
            line2 = new Line {
                x1 = 1, y1 = 1, x2 = 10, y2 = 10
            };

            Assert.AreEqual(default(Point), LineIntersection.FindIntersection(line1, line2));

            line1 = new Line {
                x1 = 3, y1 = -5, x2 = 3, y2 = 10
            };
            line2 = new Line {
                x1 = 0, y1 = 5, x2 = 10, y2 = 5
            };

            Assert.AreEqual(new Point()
            {
                x = 3, y = 5
            }, LineIntersection.FindIntersection(line1, line2));

            line1 = new Line {
                x1 = 0, y1 = 5, x2 = 10, y2 = 5
            };
            line2 = new Line {
                x1 = 3, y1 = -5, x2 = 3, y2 = 10
            };

            Assert.AreEqual(new Point()
            {
                x = 3, y = 5
            }, LineIntersection.FindIntersection(line1, line2));


            line1 = new Line {
                x1 = 0, y1 = 5, x2 = 10, y2 = 5
            };
            line2 = new Line {
                x1 = 3, y1 = -5, x2 = 5, y2 = 15
            };

            Assert.AreEqual(new Point()
            {
                x = 4, y = 5
            }, LineIntersection.FindIntersection(line1, line2));



            line1 = new Line {
                x1 = 0, y1 = -5, x2 = 0, y2 = 5
            };
            line2 = new Line {
                x1 = -3, y1 = 0, x2 = 3, y2 = 0
            };

            Assert.AreEqual(new Point()
            {
                x = 0, y = 0
            }, LineIntersection.FindIntersection(line1, line2));
        }
示例#30
0
        public void ScanCellsOverTriangle(ISubGridTree tree,
                                          int triIndex,
                                          Func <ISubGridTree, int, int, bool> leafSatisfied,
                                          Action <ISubGridTree, int, int, int> includeTriangleInLeaf,
                                          Action <ISubGridTree,
                                                  int,                                  // sourceTriangle
                                                  Func <ISubGridTree, int, int, bool>,  // leafSatisfied
                                                  Action <ISubGridTree, int, int, int>, // includeTriangleInLeaf
                                                  XYZ, XYZ, XYZ, bool> ProcessTrianglePiece)
        {
            Triangle Tri = TriangleItems[triIndex];

            // Split triangle into two pieces, a 'top' piece and a 'bottom' piece to simplify
            // scanning across the triangle. Split is always with a horizontal line

            XYZ[] SortVertices = new XYZ[]
            {
                VertexItems[Tri.Vertex0],
                VertexItems[Tri.Vertex1],
                VertexItems[Tri.Vertex2]
            };

            if (SortVertices[0].Y > SortVertices[1].Y)
            {
                DesignGeometry.SwapVertices(ref SortVertices[0], ref SortVertices[1]);
            }
            if (SortVertices[1].Y > SortVertices[2].Y)
            {
                DesignGeometry.SwapVertices(ref SortVertices[1], ref SortVertices[2]);
            }
            if (SortVertices[0].Y > SortVertices[1].Y)
            {
                DesignGeometry.SwapVertices(ref SortVertices[0], ref SortVertices[1]);
            }

            XYZ TopVertex     = SortVertices[2];
            XYZ CentralVertex = SortVertices[1];
            XYZ BottomVertex  = SortVertices[0];

            // now make sure leftmost vertex in in first array item
            if (SortVertices[0].X > SortVertices[1].X)
            {
                DesignGeometry.SwapVertices(ref SortVertices[0], ref SortVertices[1]);
            }
            if (SortVertices[1].X > SortVertices[2].X)
            {
                DesignGeometry.SwapVertices(ref SortVertices[1], ref SortVertices[2]);
            }
            if (SortVertices[0].X > SortVertices[1].X)
            {
                DesignGeometry.SwapVertices(ref SortVertices[0], ref SortVertices[1]);
            }

            XYZ LeftMostVertex  = SortVertices[0];
            XYZ RightMostVertex = SortVertices[2];

            // Are top or bottom vertices coincident with the middle vertex
            bool BottomPieceOnly = Math.Abs(TopVertex.Y - CentralVertex.Y) < 0.0001;
            bool TopPieceOnly    = Math.Abs(BottomVertex.Y - CentralVertex.Y) < 0.0001;

            if (TopPieceOnly && BottomPieceOnly) // It's a thin horizontal triangle
            {
                ProcessTrianglePiece(tree, triIndex, leafSatisfied, includeTriangleInLeaf, LeftMostVertex, RightMostVertex, CentralVertex, true);
            }
            else
            {
                if (!(TopPieceOnly || BottomPieceOnly))
                {
                    // Divide triangle in two with a horizontal line
                    // Find intersection point of triangle edge between top most and bottom most vertices
                    if (LineIntersection.LinesIntersect(LeftMostVertex.X - 1, CentralVertex.Y,
                                                        RightMostVertex.X + 1, CentralVertex.Y,
                                                        TopVertex.X, TopVertex.Y,
                                                        BottomVertex.X, BottomVertex.Y,
                                                        out double IntersectX, out double IntersectY, true, out _))
                    {
                        XYZ IntersectionVertex = new XYZ(IntersectX, IntersectY, 0);
                        ProcessTrianglePiece(tree, triIndex, leafSatisfied, includeTriangleInLeaf, CentralVertex, IntersectionVertex, TopVertex, false);
                        ProcessTrianglePiece(tree, triIndex, leafSatisfied, includeTriangleInLeaf, CentralVertex, IntersectionVertex, BottomVertex, false);
                    }
                    else
                    {
                        Log.LogWarning($"Triangle {Tri} failed to have intersection line calculated for it");
                    }
                }