Beispiel #1
0
        private bool FindTrasingData(Segment l, ref TracingData data)
        {
            var intersections = new Dictionary <Vector2, int>();
            var inDistances   = new Dictionary <float, Vector2>();
            var outDistances  = new Dictionary <float, Vector2>();

            for (var i = 0; i < Segments.Length; i++)
            {
                var segment        = Segments[i];
                var intersectPoint = new Vector2();
                var isIntersects   = Vector2.SegmentToSegmentIntersection(l.Start, l.End, segment.Start, segment.End, ref intersectPoint);
                if (!isIntersects)
                {
                    continue;
                }

                intersections[intersectPoint] = i;
                inDistances[Vector2.Distance(intersectPoint, l.Start)] = intersectPoint;
                outDistances[Vector2.Distance(intersectPoint, l.End)]  = intersectPoint;
            }

            if (intersections.Count == 0)
            {
                return(false);
            }

            data.InPoint         = inDistances[inDistances.Keys.Min()];
            data.OutPoint        = outDistances[outDistances.Keys.Min()];
            data.InSegmentIndex  = intersections[data.InPoint];
            data.OutSegmentIndex = intersections[data.OutPoint];
            return(true);
        }
Beispiel #2
0
        private List <Vector2> findIntersectPoints3(Vector2 start, Vector2 end, Segment[] segments)
        {
            var result = new List <Vector2>();

            foreach (Segment segment in segments)
            {
                Vector2 res = Vector2.zero;
                if (Vector2.SegmentToSegmentIntersection(segment.start, segment.end, start, end, ref res))
                {
                    result.Add(res);
                }
            }

            return(result);
        }
Beispiel #3
0
        private int findIntersectPoints2(Vector2 start, Vector2 end, Segment[] segments)
        {
            int count = 0;

            foreach (Segment segment in segments)
            {
                Vector2 res = Vector2.zero;
                if (Vector2.SegmentToSegmentIntersection(segment.start, segment.end, start, end, ref res))
                {
                    count++;
                }
            }

            return(count);
        }
Beispiel #4
0
        private IEnumerable <Vector2> findIntersectPoints(Vector2 startPath, Vector2 endPath)
        {
            var result = new List <Vector2>();

            for (int i = 0; i < obstacles.Length; i++)
            {
                for (int j = 0; j < obstacles[i].Length; j++)
                {
                    Vector2 v1;
                    Vector2 v2;

                    if (j == obstacles[i].Length - 1)
                    {
                        v1 = new Vector2(obstacles[i][j].x, obstacles[i][j].y);
                        v2 = new Vector2(obstacles[i][0].x, obstacles[i][0].y);
                    }
                    else
                    {
                        v1 = new Vector2(obstacles[i][j].x, obstacles[i][j].y);
                        v2 = new Vector2(obstacles[i][j + 1].x, obstacles[i][j + 1].y);
                    }

                    Vector2 v3  = new Vector2(startPath.x, startPath.y);
                    Vector2 v4  = new Vector2(endPath.x, endPath.y);
                    Vector2 res = new Vector2();

                    //  Console.WriteLine("obstacle segment = " + v1 + " - " + v2);
                    //   Console.WriteLine("path segment = " + v3 + " - " + v4);

                    var segmentToSegmentIntersection = Vector2.SegmentToSegmentIntersection(v1, v2, v3, v4, ref res);
                    //   Console.WriteLine("intersect res = " + segmentToSegmentIntersection);
                    //  Console.WriteLine("intersect point = " + v4);
                    //  Console.WriteLine();
                    if (segmentToSegmentIntersection)
                    {
                        result.Add(res);
                    }
                }
            }

            return(result);
        }