Example #1
0
        private void rysujSamolot(Samolot s, Dystans d, Graphics g)
        {
            const int a = 24;
            Point     pw = pozycjaWzgledna(s.X, s.Y);
            int       x = pw.X, y = pw.Y;

            Point[] pkt = new Point[]
            {
                new Point(x - a / 3, y + a / 2),
                new Point(x, y - a / 2),
                new Point(x + a / 3, y + a / 2),
                new Point(x, y + a / 6),
                new Point(x - a / 3, y + a / 2)
            };
            pkt = obrocPunkty(pkt, new Point(x, y), s.Kierunek);
            Pen p = new Pen(d == Dystans.Bezpieczny ? Color.Green : Color.Red);

            p.Width = 1;
            if (d == Dystans.Kolizja)
            {
                g.FillPolygon(Brushes.DarkRed, pkt);
            }
            g.DrawLines(p, pkt);
            int r = Radar.MIN_ODLEGLOSC * Skala / 100;

            g.DrawEllipse(d == Dystans.Bezpieczny ? Pens.Gray : Pens.DarkRed, x - r, y - r, 2 * r, 2 * r);
            StringFormat sf = new StringFormat();

            sf.Alignment     = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            g.DrawString("S-" + s.Numer, Font, d != Dystans.Kolizja ? Brushes.White : Brushes.Red, new Rectangle(x, y - a / 2 - 15, 0, 0), sf);
        }
Example #2
0
 private void button3_Click(object sender, EventArgs e)
 {
     if (!radar.WczytajPlik())
     {
         return;
     }
     NextId = 1;
     comboBox1.Items.Clear();
     foreach (Obiekt o in radar.Obiekty)
     {
         Samolot s = o as Samolot;
         if (s != null)
         {
             NextId = s.Numer + 1;
         }
         comboBox1.Items.Add(o);
     }
     if (radar.Obiekty.Count > 0)
     {
         comboBox1.SelectedIndex = 0;
     }
     else
     {
         ZaznaczonyObiekt = null;
     }
 }
Example #3
0
        private void wyswietlDaneObiektu()
        {
            if (zaznaczonyObiekt == null)
            {
                pokazDaneSamolotu(false);
                pokazDaneObiektuNaziemnego(false);
                comboBox1.Text = "";
                return;
            }
            pokazDaneObiektuNaziemnego(true);
            xTextBox.Text = zaznaczonyObiekt.X.ToString("f2");
            yTextBox.Text = zaznaczonyObiekt.Y.ToString("f2");
            Samolot s = zaznaczonyObiekt as Samolot;

            pokazDaneSamolotu(s != null);
            if (s == null)
            {
                return;
            }
            wysokoscTextBox.Text = s.Wysokosc.ToString("f2");
            predkoscTextBox.Text = s.Predkosc.ToString("f2");
            if (!kierunekTextBox.Focused)
            {
                kierunekTextBox.Text = s.Kierunek.ToString("f2");
            }
            if (!wysokoscDocelowaTextBox.Focused)
            {
                wysokoscDocelowaTextBox.Text = s.WysokoscDocelowa.ToString("f2");
            }
            if (!predkoscDocelowaTextBox.Focused)
            {
                predkoscDocelowaTextBox.Text = s.PredkoscDocelowa.ToString("f2");
            }
        }
Example #4
0
 public bool Kolizja(Samolot s, Obiekt o)
 {
     if (s == o)
     {
         return(false);
     }
     return(odleglosc(s, o) <= 20);
 }
Example #5
0
 public bool NiebezpiecznyDystans(Samolot s, Obiekt o)
 {
     if (s == o)
     {
         return(false);
     }
     return(odleglosc(s, o) <= MIN_ODLEGLOSC);
 }
Example #6
0
        private void generujSamolot()
        {
            int    x = 0, y = 0;
            double kierunek = 0;

            switch (rand.Next() % 4)
            {
            case 0:
                x        = 0;
                y        = rand.Next() % rozmiarEkranu.Height;
                kierunek = rand.NextDouble() * 180;
                break;

            case 1:
                x        = pictureBox1.Width;
                y        = rand.Next() % rozmiarEkranu.Height;
                kierunek = rand.NextDouble() * 180 + 180;
                break;

            case 2:
                x        = rand.Next() % rozmiarEkranu.Width;
                y        = 0;
                kierunek = rand.NextDouble() * 180 + 90;
                break;

            case 3:
                x        = rand.Next() % rozmiarEkranu.Width;
                y        = pictureBox1.Height;
                kierunek = rand.NextDouble() * 180 - 90;
                break;
            }
            Point p = pozycjaBezwzgledna(x, y);

            if (p.X < minX)
            {
                p.X = minX;
            }
            if (p.X > maxX)
            {
                p.X = maxX;
            }
            if (p.Y < minY)
            {
                p.Y = minY;
            }
            if (p.Y > maxY)
            {
                p.Y = maxY;
            }
            Samolot s = new Samolot(NextId, p.X, p.Y, rand.NextDouble() * 1000 + 5, rand.NextDouble() * 45 + 5, kierunek);

            DodajObiekt(s, false);
        }
Example #7
0
 private void predkoscDocelowaTextBox_KeyDown(object sender, KeyEventArgs e)
 {
     if (e.KeyCode == Keys.Enter)
     {
         e.SuppressKeyPress = true;
         Samolot s = comboBox1.SelectedItem as Samolot;
         double  pr;
         if (double.TryParse(predkoscDocelowaTextBox.Text, out pr) || pr < 0)
         {
             s.PredkoscDocelowa = pr;
         }
         predkoscTextBox.Text = s.PredkoscDocelowa.ToString("f2");
     }
 }
Example #8
0
 private void wysokoscDocelowaTextBox_KeyDown(object sender, KeyEventArgs e)
 {
     if (e.KeyCode == Keys.Enter)
     {
         e.SuppressKeyPress = true;
         Samolot s = comboBox1.SelectedItem as Samolot;
         double  wys;
         if (double.TryParse(wysokoscDocelowaTextBox.Text, out wys) || wys < 0)
         {
             s.WysokoscDocelowa = wys;
         }
         wysokoscDocelowaTextBox.Text = s.WysokoscDocelowa.ToString("f2");
     }
 }
Example #9
0
 private void kierunekTextBox_KeyDown(object sender, KeyEventArgs e)
 {
     if (e.KeyCode == Keys.Enter)
     {
         e.SuppressKeyPress = true;
         Samolot s = comboBox1.SelectedItem as Samolot;
         double  kierunek;
         if (double.TryParse(kierunekTextBox.Text, out kierunek))
         {
             s.Kierunek = kierunek;
         }
         kierunekTextBox.Text = s.Kierunek.ToString("f2");
     }
 }
Example #10
0
        public void DodajObiekt(Obiekt o, bool zaznacz = true)
        {
            Samolot s = o as Samolot;

            if (s != null)
            {
                NextId = s.Numer + 1;
            }
            radar.DodajObiekt(o);
            comboBox1.Items.Add(o);
            if (zaznacz)
            {
                comboBox1.SelectedItem = o;
            }
            button5.Enabled = true;
        }
Example #11
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked && idTextBox.Text.Length == 0)
            {
                MessageBox.Show("Podaj nazwę obiektu!");
                return;
            }
            double x, y;

            if (!double.TryParse(xTextBox.Text, out x) || !double.TryParse(yTextBox.Text, out y))
            {
                MessageBox.Show("Nieprawidłowe współrzędne!");
                return;
            }
            Obiekt o;

            if (radioButton1.Checked)
            {
                o = new ObiektNaziemny(idTextBox.Text, x, y);
            }
            else
            {
                double wysokosc, predkosc, kierunek;
                if (!double.TryParse(wysokoscTextBox.Text, out wysokosc) || wysokosc < 0)
                {
                    MessageBox.Show("Nieprawidłowa wartość wysokości!");
                    return;
                }
                if (!double.TryParse(predkoscTextBox.Text, out predkosc) || predkosc < 0)
                {
                    MessageBox.Show("Nieprawidłowa wartość prędkości!");
                    return;
                }
                if (!double.TryParse(kierunekTextBox.Text, out kierunek))
                {
                    MessageBox.Show("Nieprawdiłowy kierunek!");
                    return;
                }
                o = new Samolot((owner.NextId), x, y, wysokosc, predkosc, kierunek);
            }
            owner.DodajObiekt(o);
            Close();
        }
Example #12
0
 public MainForm()
 {
     InitializeComponent();
     DoubleBuffered      = true;
     NextId              = 1;
     rozmiarEkranu       = pictureBox1.Size;
     hScrollBar1.Maximum = maxX - minX;
     vScrollBar1.Maximum = maxY - minY;
     Skala           = 100;
     drawTimer.Tick += (s, e) =>
     {
         radar.Aktualizuj();
         for (int i = 0; i < radar.Obiekty.Count; i++)
         {
             Samolot samolot = radar.Obiekty[i] as Samolot;
             if (samolot == null)
             {
                 continue;
             }
             if (samolot.X < minX || samolot.X > maxX || samolot.Y < minY || samolot.Y > maxY)
             {
                 usunObiekt(samolot);
                 i--;
             }
         }
         pictureBox1.Refresh();
     };
     propertyTimer.Tick += (s, e) =>
     {
         wyswietlDaneObiektu();
     };
     generator.Tick += (s, e) =>
     {
         if (rand.NextDouble() <= 0.4 && radar.Obiekty.OfType <Samolot>().Count() < 25)
         {
             generujSamolot();
         }
     };
     drawTimer.Start();
     propertyTimer.Start();
     generator.Start();
 }
Example #13
0
        private void predkoscDocelowaTextBox_Leave(object sender, EventArgs e)
        {
            Samolot s = ZaznaczonyObiekt as Samolot;

            predkoscDocelowaTextBox.Text = s != null?s.PredkoscDocelowa.ToString("f2") : "";
        }
Example #14
0
        private void kierunekTextBox_Leave(object sender, EventArgs e)
        {
            Samolot s = ZaznaczonyObiekt as Samolot;

            kierunekTextBox.Text = s != null?s.Kierunek.ToString("f2") : "";
        }
Example #15
0
        private void rysuj(Graphics g)
        {
            Dystans[] d = new Dystans[radar.Obiekty.Count];
            for (int i = 0; i < d.Length; i++)
            {
                d[i] = Dystans.Bezpieczny;
            }
            for (int i = 0; i < radar.Obiekty.Count; i++)
            {
                Samolot s = radar.Obiekty[i] as Samolot;
                if (s == null || d[i] == Dystans.Kolizja)
                {
                    continue;
                }
                for (int j = 0; j < radar.Obiekty.Count; j++)
                {
                    if (radar.Kolizja(s, radar.Obiekty[j]))
                    {
                        d[i] = Dystans.Kolizja;
                        d[j] = Dystans.Kolizja;
                        break;
                    }
                    else if (radar.NiebezpiecznyDystans(s, radar.Obiekty[j]))
                    {
                        d[i] = Dystans.Niebezpieczny;
                        d[j] = Dystans.Niebezpieczny;
                    }
                }
            }
            g.Clear(Color.Black);
            for (int i = 0; i < radar.Obiekty.Count; i++)
            {
                if (radar.Obiekty[i] is ObiektNaziemny)
                {
                    ObiektNaziemny on = radar.Obiekty[i] as ObiektNaziemny;
                    rysujObiektNaziemny(on, d[i], g);
                }
                else if (radar.Obiekty[i] is Samolot)
                {
                    Samolot s = radar.Obiekty[i] as Samolot;
                    rysujSamolot(s, d[i], g);
                }
            }
            g.DrawLines(Pens.White, new Point[] { new Point(10, 40), new Point(5, 40), new Point(5, rozmiarEkranu.Height - 40), new Point(10, rozmiarEkranu.Height - 40) });
            g.DrawLine(Pens.White, 5, rozmiarEkranu.Height / 2, 10, rozmiarEkranu.Height / 2);
            g.DrawLines(Pens.White, new Point[] { new Point(40, 10), new Point(40, 5), new Point(rozmiarEkranu.Width - 40, 5), new Point(rozmiarEkranu.Width - 40, 10) });
            g.DrawLine(Pens.White, rozmiarEkranu.Width / 2, 5, rozmiarEkranu.Width / 2, 10);
            StringFormat sf = new StringFormat();

            sf.Alignment     = StringAlignment.Near;
            sf.LineAlignment = StringAlignment.Center;
            Point pb1 = pozycjaBezwzgledna(40, 40), pb2 = pozycjaBezwzgledna(rozmiarEkranu.Width / 2, rozmiarEkranu.Height / 2), pb3 = pozycjaBezwzgledna(rozmiarEkranu.Width - 40, rozmiarEkranu.Height - 40);

            g.DrawString(pb1.Y.ToString(), Font, Brushes.White, new Rectangle(12, 40, 0, 0), sf);
            g.DrawString(pb2.Y.ToString(), Font, Brushes.White, new Rectangle(12, rozmiarEkranu.Height / 2, 0, 0), sf);
            g.DrawString(pb3.Y.ToString(), Font, Brushes.White, new Rectangle(12, rozmiarEkranu.Height - 40, 0, 0), sf);
            sf.Alignment     = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Near;
            g.DrawString(pb1.X.ToString(), Font, Brushes.White, new Rectangle(40, 12, 0, 0), sf);
            g.DrawString(pb2.X.ToString(), Font, Brushes.White, new Rectangle(rozmiarEkranu.Width / 2, 12, 0, 0), sf);
            g.DrawString(pb3.X.ToString(), Font, Brushes.White, new Rectangle(rozmiarEkranu.Width - 40, 12, 0, 0), sf);
        }
Example #16
0
        public bool WczytajPlik()
        {
            OpenFileDialog dialog = new OpenFileDialog {
                Filter = "Mapa Kontroli Lotu (*.mkl)|*.mkl"
            };

            if (dialog.ShowDialog() != DialogResult.OK)
            {
                return(false);
            }
            string path = dialog.FileName;

            byte[] file;
            try { file = System.IO.File.ReadAllBytes(path); }
            catch
            {
                MessageBox.Show("Nie udało się otworzyć pliku!", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return(false);
            }
            List <Obiekt> noweObiekty = new List <Obiekt>();

            try
            {
                int onLength = BitConverter.ToInt32(file, 0);
                int pos      = 4;
                for (int i = 0; i < onLength; i++)
                {
                    int nazwaLength = BitConverter.ToInt32(file, pos);
                    pos += 4;
                    string nazwa = Encoding.Default.GetString(file, pos, nazwaLength);
                    pos += nazwaLength;
                    double x = BitConverter.ToDouble(file, pos);
                    pos += 8;
                    double y = BitConverter.ToDouble(file, pos);
                    pos += 8;
                    noweObiekty.Add(new ObiektNaziemny(nazwa, x, y));
                }
                int sLength = BitConverter.ToInt32(file, pos);
                pos += 4;
                for (int i = 0; i < sLength; i++)
                {
                    int nr = BitConverter.ToInt32(file, pos);
                    pos += 4;
                    double x = BitConverter.ToDouble(file, pos);
                    pos += 8;
                    double y = BitConverter.ToDouble(file, pos);
                    pos += 8;
                    double wysokosc = BitConverter.ToDouble(file, pos);
                    pos += 8;
                    double predkosc = BitConverter.ToDouble(file, pos);
                    pos += 8;
                    double kierunek = BitConverter.ToDouble(file, pos);
                    pos += 8;
                    double wDocelowa = BitConverter.ToDouble(file, pos);
                    pos += 8;
                    double pDocelowa = BitConverter.ToDouble(file, pos);
                    pos += 8;
                    Samolot s = new Samolot(nr, x, y, wysokosc, predkosc, kierunek);
                    s.WysokoscDocelowa = wDocelowa;
                    s.PredkoscDocelowa = pDocelowa;
                    noweObiekty.Add(s);
                }
            }
            catch
            {
                MessageBox.Show("Niepoprawny format pliku!", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return(false);
            }
            Obiekty.Clear();
            foreach (Obiekt o in noweObiekty)
            {
                DodajObiekt(o);
            }
            return(true);
        }