Esempio 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]);
                }

            }
        }
Esempio n. 2
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));
            }
        }
Esempio n. 3
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;
 }
Esempio n. 4
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();
        }
Esempio n. 5
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;
        }