コード例 #1
0
ファイル: PolygonDoc.cs プロジェクト: AtanasK/VP
        public void Click(Point position)
        {
            if (firstPoint.IsEmpty)
            {
                firstPoint = position;
                currentPolygon = new Polygon(Color);
                currentPolygon.AddPoint(position);
            }
            else
            {
                currentPolygon.AddPoint(position);
                if (isHit)
                {
                    currentPolygon.IsClosed = true;
                    poligons.Add(currentPolygon);
                    firstPoint = Point.Empty;
                }

            }
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: BojanaT/VP
 private void newDoc()
 {
     askToSave();
     FileName = "Untitled";
     polygonDoc = new PolygonDoc();
     currentPolygon = null;
     startPoint = Point.Empty;
     previousPoint = Point.Empty;
     setTitle();
 }
コード例 #3
0
ファイル: Form1.cs プロジェクト: BojanaT/VP
 private void Form1_MouseClick(object sender, MouseEventArgs e)
 {
     if (startPoint.IsEmpty)
     {
         startPoint = e.Location;
         currentPolygon = new Polygon(currentColor);
         currentPolygon.AddPoint(startPoint);
         previousPoint = startPoint;
     }
     else
     {
         if (drawSquare)
         {
             currentPolygon.IsClosed = true;
             startPoint = Point.Empty;
             previousPoint = Point.Empty;
             drawSquare = false;
             if (currentPolygon.Points.Count >= 3)
             {
                 polygonDoc.AddPolygon(currentPolygon);
                 setTitle();
             }
         }
         else
         {
             currentPolygon.AddPoint(e.Location);
             previousPoint = e.Location;
         }
     }
     Invalidate(true);
 }
コード例 #4
0
        /// <summary>
        /// Кнопка "Выбрать файл"
        /// </summary>
        private void ChooseFile_Click(object sender, RoutedEventArgs e)
        {
            var openFileDialog = new OpenFileDialog();
            var list           = new List <Polygon>(); // список многоугольников

            string[] numbers;                          // массив координат, которые записаны в 1 строке
            double   perimeter;                        // периметр
            Polygon  polygon;

            if (openFileDialog.ShowDialog() == true)
            {
                MessageBox.Show("Числа в файле должны быть"
                                + Environment.NewLine
                                + "записаны в следующем формате:"
                                + Environment.NewLine
                                + "X1 Y1 X2 Y2 X3 Y3", "Формат");
            }

            try
            {
                using (var streamReader = new StreamReader(openFileDialog.FileName)) // открываем файл на чтение
                {
                    while (!streamReader.EndOfStream)
                    {
                        string str = streamReader.ReadLine(); // читаем 1 строку
                        perimeter = 0;                        // обнуляем периметр для нового многоугольника

                        int X1 = 0;
                        int X2 = 0;
                        int Y1 = 0;
                        int Y2 = 0;

                        numbers = str.Split(' ');                               // добавляем каждую коориднату в массив с помощью Split

                        if ((numbers.Length >= 3) && (numbers.Length % 2 == 0)) // если количество вершин >=3 и количество вершин четное количество
                        {
                            for (int i = 0; i <= (numbers.Length - 3); i += 2)  // находим периметр
                            {
                                X1 = int.Parse(numbers[i]);
                                Y1 = int.Parse(numbers[i + 1]);

                                X2 = int.Parse(numbers[i + 2]);
                                Y2 = int.Parse(numbers[i + 3]);

                                double distance = Math.Sqrt(Math.Pow((X2 - X1), 2) + Math.Pow((Y2 - Y1), 2)); // длина отрезка (стороны)

                                perimeter += distance;
                            }

                            perimeter += Math.Sqrt(Math.Pow(X2 - int.Parse(numbers[0]), 2) + Math.Pow((Y2 - int.Parse(numbers[1])), 2));

                            // переменные для нахожения площади многоугольника
                            double sum = 0;

                            for (int i = 0; i <= (numbers.Length - 4); i++) // находим площадь
                            {
                                X1 = int.Parse(numbers[i]);
                                Y1 = int.Parse(numbers[i + 1]);

                                X2 = int.Parse(numbers[i + 2]);
                                Y2 = int.Parse(numbers[i + 3]);

                                sum += X1 * Y2 - X2 * Y1;
                            }

                            //int Y = int.Parse(numbers[3]);

                            //for (int i = 0; i <= (numbers.Length - 4); i += 2)
                            //{
                            //    int X = int.Parse(numbers[i]);
                            //    sum1 += X * Y;
                            //    Y = int.Parse(numbers[i + 2]);
                            //}

                            //int x1 = int.Parse(numbers[2]);

                            //for (int i = 1; i <= (numbers.Length - 4); i += 2)
                            //{
                            //    int y1 = int.Parse(numbers[i]);
                            //    sum2 += y1 * x1;
                            //    x1 = int.Parse(numbers[i + 2]);
                            //}


                            polygon = new Polygon()
                            {
                                CountOfPoints = Convert.ToString(numbers.Length / 2),
                                Perimeter     = Convert.ToString(perimeter),
                                Square        = Convert.ToString(Math.Abs(sum / 2))
                            };

                            list.Add(polygon);
                        }

                        else
                        {
                            polygon = new Polygon()
                            {
                                CountOfPoints = "Ошибка",
                                Perimeter     = "Ошибка",
                                Square        = "Ошибка"
                            };
                            list.Add(polygon);
                        }
                    }
                }
            }
            // обработка исключений
            catch (System.OverflowException)
            {
                MessageBox.Show("Скорее всего произошло переполнение!");
                list = null; // обнуляем список
            }

            catch (System.FormatException)
            {
                MessageBox.Show("Файл не является текстовым!");
                list = null;
            }

            catch (System.ArgumentException)
            {
                MessageBox.Show("Вы не выбрали файл!");
            }

            catch (DirectoryNotFoundException)
            {
                MessageBox.Show("Директория не найдена!");
            }

            catch (FileNotFoundException)
            {
                MessageBox.Show("Файл не найден!");
            }

            catch (IOException)
            {
                MessageBox.Show("Ошибка при вводе-выводе");
            }

            ListView.ItemsSource = list;
        }