Exemple #1
0
        public bool Intersects(IEnumerable <Vector2> path, ref Vector2 badSegmentStart, ref Vector2 badSegmentEnd)
        {
            // Сначала проверим дешёвое пересечение AABB с этим путём

            bool    intersectsWithAABB = false;
            bool    first = true;
            Vector2 prev  = Vector2.zero;


            foreach (Vector2 point in path)
            {
                if (first)
                {
                    first = false;
                    prev  = point;
                }

                if (Bounds.ContainOrIntersects(prev, point))
                {
                    intersectsWithAABB = true;
                    break;
                }
            }

            // Временно выключим
            //     if (!intersectsWithAABB)
            //        return false;

            //  Vector2 temp = Vector2.down;
            // Раз мы здесь, значит нет пересечения с AABB и надо уже проверять умное пересечение с гранями
            first = true;
            prev  = Vector2.zero;

            foreach (Vector2 point in path)
            {
                if (first)
                {
                    first = false;
                    prev  = point;
                    continue;
                }

                // у нас есть сегмент пути [prev, point]
                // получим его нормаль и посмотрим что все точки контура с одной из сторон от этого сегмента
                if (AllContourPointsOnOneSide(prev, Vector2.Perpendicular(point - prev)))
                {
                    prev = point;
                    continue;
                }

                // Далее остаётся немного вариантов - и совпадение с внутренним ребром один из них
                for (int i = 0; i < triangles.Length; i++)
                {
                    if (triangles[i].TriangleSegmentIntersection(prev, point))
                    {
                        badSegmentStart = prev;
                        badSegmentEnd   = point;
                        return(true);
                    }
                }

                prev = point;
            }

            return(false);
        }