Ejemplo n.º 1
0
        private void CalculateVisibilityGraph()
        {
            DateTime startTime = DateTime.Now;

            Waypoints = new Waypoint[CollisionAreas.Sum(p => p.Points.Length)];

            int x = 0;

            foreach (Polygon poly in CollisionAreas)
            {
                for (int i = 0; i < poly.Points.Length; i++)
                {
                    poly.Points[i] = poly.Points[i] * SCALE_FACTOR;
                    Waypoints[x]   = new Waypoint(x)
                    {
                        Position = poly.Points[i]
                    };
                    x++;
                }

                poly.UpdateBounds();
            }

            x = 0;
            foreach (Polygon poly in CollisionAreas)
            {
                for (int i = 0, j = poly.Points.Length - 1; i < poly.Points.Length; j = i++)
                {
                    Waypoints[x + i].AddConnectionTo(Waypoints[x + j]);
                }

                x += poly.Points.Length;
            }

            Parallel.ForEach(Waypoints, (w1) =>
            {
                foreach (Waypoint w2 in Waypoints)
                {
                    if (!w1.IsConnectedTo(w2))
                    {
                        if (HasClearLineOfSight(w1.Position, w2.Position))
                        {
                            w1.AddConnectionTo(w2);
                        }
                    }
                }
            });

            TimeSpan calculationTime = DateTime.Now - startTime;

            s_log.Debug("Calculate waypoints took {0}", calculationTime);
        }
Ejemplo n.º 2
0
        public Bitmap RenderMap(int height)
        {
            BoundingBox bounds = BoundingBox.CreateFromPoints(CollisionAreas.SelectMany(p => p.Points));
            Vector2     scale  = bounds.Max - bounds.Min;

            float aspect = scale.X / scale.Y;

            int width = (int)(height * aspect);

            Bitmap render = new Bitmap(width, height);

            Vector2 size = new Vector2(width - 24, height - 24);

            using (Graphics g = Graphics.FromImage(render))
                using (Font font = new Font("Arial", 24, FontStyle.Bold))
                    using (SolidBrush b = new SolidBrush(Color.Red))
                    {
                        g.Clear(Color.White);

                        Pen connectionPen = new Pen(Color.Green, 1);
                        foreach (WaypointConnection connection in Waypoints.SelectMany(w => w.Connections))
                        {
                            Point p1 = NormaliseVector(connection.Source.Position, bounds.Min, scale, size);
                            Point p2 = NormaliseVector(connection.Target.Position, bounds.Min, scale, size);

                            g.DrawLine(connectionPen, p1, p2);
                        }

                        Pen polyPen = new Pen(Color.Red, 3);
                        foreach (Polygon poly in CollisionAreas)
                        {
                            for (int i = 0, j = poly.Points.Length - 1; i < poly.Points.Length; j = i++)
                            {
                                Point p1 = NormaliseVector(poly.Points[i], bounds.Min, scale, size);
                                Point p2 = NormaliseVector(poly.Points[j], bounds.Min, scale, size);

                                g.DrawLine(polyPen, p1, p2);
                            }
                        }

                        Pen            pathPen = new Pen(Color.Blue, 3);
                        Vector2        from    = new Vector2(120, 92);
                        Vector2        to      = new Vector2(70, 88);
                        List <Vector2> path    = FindPath(from, to);

                        Point  a1 = NormaliseVector(from * 1000, bounds.Min, scale, size);
                        Point  a2 = NormaliseVector(path[0] * 1000, bounds.Min, scale, size);
                        PointF c  = new PointF(a1.X, a1.Y);
                        g.DrawLine(pathPen, a1, a2);
                        g.DrawString("From", font, b, c);

                        for (int i = 0; i < path.Count - 1; i++)
                        {
                            Point  p1 = NormaliseVector(path[i] * 1000, bounds.Min, scale, size);
                            Point  p2 = NormaliseVector(path[i + 1] * 1000, bounds.Min, scale, size);
                            PointF f  = new PointF(p1.X, p1.Y);
                            g.DrawLine(pathPen, p1, p2);
                            g.DrawString(i.ToString(), font, b, f);
                        }
                    }
            return(render);
        }