Ejemplo n.º 1
0
        private void MergeInnerWithInner(Polygon WindowHole, Polygon PolygonHole)
        {
            PolygonMerger PM = new PolygonMerger(WindowHole, PolygonHole, MergedContoursType.INNER_WITH_INNER/* 3*/);

            List<Polygon> contoursToAdd = PM.MakeUnion();
            if (PM.NoIntersect)
            {
                if (Utility.PointInPolygon(WindowHole, PolygonHole.GetVertex(0)) == Utility.Inside)
                {
                    RESULT.AddSubPolygon(PolygonHole);
                }
                else if (Utility.PointInPolygon(PolygonHole, WindowHole.GetVertex(0)) == Utility.Inside)
                {
                    RESULT.AddSubPolygon(WindowHole);
                }
            }
            else
            {
                for (int i = 0; i < contoursToAdd.Count; i++)
                {
                    RESULT.AddSubPolygon(contoursToAdd[i]);
                }

            }
        }
Ejemplo n.º 2
0
 public void AddPoint(EventArgs e)
 {
     EditorState edState = new AddingPoints();
     State = edState;
     CurPoints.Clear();
     CurContour = null;
 }
Ejemplo n.º 3
0
        public Polygon GetPolygonByName(string s)
        {
            DBase db = new DBase(DataContextConnection);

            Polygon poly = new Polygon();

            // construct polygon
            Polygons DBpoly = (from c in db.Polygons
                                         where c.NAME==s && c.PARENT_ID==null
                                         select c).SingleOrDefault<Polygons>();

            foreach (Points pnt in DBpoly.Points)
            {
                Point p = new Point(pnt.X,pnt.Y);
                poly.AddVertex(p);
            }
            // construct subPolygons
            IQueryable <Polygons> DBsubpolys = from c in db.Polygons
                              where c.PARENT_ID == DBpoly.ID
                              select c;

            foreach (Polygons DBsubpoly in DBsubpolys)
            {
                Polygon subpoly = new Polygon();

                foreach (Points pnt in DBsubpoly.Points)
                {
                    Point p = new Point(pnt.X, pnt.Y);
                    subpoly.AddVertex(p);
                }
                poly.AddSubPolygon(subpoly);
            }

            return poly;
        }
Ejemplo n.º 4
0
        public void AddOuterContour()
        {
            Polygon polyg = new Polygon();
            Polygs.Add(polyg);
            CurPolygon = polyg;

            EditorState edState = new NewPolygon();
            State = edState;
        }
Ejemplo n.º 5
0
 public static double PolygonSquare(Polygon Polyg)
 {
     double sum = 0;
     for (int i = 0; i < Polyg.VertexCount; i++)
     {
         int j = i + 1;
         if (i == Polyg.VertexCount - 1) j = 0;
         sum += Polyg.GetVertex(i).X * Polyg.GetVertex(j).Y - Polyg.GetVertex(j).X * Polyg.GetVertex(i).Y;
     }
     return sum/2;
 }
Ejemplo n.º 6
0
        public void AddInnerContour()
        {
            if (CurPolygon == null)
            {
                MessageBox.Show("Внешний контур не задан!");
                return;
            }
            Polygon polyg = new Polygon();
            CurContour = polyg;
               // Polygs.Add(polyg);
            CurPolygon.AddSubPolygon(polyg);

            EditorState edState = new NewContour();
            State = edState;
        }
Ejemplo n.º 7
0
        private void AddNewPoint(ImageEditor imgEd, MouseEventArgs e, Polygon polyg)
        {
            int pos = Math.Max(imgEd.CurPoints[0], imgEd.CurPoints[1]);
            if (pos == polyg.VertexCount-1) pos++;

            Point E = imgEd.ShiftPointIfNotEmpty(new Point(e.X, e.Y));
            Graphics g = imgEd.GetGraphics();
            polyg.Paint(g, new Pen(Brushes.White, 2));
            polyg.AddVertexAt(pos, E);
            imgEd.ReDraw();
            g = imgEd.GetGraphics();
            polyg.Paint(g);
            imgEd.ReDraw();

            imgEd.CurPoints.Clear();
        }
Ejemplo n.º 8
0
        public Polygon(string[] lines)
        {
            VertexList = new List<Point>();
            SubPolygs = new List<Polygon>();
            if (lines.Length <= 3) return;

            int vertCount = 0, subCount = 0;
            Int32.TryParse(lines[1], out vertCount);
            Int32.TryParse(lines[2], out subCount);
            int shift = 3, i = 0;
            for (i = 0; i < vertCount; i++)
            {
                System.Windows.Point pw = System.Windows.Point.Parse(lines[i + shift]);
                Point p = new Point((int)pw.X,(int) pw.Y);
                VertexList.Add(p);
            }
            shift += i;
            for (int k = 0; k < subCount; k++)
            {
                Polygon sub = new Polygon();
                SubPolygs.Add(sub);
                shift ++;

                int vertSubCount = 0;
                Int32.TryParse(lines[shift], out vertSubCount);
                shift++;

                int j = 0;
                for (j = 0; j < vertSubCount; j++)
                {
                    System.Windows.Point pw = System.Windows.Point.Parse(lines[j + shift]);
                    Point p = new Point((int)pw.X, (int)pw.Y);
                    sub.VertexList.Add(p);
                }
                shift += j;
            }
        }
Ejemplo n.º 9
0
 public WholeMerger(Polygon _WINDOW, Polygon _POLYGON)
 {
     WINDOW = _WINDOW;
     POLYGON = _POLYGON;
     RESULT = new Polygon();
 }
Ejemplo n.º 10
0
 public void AddSubPolygon(Polygon subpolyg)
 {
     SubPolygs.Add(subpolyg);
 }
Ejemplo n.º 11
0
        public void PaintOne(Polygon p, Graphics g, Pen pen)
        {
            for (int i = 0; i < p.VertexCount; i++)
            {
                int prev = 0;
                if (i != 0) prev = i - 1;
                else prev = p.VertexCount - 1;

                g.DrawLine(pen, new Point(p.GetVertex(prev).X, p.GetVertex(prev).Y), new Point(p.GetVertex(i).X, p.GetVertex(i).Y));
            }
        }
Ejemplo n.º 12
0
        public void ImageClean()
        {
            CurPolygon = null;
            Polygs.Clear();
            CurPoints.Clear();
            EditingOff();

            g = GetGraphics();
            g.Clear(System.Drawing.Color.White);
            ReDraw();

            NotValidated = false;
        }
Ejemplo n.º 13
0
 public PolygonValidater(Polygon p)
 {
     polygon = p;
     segments = new List<Segment>();
 }
Ejemplo n.º 14
0
        public void Merge()
        {
            if (Polygs.Count < PolygonMaxNumber)
            { MessageBox.Show("Для объединения необходимо создать " + PolygonMaxNumber.ToString() + " полигона"); return; }

            WholeMerger WholeMerg = new WholeMerger(Polygs[0], Polygs[1]);
            WholeMerg.Merge();
            if (WholeMerg.noSharedPoints) {ImageClean(); return; }
            Polygon United = WholeMerg.GetResult();
            CurPolygon = United;

            Polygs.Clear();
            Polygs.Add(United);

            g = GetGraphics();
            g.Clear(System.Drawing.Color.White);
            ReDraw();

            g = GetGraphics();
            United.Paint(g);
            ReDraw();
        }
Ejemplo n.º 15
0
        public void SavePolygon(Polygon polyg, string name)
        {
            DBase db = new DBase(DataContextConnection);
            // insert polygon
            Polygons DBpoly = new Polygons
            {
                NAME = name,
            };

            for (int i = 0; i < polyg.VertexCount; i++)
            {
                Points p = new Points
                {
                    X = polyg.GetVertex(i).X,

                    Y = polyg.GetVertex(i).Y,

                    POLYGON_ID = DBpoly.ID,
                };
                DBpoly.Points.Add(p);
            }
            db.Polygons.InsertOnSubmit(DBpoly);
            db.SubmitChanges();

            // insert subPolygons
            for (int i = 0; i < polyg.SubPolygs.Count; i++)
            {
                Polygons DBsubpoly = new Polygons
                {
                    NAME = name,
                    PARENT_ID = DBpoly.ID,
                };

                for (int j = 0; j < polyg.SubPolygs[i].VertexCount; j++)
                {
                    Points sp = new Points
                    {
                        X = polyg.SubPolygs[i].GetVertex(j).X,

                        Y = polyg.SubPolygs[i].GetVertex(j).Y,

                        POLYGON_ID = DBsubpoly.ID,
                    };
                    DBsubpoly.Points.Add(sp);
                }
                db.Polygons.InsertOnSubmit(DBsubpoly);
            }

            db.SubmitChanges();
        }
Ejemplo n.º 16
0
 // constructor
 public UtilityTest()
 {
     // create all polygons we need
     Polygon_ForGeneralCases = InitializePolygon_ForGeneralCases();
     Polygon_ForSpecialCases = InitializePolygon_ForSpecialCases();
 }
Ejemplo n.º 17
0
        private Polygon InitializePolygon_ForSpecialCases()
        {
            Polygon Polyg = new Polygon();

            Polyg.AddVertex(new Point(327, 233));
            Polyg.AddVertex(new Point(311, 280));
            Polyg.AddVertex(new Point(232, 252));
            Polyg.AddVertex(new Point(197, 185));
            Polyg.AddVertex(new Point(126, 147));
            Polyg.AddVertex(new Point(197, 128));
            Polyg.AddVertex(new Point(232, 145));
            Polyg.AddVertex(new Point(232, 125));
            Polyg.AddVertex(new Point(327, 125));

            return Polyg;
        }
Ejemplo n.º 18
0
        private Polygon InitializePolygon_ForGeneralCases()
        {
            Polygon Polyg = new Polygon();
            Polyg.AddVertex(new Point(235, 47));
            Polyg.AddVertex(new Point(408, 47));
            Polyg.AddVertex(new Point(370, 195));
            Polyg.AddVertex(new Point(238, 113));
            Polyg.AddVertex(new Point(112, 208));
            Polyg.AddVertex(new Point(112, 158));
            Polyg.AddVertex(new Point(152, 115));
            Polyg.AddVertex(new Point(112, 105));
            Polyg.AddVertex(new Point(112, 76));

            return Polyg;
        }
Ejemplo n.º 19
0
 protected bool WhereIsThePoint(MouseEventArgs e, ImageEditor imgEd, out Polygon p, out int index)
 {
     int mouseSensitivity = imgEd.mouseSensitivity;
     p = null;
     index = -1;
     for (int i = 0; i < imgEd.CurPolygon.VertexCount; i++)
     {
         if (Math.Abs(e.X - imgEd.CurPolygon.GetVertex(i).X) <= mouseSensitivity
             && Math.Abs(e.Y - imgEd.CurPolygon.GetVertex(i).Y) <= mouseSensitivity)
         {
             p = imgEd.CurPolygon;
             index = i;
             return true;
         }
     }
     for (int i = 0; i < imgEd.CurPolygon.SubPolygs.Count; i++)
     {
         for (int j = 0; j < imgEd.CurPolygon.SubPolygs[i].VertexCount; j++)
         {
             if (Math.Abs(e.X - imgEd.CurPolygon.SubPolygs[i].GetVertex(j).X) <= mouseSensitivity
                 && Math.Abs(e.Y - imgEd.CurPolygon.SubPolygs[i].GetVertex(j).Y) <= mouseSensitivity)
             {
                 p = imgEd.CurPolygon.SubPolygs[i];
                 index = j;
                 return true;
             }
         }
     }
     MessageBox.Show("Указанная точка не совпадает ни с одной из вершин редактируемого полигона! Можно снизить чувствительность курсора");
     return false;
 }
Ejemplo n.º 20
0
 public void DeletePoint(EventArgs e)
 {
     EditorState edState = new DeletingPoints();
     State = edState;
     CurContour = null;
 }
Ejemplo n.º 21
0
        private void открытьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ImgEditor.Polygs.Count == 2) { MessageBox.Show("Вначале объедините существующие полигоны"); return; }

            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "Выберите файл";
            openFileDialog.Filter = "Текстовые файлы|*.txt";

            if (openFileDialog.ShowDialog() != DialogResult.OK) return;
            string[] lines = ToFileFromFile.ReadFromFile(openFileDialog.FileName);

            Polygon poly = new Polygon(lines);
            Graphics g = ImgEditor.GetGraphics();
            poly.Paint(g);
            ImgEditor.ReDraw();

            ImgEditor.Polygs.Add(poly);
            ImgEditor.CurPolygon = poly;
        }
Ejemplo n.º 22
0
        public static int PointInPolygon(Polygon Polyg, Point a)
        {
            // из точки проводим луч вертикально вниз и считаем пересечения со сторонами полигона
            int intersectNum = 0;
            bool EndIntersect = false, SecondLoop = false;
            int SegStartX = 0;

            for (int i = 0; i < Polyg.VertexCount; i++)
            {
                int j = i + 1;
                if (i == Polyg.VertexCount - 1) j = 0;
                // точка совпала с одним из концов отрезка
                if (a == Polyg.GetVertex(i) || a == Polyg.GetVertex(j)) return OnBorder;
                // начало и конец отрезка лежат по одну сторону от луча
                int Min_X = Math.Min(Polyg.GetVertex(i).X, Polyg.GetVertex(j).X);
                int Max_X = Math.Max(Polyg.GetVertex(i).X, Polyg.GetVertex(j).X);
                if (a.X < Min_X || a.X > Max_X) continue;

                if (a.Y == Polyg.GetVertex(i).Y && a.Y == Polyg.GetVertex(j).Y) return OnBorder;

                double X_dif = (double)(Polyg.GetVertex(j).X - Polyg.GetVertex(i).X);
                if (X_dif !=0 )
                {
                    double _y = Polyg.GetVertex(i).Y +
                        (a.X - Polyg.GetVertex(i).X) * (Polyg.GetVertex(j).Y - Polyg.GetVertex(i).Y) / X_dif;

                    if (a.Y <= _y)
                    {
                        // точки начала и конца лежат на луче
                        if (Polyg.GetVertex(i).X == a.X && Polyg.GetVertex(j).X == a.X) continue;
                        // конец отрезка лежит на луче
                        if (Polyg.GetVertex(j).X == a.X)
                        {
                            SegStartX = Polyg.GetVertex(i).X;
                            EndIntersect = true;
                        }
                        else
                        {
                            if (EndIntersect)
                            {
                                if (a.X > Math.Min(SegStartX, Polyg.GetVertex(j).X)
                                    && a.X < Math.Max(SegStartX, Polyg.GetVertex(j).X))
                                {
                                    intersectNum++;
                                    if (SecondLoop) { intersectNum--; break; }
                                }
                                else if (SecondLoop) {  intersectNum--; break;}
                                EndIntersect = false;
                            }
                            else intersectNum++;
                        }
                    }
                }

                if (j == Polyg.VertexCount - 1 && EndIntersect)
                {
                    i = -1;
                    SecondLoop = true;
                }
            }

            if (intersectNum % 2 == 1) return Inside;
            else return Outside;
        }