Beispiel #1
0
        private void OnResize()
        {
            GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);

            InternalBox box = obstaclesCollection.Bounds;

            settings.Size = new Size(Bounds.Width, Bounds.Height);

            // теперь отскейлим  box так, чтобы сохранить пропорции
            // также добавим ещё процентов 20 по сторонам

            float coeffW = (float)Width / Height;
            float coeffM = box.W / box.H;

            float halfW, halfH;

            if (coeffW < coeffM)
            {
                halfW = box.W / 2;
                halfH = box.W / coeffW / 2;
            }
            else
            {
                halfH = box.H / 2;
                halfW = box.H * coeffW / 2;
            }

            Vector2 center = box.Center;

            // если пути нет, проставим его ровно посереднине слева направо
            if (!pathSet)
            {
                start   = new Vector2(center.x - halfW * 1.1f, center.y);
                end     = new Vector2(center.x + halfW * 1.1f, center.y);
                pathSet = true;

                settings.StartAndEnd = new[] { start, end };
            }

            // И надбавим 20% чтобы было откуда и куда строить путь
            halfW *= 1.2f;
            halfH *= 1.2f;

            Matrix4 projection = Matrix4.CreateOrthographicOffCenter(center.x - halfW, center.x + halfW,
                                                                     center.y - halfH, center.y + halfH, -10, 10);

            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadMatrix(ref projection);

            Vector3 point = new Vector3(0, 0, 0);

            Glu.UnProject(new Vector3(0, 0, 0), ref point);
            Vector2 pointA = new Vector2(point.X, point.Y);

            Glu.UnProject(new Vector3(20, 0, 0), ref point);
            Vector2 pointB = new Vector2(point.X, point.Y);

            selectionRadius = Vector2.Distance(pointA, pointB);
        }
Beispiel #2
0
 public void Init(Vector2[][] obstacles)
 {
     _obstacles     = obstacles;
     _obstaclesAABB = new InternalBox[_obstacles.Length];
     for (int i = 0; i < obstacles.Length; i++)
     {
         _obstaclesAABB[i] = new InternalBox(_obstacles[i]);
     }
 }
Beispiel #3
0
        private Vector2?Intersect(InternalBox aabb, Vector2 start, Vector2 end)
        {
            Vector2 tmp = new Vector2();

            if (aabb.Contains(start))
            {
                return(start);
            }
            if (aabb.Contains(end))
            {
                return(end);
            }

            bool intersect = Vector2.SegmentToSegmentIntersection(start, end, aabb.LeftBottom, new Vector2(aabb.LeftBottom.x, aabb.RightTop.y), ref tmp) ||
                             Vector2.SegmentToSegmentIntersection(start, end, aabb.RightTop, new Vector2(aabb.LeftBottom.x, aabb.RightTop.y), ref tmp) ||
                             Vector2.SegmentToSegmentIntersection(start, end, aabb.RightTop, new Vector2(aabb.RightTop.x, aabb.LeftBottom.y), ref tmp) ||
                             Vector2.SegmentToSegmentIntersection(start, end, aabb.LeftBottom, new Vector2(aabb.RightTop.x, aabb.LeftBottom.y), ref tmp);

            return(intersect ? tmp : (Vector2?)null);
        }
Beispiel #4
0
            public Obstacles(string file, InternalObstaclesCollection collection)
            {
                this.file       = file;
                this.collection = collection;

                const int totalNumberOfPoints = 100;

                int            numberOfRandomPoints = collection.areasA != null && collection.areasB != null ? (int)(totalNumberOfPoints * 0.2f) : totalNumberOfPoints;
                const int      numberOfAreaPoints   = (int)(totalNumberOfPoints * 0.8f);
                List <Vector2> testPoints           = new List <Vector2>();

                int pos = 0;

                // Для каждого набора точек сгенерим список случайных конечных и начальных точек
                // Если на карте заданы areaA и areaB, мы сгенерим кучку точек в пределах этих area
                if (collection.areasA != null && collection.areasB != null)
                {
                    do
                    {
                        InternalBox[] startAreas;
                        InternalBox[] endAreas;

                        if (Mathematics.Random.Range(0, 2) == 0)
                        {
                            startAreas = collection.areasA;
                            endAreas   = collection.areasB;
                        }
                        else
                        {
                            startAreas = collection.areasA;
                            endAreas   = collection.areasB;
                        }

                        InternalBox startArea = startAreas[Mathematics.Random.Range(0, startAreas.Length)];
                        InternalBox endArea   = endAreas[Mathematics.Random.Range(0, endAreas.Length)];

                        Vector2 startPoint = new Vector2(Mathematics.Random.Range(startArea.LeftBottom.x, startArea.RightTop.x),
                                                         Mathematics.Random.Range(startArea.LeftBottom.y, startArea.RightTop.y));
                        Vector2 endPoint = new Vector2(Mathematics.Random.Range(endArea.LeftBottom.x, endArea.RightTop.x),
                                                       Mathematics.Random.Range(endArea.LeftBottom.y, endArea.RightTop.y));

                        if (collection.Contains(startPoint) || collection.Contains(endPoint))
                        {
                            continue;
                        }

                        testPoints.Add(startPoint);
                        testPoints.Add(endPoint);

                        pos += 2;
                    } while (pos <= numberOfAreaPoints);
                }

                InternalBox box = collection.Bounds;
                Vector2     min = box.LeftBottom - new Vector2(box.W * 0.1f, box.H * 0.1f);
                Vector2     max = box.RightTop + new Vector2(box.W * 0.1f, box.H * 0.1f);

                pos = 0;
                do
                {
                    Vector2 randomPoint = new Vector2(Mathematics.Random.Range(min.x, max.x), Mathematics.Random.Range(min.y, max.y));

                    if (collection.Contains(randomPoint))
                    {
                        continue;
                    }

                    testPoints.Add(randomPoint);
                    pos++;
                } while (pos != numberOfRandomPoints);

                points = testPoints.ToArray();
            }
Beispiel #5
0
 public ObstacleToIndex(InternalBox obstacle, int index)
 {
     this.obstacle = obstacle;
     this.index    = index;
 }