Пример #1
0
        public Point get_menos_concurrido(Vector location)
        {
            int   min   = Logica.nro_boids + 10;
            int   n     = 0;
            Point final = objetivos [0];

            foreach (Point p in objetivos)
            {
                n = 0;
                foreach (Boid b in Logica.objects)
                {
                    if (b.Location.distance(new Vector(p.X, Logica.mapeo(p.Y))) <= Constantes.radio_objetivos * 2)
                    {
                        n++;
                    }
                }
                if (n < min)
                {
                    final = p;
                    min   = n;
                }
                else if (n == min)
                {
                    if (location.distance(new Vector(p.X, Logica.mapeo(p.Y))) < location.distance(new Vector(final.X, Logica.mapeo(final.Y))))
                    {
                        final = p;
                    }
                }
            }
            return(final);
        }
Пример #2
0
        private Vector cohesion(BoidCollection boids)         //promedio de las posiciones de los vecinos dentro del radio
        {
            Vector sum       = new Vector(0, 0);
            int    neighbors = 0;
            int    n         = 0;

            if (Logica.puntos_objetivo.Count > 0 && objetivo < Logica.puntos_objetivo.Count)
            {
                Vector nuevo = new Vector(intermedio.X, Logica.mapeo(intermedio.Y));
                sum.add(nuevo);
                neighbors++;
            }

            else
            {
                foreach (Boid b in boids)
                {
                    double d = Location.distance(b.Location);
                    if ((Logica.mouse && n == 0) || (n != 0 && d > 0 && d < Logica.radioCoh))
                    {
                        sum.add(b.Location);
                        neighbors++;
                    }
                    n++;
                }
            }

            if (neighbors > 0)
            {
                sum.div(neighbors);
                return(seek(sum));
            }

            return(sum);
        }
Пример #3
0
 public override void calculateVelocity(BoidCollection s, DrawingArea area)
 {
     area.GetPointer(out x, out y);
     y               = (int)Logica.mapeo(y);
     Velocity.X      = x - Location.X;
     Velocity.Y      = y - Location.Y;
     LocationNueva.X = x;
     LocationNueva.Y = y;
 }
Пример #4
0
        private void update_objetivo()
        {
            if (Logica.puntos_objetivo.Count > 0 && objetivo < Logica.puntos_objetivo.Count)
            {
                double distancia = Location.distance(new Vector(intermedio.X, Logica.mapeo(intermedio.Y)));
                if (distancia <= Constantes.radio_objetivos)                   //ver cuando cambiar el objetivo
                {
                    objetivo++;
                    if (objetivo < Logica.puntos_objetivo.Count && Logica.puntos_objetivo [objetivo].Objetivos().Count > 0)
                    {
                        if (criterio == 0)
                        {
                            intermedio = Logica.puntos_objetivo [objetivo].get_cercano(Location);
                        }
                        else if (criterio == 1)
                        {
                            intermedio = Logica.puntos_objetivo [objetivo].get_menos_concurrido(Location);
                        }
                        else
                        {
                            intermedio = Logica.puntos_objetivo [objetivo].get_random(random);
                        }
                    }

                    else
                    {
                        objetivo = 0;
                        actualizarMetricas();

                        if (Constantes.simulacionContinua)
                        {
                            ubicar_boid_punto_inicio();
                            trayectoria.Clear();
                        }
                        else
                        {
                            llego = true;
                        }

                        if (criterio == 0)
                        {
                            intermedio = Logica.puntos_objetivo [objetivo].get_cercano(LocationNueva);
                        }
                        else if (criterio == 1)
                        {
                            intermedio = Logica.puntos_objetivo[objetivo].get_menos_concurrido(LocationNueva);
                        }
                        else
                        {
                            intermedio = Logica.puntos_objetivo [objetivo].get_random(random);
                        }
                    }
                }
            }
        }
Пример #5
0
        private Vector align(BoidCollection boids)         //promedio de las velocidades de los vecinos
        {
            Vector sum       = new Vector(0, 0);
            int    neighbors = 0;
            int    n         = 0;

            if (Logica.puntos_objetivo.Count > 0 && objetivo < Logica.puntos_objetivo.Count)
            {
                Vector v;
                if (intermedio.X > Location.X)
                {
                    v = new Vector(intermedio.X - Location.X, Logica.mapeo(intermedio.Y) - Location.Y);
                }
                else
                {
                    v = new Vector(Location.X - intermedio.X, Location.Y - Logica.mapeo(intermedio.Y));
                }
                v.normalize();
                v.mult(0.5);
                sum.add(v);
                neighbors++;
            }

            else
            {
                foreach (Boid b in boids)
                {
                    double d = Location.distance(b.Location);
                    if ((Logica.mouse && n == 0) || (n != 0 && d > 0 && d < Logica.radioAli))
                    {
                        sum.add(b.Velocity);
                        neighbors++;
                    }
                    n++;
                }
            }

            if (neighbors > 0)
            {
                sum.div(neighbors);
                sum.normalize();
                sum.mult(Logica.max_speed);
                Vector steer = sum.sub(sum, this.Velocity);
                steer.limit(Logica.max_force);
                return(steer);
            }
            else
            {
                return(new Vector(0, 0));
            }
        }
Пример #6
0
//		Vector locacion_temp;
//		bool mover = true;
//

        public BoidAutonomo(int lapso, Random r)
        {
            ogreRef       = IntPtr.Zero;
            rotacion      = 0;
            llego         = false;
            nro_iteracion = 0;
            this.objetivo = 0;
            random        = r;
            tiempo_inicio = Constantes.tiempo;
            seleccionarCriterio(r);
            trayectoria       = new List <Point> ();
            this.vecinos      = 0;
            this.Acceleration = new Vector(0, 0);
            double angle = r.NextDouble() * 2 * Math.PI;

            this.Velocity = new Vector(Math.Cos(angle), Math.Sin(angle));
            this.Velocity.mult(4);
//			this.Location = new Vector(Constantes.limites.Width/2, Constantes.limites.Height/2);
//			this.Velocity = new Vector(0, 1);
            if (Logica.puntos_inicio.Count > 0)
            {
                Point p = Logica.puntos_inicio.ElementAt((int)((lapso - 1) % Logica.puntos_inicio.Count));                  //el punto de inicio es numero de lapso MOD cantidad ptos inicio
                this.Location = new Vector(p.X, Logica.mapeo(p.Y));
            }
            else
            {
                this.Location = new Vector(Constantes.limites.Width / 2, Constantes.limites.Height / 2);
            }
            //this.Location = new Vector (r.NextDouble () * Constantes.limites.Width, r.NextDouble () * Constantes.limites.Height);

//			locacion_temp = new Vector (Location.X,Location.Y);

            LocationNueva = new Vector(Location.X, Location.Y);

            if (Logica.puntos_objetivo.Count > 0)               //Comprobar si elige el objetivo mas corto

            {
                if (criterio == 0)
                {
                    intermedio = Logica.puntos_objetivo [objetivo].get_cercano(Location);
                }
                else if (criterio == 1)
                {
                    intermedio = Logica.puntos_objetivo [objetivo].get_menos_concurrido(Location);
                }
                else
                {
                    intermedio = Logica.puntos_objetivo [objetivo].get_random(random);
                }
            }
        }
Пример #7
0
        private void ubicar_boid_punto_inicio()
        {
            Random r = new Random();

            if (Logica.puntos_inicio.Count > 0)
            {
                double nro = Math.Round(r.NextDouble() * (Logica.puntos_inicio.Count - 1));
                Point  p   = Logica.puntos_inicio[(int)nro];              //el punto de inicio es numero de lapso MOD cantidad ptos inicio
                this.LocationNueva = new Vector(p.X, Logica.mapeo(p.Y));
            }
            else
            {
                this.LocationNueva = new Vector(Constantes.limites.Width / 2, Constantes.limites.Height / 2);
            }
        }
Пример #8
0
        public Point get_cercano(Vector location)
        {
            double distancia = 10000;
            Point  final     = objetivos[0];

            foreach (Point p in objetivos)
            {
                double temporal = location.distance(new Vector(p.X, Logica.mapeo(p.Y)));
                if (temporal < distancia)
                {
                    final     = p;
                    distancia = temporal;
                }
            }
            return(final);
        }
Пример #9
0
        private void triangulo(Cairo.Context cr, Boid b, double rot)
        {
            reset(cr);

            cr.Save();

            r.Rotate(rot);
            t.Translate(b.Location.X, Logica.mapeo(b.Location.Y));

            r.TransformPoint(ref Ax, ref Ay);
            t.TransformPoint(ref Ax, ref Ay);

            r.TransformPoint(ref Bx, ref By);
            t.TransformPoint(ref Bx, ref By);

            r.TransformPoint(ref Cx, ref Cy);
            t.TransformPoint(ref Cx, ref Cy);

            //			System.Diagnostics.Debug.WriteLine (Ax+", "+Ay);

            cr.MoveTo(Ax, Ay);
            cr.LineTo(Bx, By);
            cr.LineTo(Cx, Cy);

            cr.LineWidth = 2;

            if (b.criterio == 0)
            {
                cr.SetSourceRGBA(0.5, 0, 0, 0.5);                  //cercano
            }
            else if (b.criterio == 1)
            {
                cr.SetSourceRGBA(0, 0.5, 0, 0.5);                  //menos concurrido
            }
            else
            {
                cr.SetSourceRGBA(0, 0, 0.5, 0.5);                  //random
            }
            cr.ClosePath();
            cr.FillPreserve();
            cr.Stroke();

            cr.Restore();
        }
Пример #10
0
 private void dibujar_trayectorias(DrawingArea area, Cairo.Context cr)
 {
     System.Drawing.Point punto;
     for (int i = 1; i < Logica.objects.Count; i++)
     {
         if (((BoidAutonomo)Logica.objects[i]).trayectoria.Count > 0)
         {
             punto = ((BoidAutonomo)Logica.objects[i]).trayectoria [0];
             foreach (System.Drawing.Point p in ((BoidAutonomo)Logica.objects[i]).trayectoria)
             {
                 cr.Save();
                 cr.LineWidth = 0.5;
                 cr.SetSourceRGBA(0, 0, 0, 0.5);
                 cr.MoveTo(punto.X, Logica.mapeo(punto.Y));
                 cr.LineTo(p.X, Logica.mapeo(p.Y));
                 cr.Stroke();
                 cr.Restore();
                 punto = p;
             }
         }
     }
 }
Пример #11
0
        private void triangulo_contorno(Cairo.Context cr, Vector location, double rot)
        {
            reset(cr);

            cr.Save();

            r.Rotate(rot);
            t.Translate(location.X, Logica.mapeo(location.Y));

            r.TransformPoint(ref Ax, ref Ay);
            t.TransformPoint(ref Ax, ref Ay);

            r.TransformPoint(ref Bx, ref By);
            t.TransformPoint(ref Bx, ref By);

            r.TransformPoint(ref Cx, ref Cy);
            t.TransformPoint(ref Cx, ref Cy);

//			System.Diagnostics.Debug.WriteLine (Ax+", "+Ay);

            cr.MoveTo(Ax, Ay);
            cr.LineTo(Bx, By);
            cr.LineTo(Cx, Cy);

            cr.LineWidth = 2;
            cr.SetSourceRGBA(0, 0, 0, 0.5);

            cr.ClosePath();
            cr.FillPreserve();
            cr.Stroke();

            cr.Restore();

            cr.Save();
            cr.Arc(Ax, Ay, Logica.radioSep, 0, 2 * Math.PI);
            cr.SetSourceRGBA(1, 0, 0, 0.2);
            cr.Stroke();
            cr.Restore();
        }
Пример #12
0
        private void dibujar_grilla_estadistica(DrawingArea area, Cairo.Context cr)
        {
            double[] color;
            double   min = Logica.grilla.min_visitas();
            double   max = Logica.grilla.max_visitas();

            for (int x = 0; x < Constantes.nro_casillas; x++)
            {
                for (int y = 0; y < Constantes.nro_casillas; y++)
                {
                    int x0 = Logica.grilla.get_casillero(x, y).inicioX;
                    int y0 = Logica.grilla.get_casillero(x, y).inicioY;
                    int xf = Logica.grilla.get_casillero(x, y).finX - Logica.grilla.get_casillero(x, y).inicioX;
                    int yf = Logica.grilla.get_casillero(x, y).finY - Logica.grilla.get_casillero(x, y).inicioY;
                    cr.Save();

//					referencia_mapa (cr);

                    cr.Save();

                    color = calcular_mapa_calor(Logica.grilla.get_casillero(x, y).visitas, min, max);
                    cr.SetSourceRGBA(color[0], color[1], color[2], 0.5);
                    cr.Rectangle(x0, Logica.mapeo(y0) - yf, xf, yf);
                    cr.Fill();


//					cr.SetSourceRGB (0,0,0);
//					cr.SelectFontFace ("Arial", FontSlant.Normal, FontWeight.Normal);
//					cr.SetFontSize (12);
//					TextExtents t = cr.TextExtents (Logica.grilla.get_casillero (x, y).visitas.ToString ());
//					cr.MoveTo (x0+10, Logica.mapeo(y0)-10);
//					cr.ShowText (Logica.grilla.get_casillero (x, y).visitas.ToString ());

                    cr.Restore();
                }
            }
        }
Пример #13
0
        public void cargar_obstaculos()
        {
            limpiar_obstaculos();
            Cairo.Rectangle n;
            foreach (Obstaculo r in Logica.rectangulos)
            {
                double X0 = r.rectangle.X, Y0 = Logica.mapeo(r.rectangle.Y), X1 = r.rectangle.X + r.rectangle.Width, Y1 = Logica.mapeo(r.rectangle.Y + r.rectangle.Height);
                double Xf, Yf;
                if (X0 < X1)
                {
                    X0 -= 10;
                    Xf  = X1 + 20;
                }
                else
                {
                    Xf = X0 + 20;
                    X0 = X1 - 10;
                }
                if (Y0 < Y1)
                {
                    Y0 -= 10;
                    Yf  = Y1 + 20;
                }
                else
                {
                    Yf = Y0 + 20;
                    Y0 = Y1 - 10;
                }

                n = new Cairo.Rectangle((int)X0, (int)Y0, (int)Xf, (int)Yf);
                ubicar_rectangulo_casillero(n, r, 'r');
            }

            foreach (Obstaculo c in Logica.circulos)
            {
                double X0 = c.rectangle.X - c.rectangle.Width - 10;
                double Y0 = Logica.mapeo(c.rectangle.Y) - c.rectangle.Width - 10;
                double X1 = c.rectangle.X + c.rectangle.Width + 10;
                double Y1 = Logica.mapeo(c.rectangle.Y) + c.rectangle.Width + 10;;
                n = new Cairo.Rectangle(X0, Y0, X1, Y1);
                ubicar_rectangulo_casillero(n, c, 'c');
            }


            foreach (Obstaculo l in Logica.lineas)
            {
                int lineainiciox, lineafinx, lineainicioy, lineafiny;
                if (l.rectangle.X < l.rectangle.Width)
                {
                    lineainiciox = (int)l.rectangle.X - 10;
                    lineafinx    = (int)l.rectangle.Width + 10;
                }
                else
                {
                    lineainiciox = (int)l.rectangle.Width - 10;
                    lineafinx    = (int)l.rectangle.X + 10;
                }
                if (Logica.mapeo(l.rectangle.Y) < Logica.mapeo(l.rectangle.Height))
                {
                    lineainicioy = (int)Logica.mapeo(l.rectangle.Y) - 10;
                    lineafiny    = (int)Logica.mapeo(l.rectangle.Height) + 10;
                }
                else
                {
                    lineainicioy = (int)Logica.mapeo(l.rectangle.Height) - 10;
                    lineafiny    = (int)Logica.mapeo(l.rectangle.Y) + 10;
                }


                n = new Cairo.Rectangle(lineainiciox, lineainicioy, lineafinx, lineafiny);
                ubicar_rectangulo_casillero(n, l, 'l');
            }
        }