/// <summary>
        /// Tìm cái khung chứa đa giác này
        /// </summary>
        private void FindPolygonRegion(clsPolygon polygon)
        {
            double minX = double.MaxValue, minY = double.MaxValue, maxX = double.MinValue, maxY = double.MinValue;

            polygon.PolygonPoints.ForEach(p =>
            {
                if (minX > p.X)
                {
                    minX = p.X;
                }
                if (minY > p.Y)
                {
                    minY = p.Y;
                }
                if (maxX < p.X)
                {
                    maxX = p.X;
                }
                if (maxY < p.Y)
                {
                    maxY = p.Y;
                }
            });
            polygon.p1 = new Point((int)minX, (int)minY);
            polygon.p2 = new Point((int)maxX, (int)maxY);
        }
        private void btnUnGroup_Click(object sender, EventArgs e)
        {
            if (lstGroup.Find(shape => shape.IsSelected) is Group selectedGroup)
            {
                foreach (clsDrawObject shape in selectedGroup.SHAPES)
                {
                    if (shape is clsPolygon)
                    {
                        clsPolygon X = (clsPolygon)shape;

                        Polygons.Add(X);
                    }
                    else if (shape is clsCurve)
                    {
                        LstCurve.Add((clsCurve)shape);
                    }
                    else if (shape is Group)
                    {
                        lstGroup.Add((Group)shape);
                    }
                    else
                    {
                        lstObject.Add(shape);
                    }
                }
                lstGroup.Remove(selectedGroup);
            }
            plMain.Invalidate();
        }
        private void plMain_MouseDown(object sender, MouseEventArgs e)
        {
            this.isPress       = true;
            this.isStartpoly  += 1;
            this.isStartCurve += 1;
            #region Vẽ Hình

            if (this.bLine == true)
            {
                clsDrawObject myObj;
                myObj                 = new clsLine();
                myObj.myPen           = new Pen(myColor, thick);
                myObj.myPen.DashStyle = Dstyle;
                myObj.Filled          = Fill;
                myObj.p1              = e.Location;
                myObj.myOutLineColor  = myColor;
                myObj.width           = thick;
                myObj.DStyle          = Dstyle;
                this.lstObject.Add(myObj);
            }

            if (this.bCircle == true)
            {
                clsDrawObject myObj;
                myObj                = new clsCircle();
                myObj.Filled         = Fill;
                myObj.myOutLineColor = myColor;
                myObj.width          = thick;
                myObj.myFillColor    = myFillColor;
                myObj.DStyle         = Dstyle;
                myObj.BrushTypes     = Getbrush;
                myObj.p1             = e.Location;

                this.lstObject.Add(myObj);
            }
            if (this.bEcllipse == true)
            {
                clsDrawObject myObj;
                myObj                = new clsEllipse();
                myObj.DStyle         = Dstyle;
                myObj.Filled         = Fill;
                myObj.myOutLineColor = myColor;
                myObj.width          = thick;
                myObj.myFillColor    = myFillColor;
                myObj.BrushTypes     = Getbrush;
                myObj.p1             = e.Location;

                this.lstObject.Add(myObj);
            }
            if (this.bSquare == true)
            {
                clsDrawObject myObj;
                myObj = new clsSquare();

                myObj.myOutLineColor = myColor;
                myObj.width          = thick;
                myObj.myFillColor    = myFillColor;
                myObj.DStyle         = Dstyle;
                myObj.BrushTypes     = Getbrush;
                myObj.Filled         = Fill;
                myObj.p1             = e.Location;
                this.lstObject.Add(myObj);
            }
            if (this.bRect == true)
            {
                clsDrawObject myObj;
                myObj = new clsRectangle();

                myObj.myOutLineColor = myColor;
                myObj.width          = thick;
                myObj.myFillColor    = myFillColor;
                myObj.DStyle         = Dstyle;
                myObj.BrushTypes     = Getbrush;
                myObj.Filled         = Fill;
                myObj.p1             = e.Location;

                this.lstObject.Add(myObj);
            }
            if (this.bPolygon == true)
            {
                if (isStartpoly == 1)
                {
                    clsPolygon mypoly;
                    mypoly = new clsPolygon();

                    mypoly.Filled         = Fill;
                    mypoly.myOutLineColor = myColor;
                    mypoly.width          = thick;
                    mypoly.myFillColor    = myFillColor;
                    mypoly.DStyle         = Dstyle;
                    mypoly.BrushTypes     = Getbrush;
                    mypoly.PolygonPoints.Add(e.Location);

                    this.Polygons.Add(mypoly);
                }
                this.Polygons[this.Polygons.Count - 1].PolygonPoints.Add(e.Location);
            }
            if (this.bCurve == true)
            {
                if (isStartCurve == 1)
                {
                    clsCurve myCurve;
                    myCurve = new clsCurve();

                    myCurve.Filled         = Fill;
                    myCurve.myOutLineColor = myColor;
                    myCurve.width          = thick;
                    myCurve.myFillColor    = myFillColor;
                    myCurve.DStyle         = Dstyle;
                    myCurve.BrushTypes     = Getbrush;

                    myCurve.Points.Add(e.Location);
                    this.LstCurve.Add(myCurve);
                }
                this.LstCurve[this.LstCurve.Count - 1].Points.Add(e.Location);
            }
            #endregion ;

            #region Không vẽ hình
            if (IsDrawNothing == true)
            {
                if (isControlKeyPress == true)
                {
                    for (int i = 0; i < lstObject.Count; i++)
                    {
                        if (lstObject[i].IsHit(e.Location))
                        {
                            lstObject[i].IsSelected = true;
                            plMain.Refresh();
                        }
                    }
                    for (int i = 0; i < Polygons.Count; i++)
                    {
                        if (Polygons[i].IsHit(e.Location) && bPolygon != true)
                        {
                            Polygons[i].IsSelected = true;
                            plMain.Refresh();
                        }
                    }
                    for (int i = 0; i < LstCurve.Count; i++)
                    {
                        if (LstCurve[i].IsHit(e.Location) && bCurve != true)
                        {
                            LstCurve[i].IsSelected = true;
                            plMain.Refresh();
                        }
                    }
                    for (int i = 0; i < lstGroup.Count; i++)
                    {
                        if (lstGroup[i].IsHit(e.Location))
                        {
                            lstGroup[i].IsSelected = true;
                            plMain.Refresh();
                        }
                    }
                }
                else
                {
                    LstCurve.ForEach(shape => shape.IsSelected  = false);
                    lstObject.ForEach(shape => shape.IsSelected = false);
                    Polygons.ForEach(shape => shape.IsSelected  = false);
                    lstGroup.ForEach(shape => shape.IsSelected  = false);
                    plMain.Invalidate();
                    plMain.Refresh();


                    for (int i = 0; i < lstObject.Count; i++)
                    {
                        if (isHitPoint(e.Location, lstObject[i].p2) || isHitPoint(e.Location, lstObject[i].p1) ||
                            isHitPoint(e.Location, lstObject[i].p3) || isHitPoint(e.Location, lstObject[i].p4))
                        {
                            ReSizeShape             = lstObject[i];
                            lstObject[i].IsSelected = true;
                            lstObject[i].IsReSized  = true;
                            plMain.Refresh();
                            break;
                        }
                        if (lstObject[i].IsHit(e.Location))
                        {
                            selectedShape           = lstObject[i];
                            lstObject[i].IsSelected = true;
                            plMain.Refresh();
                            break;
                        }
                    }
                    for (int i = 0; i < Polygons.Count; i++)
                    {
                        FindPolygonRegion(Polygons[i]);
                        if (isHitPoint(e.Location, Polygons[i].p2) || isHitPoint(e.Location, Polygons[i].p1))
                        {
                            if (isHitPoint(e.Location, Polygons[i].p1))
                            {
                                position = 1;
                            }
                            if (isHitPoint(e.Location, Polygons[i].p2))
                            {
                                position = 2;
                            }
                            ReSizeShape            = Polygons[i];
                            Polygons[i].IsSelected = true;
                            Polygons[i].IsReSized  = true;
                            plMain.Refresh();
                            break;
                        }
                        if (Polygons[i].IsHit(e.Location))
                        {
                            selectedShape          = Polygons[i];
                            Polygons[i].IsSelected = true;
                            plMain.Refresh();
                            break;
                        }
                    }
                    for (int i = 0; i < LstCurve.Count; i++)
                    {
                        FindCurveRegion(LstCurve[i]);
                        if (isHitPoint(e.Location, LstCurve[i].p2) || isHitPoint(e.Location, LstCurve[i].p1))
                        {
                            if (isHitPoint(e.Location, LstCurve[i].p1))
                            {
                                position = 1;
                            }
                            if (isHitPoint(e.Location, LstCurve[i].p2))
                            {
                                position = 2;
                            }
                            ReSizeShape            = LstCurve[i];
                            LstCurve[i].IsSelected = true;
                            LstCurve[i].IsReSized  = true;
                            plMain.Refresh();
                            break;
                        }
                        if (LstCurve[i].IsHit(e.Location))
                        {
                            selectedShape          = LstCurve[i];
                            LstCurve[i].IsSelected = true;
                            plMain.Refresh();
                            break;
                        }
                    }
                    for (int i = 0; i < lstGroup.Count; i++)
                    {
                        FindGroupRegion(lstGroup[i]);
                        if (isHitPoint(e.Location, lstGroup[i].p2))
                        {
                            ReSizeShape            = lstGroup[i];
                            lstGroup[i].IsSelected = true;
                            lstGroup[i].IsReSized  = true;
                            plMain.Refresh();
                            break;
                        }
                        if (lstGroup[i].IsHit(e.Location))
                        {
                            selectedShape          = lstGroup[i];
                            lstGroup[i].IsSelected = true;
                            plMain.Refresh();
                            break;
                        }
                    }
                    if (selectedShape != null)
                    {
                        isMovingShape = true;

                        previousPoint = e.Location;
                    }
                    else
                    if (ReSizeShape != null)
                    {
                        isResizingShape = true;
                        previousPoint   = e.Location;
                        if (position == 2)
                        {
                            Polybegin = ReSizeShape.p1;
                            PolyEnd   = ReSizeShape.p2;
                        }
                        if (position == 1)
                        {
                            Polybegin = ReSizeShape.p2;
                            PolyEnd   = ReSizeShape.p1;
                        }
                    }
                    else
                    {
                        isMouseSelect  = true;
                        selectedRegion = new System.Drawing.Rectangle(e.Location, new Size(0, 0));
                    }
                }
            }
            #endregion
        }