Ejemplo n.º 1
0
        public IPositionSet GetRamSet2()
        {
            List <IPosition> lst  = new List <IPosition>();
            Position_Point   ipos = null;

            ipos = new Position_Point();
            ipos.SetX(10);
            ipos.SetY(20);
            lst.Add(ipos);
            ipos = new Position_Point();
            ipos.SetX(50);
            ipos.SetY(70);
            lst.Add(ipos);
            ipos = new Position_Point();
            ipos.SetX(50);
            ipos.SetY(90);
            lst.Add(ipos);
            ipos = new Position_Point();
            ipos.SetX(25);
            ipos.SetY(30);
            lst.Add(ipos);
            PositionSetEdit_ImplementByICollectionTemplate pSet = new PositionSetEdit_ImplementByICollectionTemplate(lst);

            return(pSet);
        }
Ejemplo n.º 2
0
        public IPositionSet WriteLineInGrid(float gridWidth, float gridHeight, IPosition startPosition, IPosition endPosition)
        {
            Position_Point refPos = new Position_Point();

            refPos.SetX(0);
            refPos.SetY(0);
            return(getBlockLine(refPos, gridWidth, startPosition, endPosition));
        }
Ejemplo n.º 3
0
        private void button1_Click_1(object sender, EventArgs e)
        {
            //取得测试数据
            float rx  = float.Parse(txtRX.Text);
            float ry  = float.Parse(txtRY.Text);
            float sx  = float.Parse(txtSX.Text);
            float sy  = float.Parse(txtSY.Text);
            float ex  = float.Parse(txtEX.Text);
            float ey  = float.Parse(txtEY.Text);
            float itv = float.Parse(txtItv.Text);

            //求解
            Position_Point rp = new Position_Point(rx, ry);
            Position_Point sp = new Position_Point(sx, sy);
            Position_Point ep = new Position_Point(ex, ey);
            IPositionSet   ps = BlockLine.getBlockLine(rp, itv, sp, ep);

            //作图
            pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            Graphics g = Graphics.FromImage(pictureBox1.Image);

            g.Clear(Color.White);
            //网格线
            for (float x = rx - itv; x >= 0; x -= itv)
            {
                g.DrawLine(Pens.Black, x, 0, x, pictureBox1.Height);
            }
            for (float y = ry - itv; y >= 0; y -= itv)
            {
                g.DrawLine(Pens.Black, 0, y, pictureBox1.Width, y);
            }
            for (float x = rx; x < pictureBox1.Width; x += itv)
            {
                g.DrawLine(Pens.Black, x, 0, x, pictureBox1.Height);
            }
            for (float y = ry; y < pictureBox1.Width; y += itv)
            {
                g.DrawLine(Pens.Black, 0, y, pictureBox1.Width, y);
            }
            //经过的网格
            ps.InitToTraverseSet();
            while (ps.NextPosition())
            {
                IPosition p = ps.GetPosition();
                float     x = p.GetX();
                float     y = p.GetY();
                g.FillRectangle(Brushes.Gray, x - itv / 2 + 1, y - itv / 2 + 1, itv - 1, itv - 1);
            }
            //起点和终点
            g.FillEllipse(Brushes.Black, sx - 2, sy - 2, 4, 4);
            g.FillEllipse(Brushes.Black, ex - 2, ey - 2, 4, 4);
            g.DrawLine(Pens.Red, sx, sy, ex, ey);
        }
Ejemplo n.º 4
0
        public IPositionSet GetRamSet(int wRank, int hRank)
        {
            List <IPosition> lst  = new List <IPosition>();
            Position_Point   ipos = null;

            for (int i = 0; i < 100; i++)
            {
                ipos = new Position_Point();
                ipos.SetX(RandomMakerAlgorithm.RandomMaker.RapidBetween(0, wRank));
                ipos.SetY(RandomMakerAlgorithm.RandomMaker.RapidBetween(0, hRank));
                lst.Add(ipos);
            }
            PositionSetEdit_ImplementByICollectionTemplate pSet = new PositionSetEdit_ImplementByICollectionTemplate(lst);

            return(pSet);
        }
Ejemplo n.º 5
0
        //根据函数来作图
        public void draw(double[] xData)
        {
            List <IPosition>[] pl = new List <IPosition> [nameList.Count]; //new List<IPosition>()[xData.Length];

            for (int i = 0; i < nameList.Count; i++)
            {
                pl[i] = new List <IPosition>();
            }

            for (int j = 0; j < xData.Length; j++)
            {
                //对每个函数分发同一个object
                Object temp = null;

                if (getPublicObjectFunc != null)
                {
                    temp = getPublicObjectFunc(xData[j]);
                }

                //先对每一个函数计算同一个规模的输入的函数值
                for (int i = 0; i < nameList.Count; i++)
                {
                    Position_Point p = new Position_Point();
                    p.SetX((float)(xData[j]));

                    if (getPublicObjectFunc == null)
                    {
                        p.SetY((float)(funcList[i](xData[j])));
                    }
                    else
                    {
                        p.SetY((float)(funcWithObjectList[i](xData[j], temp)));
                    }

                    pl[i].Add(p);
                }
            }

            for (int i = 0; i < nameList.Count; i++)
            {
                curvePlot.addCurve(nameList[i], new PositionSet_ImplementByIEnumerableTemplate(pl[i]));
            }

            curvePlot.draw();
        }
Ejemplo n.º 6
0
 public void draw_Old(double[] xData)
 {
     for (int i = 0; i < nameList.Count; i++)
     {
         List <IPosition> pl    = new List <IPosition>();
         double[]         yData = new double[xData.Length];
         string           name  = nameList[i];
         Func             func  = funcList[i];
         for (int j = 0; j < xData.Length; j++)
         {
             Position_Point p = new Position_Point();
             p.SetX((float)(xData[j]));
             p.SetY((float)(func(xData[j])));
             pl.Add(p);
         }
         curvePlot.addCurve(name, new PositionSet_ImplementByIEnumerableTemplate(pl));
     }
     curvePlot.draw();
 }
        private void SelectBotton_Click(object sender, EventArgs e)
        {
            layersPaintedControl.state_PainterForm_MouseDown = LayersPaintedControl.State_PainterForm_MouseDown.onDaggle;
            ResetEventHandler();
            //layersPainterForm.state_PainterForm_MouseDown = LayersPainterForm.State_PainterForm_MouseDown.Other;
            IPosition nearestPoint = new Position_Point(0, 0);
            PositionSetEdit_ImplementByICollectionTemplate positionSetEdit_ImplementByICollectionTemplate = new PositionSetEdit_ImplementByICollectionTemplate();

            positionSetEdit_ImplementByICollectionTemplate.AddPosition(nearestPoint);
            Layer_PositionSet_Square layer = new Layer_PositionSet_Square(positionSetEdit_ImplementByICollectionTemplate);

            MouseMovedRealPosition = delegate(IPosition position)
            {
                positionSetEdit_ImplementByICollectionTemplate.RemovePosition(nearestPoint);
                nearestPoint = m2m_NN.NearestNeighbor(position);
                positionSetEdit_ImplementByICollectionTemplate.AddPosition(nearestPoint);
                layer.SpringLayerRepresentationChangedEvent(layer);
                layersPaintedControl.Invalidate();
            };

            layer.VisibleInOptDlg = false;
            layer.Active          = true;
            layer.Name            = "Edited Figure";
            layer.SquareFrameDrawer.RectangleRadiusWidth = 8;
            layer.SquareFrameDrawer.LineWidth            = 1;
            layer.SquareFrameDrawer.LineColor            = Color.YellowGreen;
            layer.CenterPointCoordinate.Visible          = true;
            layer.CenterPointCoordinate.CoordinateFont   = new Font(layer.CenterPointCoordinate.CoordinateFont, FontStyle.Bold);

            //layer.Point.IsDrawPointBorder = true;
            //layer.Point.PointColor = Color.Red;
            //layer.Point.PointRadius = 3;
            //layer.PointCoordinate.CoordinateFont = new Font(layer.PointCoordinate.CoordinateFont, FontStyle.Bold);
            //layer.PointCoordinate.Visible = true;

            if (currentLayer != null)
            {
                layers.Remove(currentLayer);
            }
            currentLayer = layer;
            layers.Add(layer);
        }
        public IPosition GetMouseDoubleChickedRealPosition()
        {
            System.Threading.Thread currentThread            = System.Threading.Thread.CurrentThread;
            Position_Point          mouseChickedRealPosition = null;

            this.BeginInvoke(new dShow(MessageBox.Show), new object[] { "please double chick on the screen." });

            MouseDoubleClick += delegate(object sender, MouseEventArgs e)
            {
                mouseChickedRealPosition = new Position_Point();
                mouseChickedRealPosition.SetX(ConvertMouseXToRealX(e.X));
                mouseChickedRealPosition.SetY(ConvertMouseYToRealY(e.Y));

                if ((currentThread.ThreadState & System.Threading.ThreadState.Suspended) == System.Threading.ThreadState.Suspended)
                {
                    currentThread.Resume();
                }
            };

            currentThread.Suspend();
            return(mouseChickedRealPosition);
        }
Ejemplo n.º 9
0
        public IPositionSet QueryConvexHull()
        {
            #region code for algorithm demo
            if (GetChildPositionSetInSpecificLevelOfConvexHull != null)
            {
                GetChildPositionSetInSpecificLevelOfConvexHull(m2mStructure.GetLevel(1), 1, m2mStructure.GetChildPositionSetByParentPart(0, m2mStructure.GetLevel(0).GetPartRefByPartIndex(0, 0)));
            }
            #endregion

            IPositionSet PositionSetInConvexHull = convexHullEngine.ConvexHull(
                m2mStructure.GetChildPositionSetByParentPart(0, m2mStructure.GetLevel(0).GetPartRefByPartIndex(0, 0)));

            for (int i = 1; i < m2mStructure.GetLevelNum(); i++)
            {
                ILevel currentLevel = m2mStructure.GetLevel(i);

                #region code for algorithm demo
                if (GetConvexHullPositionSetInSpecificLevel != null)
                {
                    GetConvexHullPositionSetInSpecificLevel(currentLevel, i, PositionSetInConvexHull);
                }
                #endregion

                tempChildPositionList = new List <IPosition>();

                tempChildPositionList.Capacity = 4 * PositionSetInConvexHull.GetNum();

                PositionSetInConvexHull.InitToTraverseSet();

                IPosition hullStartPoint = null;
                IPosition temp1          = null;
                IPosition temp2          = null;

                if (PositionSetInConvexHull.NextPosition())
                {
                    temp1          = PositionSetInConvexHull.GetPosition();
                    hullStartPoint = temp1;
                    AddChildPositionSetToList(i, temp1);

                    #region code for algorithm demo
                    if (GetLinePositionSetInSpecificLevel != null)
                    {
                        linePositionSetInSpecificLevel = new PositionSetEdit_ImplementByICollectionTemplate();
                        linePositionSetInSpecificLevel.AddPosition(new Position_Point(temp1.GetX(), temp1.GetY()));
                    }
                    #endregion
                }

                #region code for algorithm demo
                if (GetRepresentativeHullInSpecificLevel != null)
                {
                    representativeHull = new PositionSetEdit_ImplementByICollectionTemplate();
                }
                #endregion

                bool isWaitingLastPoint = true;

                //遍历该层凸包的每个分块,以确定下一层的候选分块
                while (true)
                {
                    if (isWaitingLastPoint)
                    {
                        if (PositionSetInConvexHull.NextPosition() == false)
                        {
                            isWaitingLastPoint = false;
                            temp2 = hullStartPoint;
                        }
                        else
                        {
                            temp2 = PositionSetInConvexHull.GetPosition();
                            AddChildPositionSetToList(i, temp2);
                        }
                    }
                    else
                    {
                        break;
                    }

                    IPosition start = new Position_Point(currentLevel.ConvertRealValueToRelativeValueX(((IPart)temp1).GetRandomOneFormDescendantPoint().GetX()), currentLevel.ConvertRealValueToRelativeValueY(((IPart)temp1).GetRandomOneFormDescendantPoint().GetY()));
                    IPosition end   = new Position_Point(currentLevel.ConvertRealValueToRelativeValueX(((IPart)temp2).GetRandomOneFormDescendantPoint().GetX()), currentLevel.ConvertRealValueToRelativeValueY(((IPart)temp2).GetRandomOneFormDescendantPoint().GetY()));

                    IPositionSet tempPositionSet = writeLineInGridEngine.WriteLineInGrid(currentLevel.GetGridWidth(), currentLevel.GetGridHeight(), start, end);

                    #region code for algorithm demo
                    if (GetRepresentativeHullInSpecificLevel != null)
                    {
                        representativeHull.AddPosition(start);
                        representativeHull.AddPosition(end);
                        GetRepresentativeHullInSpecificLevel(m2mStructure.GetLevel(i), i, representativeHull);
                    }
                    #endregion

                    tempPositionSet.InitToTraverseSet();

                    while (tempPositionSet.NextPosition())
                    {
                        IPosition tempPosition = tempPositionSet.GetPosition();

                        IPart tempPart = currentLevel.GetPartRefByPartIndex(currentLevel.ConvertRelativeValueToPartSequenceX(tempPosition.GetX()),
                                                                            currentLevel.ConvertRelativeValueToPartSequenceY(tempPosition.GetY()));

                        #region code for algorithm demo
                        if (GetLinePositionSetInSpecificLevel != null)
                        {
                            linePositionSetInSpecificLevel.AddPosition(new Position_Point(currentLevel.ConvertRelativeValueToPartSequenceX(tempPosition.GetX())
                                                                                          , currentLevel.ConvertRelativeValueToPartSequenceY(tempPosition.GetY())));
                        }
                        #endregion

                        if (tempPart != null)
                        {
                            AddChildPositionSetToList(i, tempPart);
                        }
                    }

                    #region code for algorithm demo
                    if (GetLinePositionSetInSpecificLevel != null)
                    {
                        if (isWaitingLastPoint)
                        {
                            linePositionSetInSpecificLevel.AddPosition(new Position_Point(temp2.GetX(), temp2.GetY()));
                        }
                    }
                    #endregion

                    #region code for algorithm demo
                    if (GetLinePositionSetInSpecificLevel != null)
                    {
                        GetLinePositionSetInSpecificLevel(m2mStructure.GetLevel(i), i, linePositionSetInSpecificLevel);
                    }
                    #endregion

                    temp1 = temp2;
                }

                IPositionSet ChildPositionSetInConvexHull = new PositionSetEdit_ImplementByICollectionTemplate(tempChildPositionList);

                #region code for algorithm demo
                if (GetChildPositionSetInSpecificLevelOfConvexHull != null)
                {
                    if (i < m2mStructure.GetLevelNum() - 1)
                    {
                        GetChildPositionSetInSpecificLevelOfConvexHull(m2mStructure.GetLevel(i + 1), i + 1, ChildPositionSetInConvexHull);
                    }
                }
                #endregion

                PositionSetInConvexHull =
                    convexHullEngine.ConvexHull(ChildPositionSetInConvexHull);
            }

            #region code for algorithm demo
            if (GetRealConvexHull != null)
            {
                GetRealConvexHull(PositionSetInConvexHull);
            }
            #endregion

            return(PositionSetInConvexHull);
        }
Ejemplo n.º 10
0
 public void SetRelativelyPoint(Position_Point relativelyPoint)
 {
     this.relativelyPoint = relativelyPoint;
 }
Ejemplo n.º 11
0
        public void PreProcess(PositionSetEditSet pointList)
        {
            #region code for algorithm demo
            if (GetPositionSetToGetConvexHull != null)
            {
                GetPositionSetToGetConvexHull(pointList);
            }
            #endregion

            //请在这里改变不同的m2mStructure实现。
            M2MSCreater_ForGeneralM2MStruture m2m_Creater_ForGeneralM2MStruture = new M2MSCreater_ForGeneralM2MStruture();

            m2mStructure = m2m_Creater_ForGeneralM2MStruture.CreateAutomatically(pointList, levelNum);

            PositionSetEdit_ImplementByICollectionTemplate positionSet = new PositionSetEdit_ImplementByICollectionTemplate();



            ILevel bottomLevel = m2mStructure.GetLevel(m2mStructure.GetLevelNum() - 1);

            for (int i = 0; i < pointList.GetPositionSetNum(); i++)
            {
                IPosition start = new Position_Point();
                IPosition end   = new Position_Point();
                IPosition first = new Position_Point();

                IPositionSet positionListTemp = pointList.GetNthPositionSet(i);

                positionListTemp.InitToTraverseSet();
                if (positionListTemp.NextPosition())
                {
                    start = new Position_Point(bottomLevel.ConvertRealValueToRelativeValueX(positionListTemp.GetPosition().GetX()), bottomLevel.ConvertRealValueToRelativeValueY(positionListTemp.GetPosition().GetY()));
                    first = start;
                    positionSet.AddPosition(start);
                }
                while (positionListTemp.NextPosition())
                {
                    end = new Position_Point(bottomLevel.ConvertRealValueToRelativeValueX(positionListTemp.GetPosition().GetX()), bottomLevel.ConvertRealValueToRelativeValueY(positionListTemp.GetPosition().GetY()));
                    IPositionSet positionSetTemp = writeLineInGridEngine.WriteLineInGrid(bottomLevel.GetGridWidth(), bottomLevel.GetGridHeight(), start, end);
                    if (positionSetTemp != null)
                    {
                        positionSetTemp.InitToTraverseSet();
                        while (positionSetTemp.NextPosition())
                        {
                            positionSet.AddPosition(positionSetTemp.GetPosition());
                        }
                    }
                    start = end;
                    positionSet.AddPosition(start);
                }
                IPositionSet positionSetTemp2 = writeLineInGridEngine.WriteLineInGrid(bottomLevel.GetGridWidth(), bottomLevel.GetGridHeight(), end, first);
                if (positionSetTemp2 != null)
                {
                    positionSetTemp2.InitToTraverseSet();
                    while (positionSetTemp2.NextPosition())
                    {
                        positionSet.AddPosition(positionSetTemp2.GetPosition());
                    }
                }
            }

            m2mStructure.Preprocessing(positionSet);

            #region code for algorithm demo
            if (GetM2MStructure != null)
            {
                GetM2MStructure(m2mStructure);
            }
            #endregion
        }
Ejemplo n.º 12
0
        public bool Collide(IPositionSet objPositionSet)
        {
            IPosition start        = new Position_Point();
            IPosition end          = new Position_Point();
            IPosition first        = new Position_Point();
            ILevel    currentLevel = new Level();

            for (int i = 1; i < m2mStructure.GetLevelNum(); i++)
            {
                currentLevel = m2mStructure.GetLevel(i);
                objPositionSet.InitToTraverseSet();
                //判断第一点所在的分块是否相交
                if (objPositionSet.NextPosition())
                {
                    start = new Position_Point(currentLevel.ConvertRealValueToRelativeValueX(objPositionSet.GetPosition().GetX()), currentLevel.ConvertRealValueToRelativeValueY(objPositionSet.GetPosition().GetY()));
                    first = start;
                    if (currentLevel.GetPartRefByPoint(start) != null)
                    {
                        if (i < m2mStructure.GetLevelNum() - 1)
                        {
                            goto NextLevel;
                        }
                        else
                        {
                            tempIntersectPositionSet.AddPosition(new Position_Point(currentLevel.ConvertRelativeValueToPartSequenceX(start.GetX()), currentLevel.ConvertRelativeValueToPartSequenceY(start.GetY())));
                        }
                    }
                }


                while (objPositionSet.NextPosition())
                {
                    //判断每条边所在的分块是否相交
                    end = new Position_Point(currentLevel.ConvertRealValueToRelativeValueX(objPositionSet.GetPosition().GetX()), currentLevel.ConvertRealValueToRelativeValueY(objPositionSet.GetPosition().GetY()));
                    IPositionSet positionSetTemp = writeLineInGridEngine.WriteLineInGrid(currentLevel.GetGridWidth(), currentLevel.GetGridHeight(), start, end);

                    if (positionSetTemp != null)
                    {
                        positionSetTemp.InitToTraverseSet();
                        while (positionSetTemp.NextPosition())
                        {
                            if (currentLevel.GetPartRefByPoint(positionSetTemp.GetPosition()) != null)
                            {
                                if (i < m2mStructure.GetLevelNum() - 1)
                                {
                                    goto NextLevel;
                                }
                                else
                                {
                                    tempIntersectPositionSet.AddPosition(new Position_Point(currentLevel.ConvertRelativeValueToPartSequenceX(positionSetTemp.GetPosition().GetX()), currentLevel.ConvertRelativeValueToPartSequenceY(positionSetTemp.GetPosition().GetY())));
                                }
                            }
                        }
                    }

                    //判断每条边的终点所在的分块是否相交
                    start = end;
                    if (currentLevel.GetPartRefByPoint(start) != null)
                    {
                        if (i < m2mStructure.GetLevelNum() - 1)
                        {
                            goto NextLevel;
                        }
                        else
                        {
                            tempIntersectPositionSet.AddPosition(new Position_Point(currentLevel.ConvertRelativeValueToPartSequenceX(start.GetX()), currentLevel.ConvertRelativeValueToPartSequenceY(start.GetY())));
                        }
                    }
                }

                //判断最后一条边所在的分块是否相交
                IPositionSet positionSetTemp2 = writeLineInGridEngine.WriteLineInGrid(currentLevel.GetGridWidth(), currentLevel.GetGridHeight(), end, first);
                if (positionSetTemp2 != null)
                {
                    positionSetTemp2.InitToTraverseSet();
                    while (positionSetTemp2.NextPosition())
                    {
                        if (currentLevel.GetPartRefByPoint(positionSetTemp2.GetPosition()) != null)
                        {
                            if (i < m2mStructure.GetLevelNum() - 1)
                            {
                                goto NextLevel;
                            }
                            else
                            {
                                tempIntersectPositionSet.AddPosition(new Position_Point(currentLevel.ConvertRelativeValueToPartSequenceX(positionSetTemp2.GetPosition().GetX()), currentLevel.ConvertRelativeValueToPartSequenceY(positionSetTemp2.GetPosition().GetY())));
                            }
                        }
                    }
                }

                if (i == m2mStructure.GetLevelNum() - 1 && tempIntersectPositionSet.GetNum() > 0)
                {
                    goto NextLevel;
                }

                #region code for algorithm demo
                if (GetNoCollision != null)
                {
                    GetNoCollision(objPositionSet);
                    Debug.WriteLine(i);
                }
                #endregion

                return(false);//free of collision

                NextLevel :;
            }

            #region code for algorithm demo
            if (GetCollision != null)
            {
                GetCollision(objPositionSet);
            }
            if (GetIntersectPart != null)
            {
                GetIntersectPart(currentLevel, tempIntersectPositionSet);
            }
            #endregion
            tempIntersectPositionSet.Clear();

            return(true);
        }
        //求散点子集的凸包
        private IPositionSet solveHelp(IPosition p1, IPosition p2, IPositionSet ps)
        {
            //求离p1,p2最远的点
            ps.InitToTraverseSet();
            if (!ps.NextPosition())
            {
                return(ps);
            }
            IPosition p3   = ps.GetPosition();
            double    p3_h = getH(p1, p2, p3);
            PositionSetEdit_ImplementByICollectionTemplate rest = new PositionSetEdit_ImplementByICollectionTemplate();

            while (ps.NextPosition())
            {
                IPosition pt   = ps.GetPosition();
                double    pt_h = getH(p1, p2, pt);
                if (pt_h > p3_h)
                {
                    rest.AddPosition(p3);
                    p3   = pt;
                    p3_h = pt_h;
                }
                else
                {
                    rest.AddPosition(pt);
                }
            }

            //把散点分成几部分
            PositionSetEdit_ImplementByICollectionTemplate leftpart  = new PositionSetEdit_ImplementByICollectionTemplate();
            PositionSetEdit_ImplementByICollectionTemplate rightpart = new PositionSetEdit_ImplementByICollectionTemplate();
            PositionSetEdit_ImplementByICollectionTemplate p1set     = new PositionSetEdit_ImplementByICollectionTemplate();
            PositionSetEdit_ImplementByICollectionTemplate p2set     = new PositionSetEdit_ImplementByICollectionTemplate();

            ps = rest;
            IPosition p11        = new Position_Point(2 * p2.GetX() - p1.GetX(), 2 * p2.GetY() - p1.GetY()); //p1关于p2的对称点
            double    leftangle  = getAngle(p1, p2, p3);
            double    rightangle = getAngle(p2, p11, p3);

            ps.InitToTraverseSet();
            while (ps.NextPosition())
            {
                IPosition p = ps.GetPosition();
                if (p.GetX() == p1.GetX() && p.GetY() == p1.GetY())
                {
                    p1set.AddPosition(p);
                }
                else if (p.GetX() == p2.GetX() && p.GetY() == p2.GetY())
                {
                    p2set.AddPosition(p);
                }
                else if (p.GetX() == p3.GetX() && p.GetY() == p3.GetY())
                {
                    leftpart.AddPosition(p);
                }
                else
                {
                    double a = getAngle(p1, p2, p);
                    if (a > leftangle)
                    {
                        leftpart.AddPosition(p);
                    }
                    a = getAngle(p2, p11, p);
                    if (a < rightangle)
                    {
                        rightpart.AddPosition(p);
                    }
                }
            }

            //对子集求解
            IPositionSet subResult1 = solveHelp(p1, p3, leftpart);
            IPositionSet subResult2 = solveHelp(p3, p2, rightpart);

            //合并各部分解
            PositionSetEdit_ImplementByICollectionTemplate result = new PositionSetEdit_ImplementByICollectionTemplate();

            p1set.InitToTraverseSet();
            while (p1set.NextPosition())
            {
                result.AddPosition(p1set.GetPosition());
            }

            subResult1.InitToTraverseSet();
            while (subResult1.NextPosition())
            {
                result.AddPosition(subResult1.GetPosition());
            }

            result.AddPosition(p3);

            subResult2.InitToTraverseSet();
            while (subResult2.NextPosition())
            {
                result.AddPosition(subResult2.GetPosition());
            }

            p2set.InitToTraverseSet();
            while (p2set.NextPosition())
            {
                result.AddPosition(p2set.GetPosition());
            }

            return(result);
        }
Ejemplo n.º 14
0
        public static IPositionSet getBlockLine2(IPosition refPos, float itv0, IPosition startPos, IPosition endPos)
        {
            rX  = refPos.GetX();
            rY  = refPos.GetY();
            itv = itv0;
            float sX = startPos.GetX();
            float sY = startPos.GetY();
            float eX = endPos.GetX();
            float eY = endPos.GetY();

            PositionSetEdit_ImplementByICollectionTemplate result = new PositionSetEdit_ImplementByICollectionTemplate();
            int   xDir = (eX > sX) ? 1 : (eX == sX) ? 0 : -1;
            int   yDir = (eY > sY) ? 1 : (eY == sY) ? 0 : -1;
            float x    = sX;
            float y    = sY;

            //结果只包含一个格
            if (getM(sX) == getM(eX) && getN(sY) == getN(eY))
            {
                x = getM(sX) * itv + itv / 2 + rX;
                y = getN(sY) * itv + itv / 2 + rY;
                result.AddPosition(new Position_Point(x, y));
                return(result);
            }

            //斜率为0或无限大的情况
            else if (xDir == 0 || yDir == 0)
            {
                //eX = getM(eX) * itv + itv / 2 + rX;
                //eY = getN(eY) * itv + itv / 2 + rY;
                //while ((eX - x) * xDir >= 0 && (eY - y) * yDir >= 0)
                //{
                //    x = getM(x) * itv + itv / 2 + rX;
                //    y = getN(y) * itv + itv / 2 + rY;
                //    result.AddPosition(new SimplePosition(x, y));
                //    x += itv * xDir;
                //    y += itv * yDir;
                //}

                while ((eX - x) * xDir >= 0 && (eY - y) * yDir >= 0)
                {
                    result.AddPosition(new Position_Point(getM(x) * itv + itv / 2 + rX, getN(y) * itv + itv / 2 + rY));
                    if (eX % itv == 0 && xDir == 0)
                    {
                        result.AddPosition(new Position_Point(getM(x - itv / 2) * itv + itv / 2 + rX, getN(y) * itv + itv / 2 + rY));
                    }
                    if (eY % itv == 0 && yDir == 0)
                    {
                        result.AddPosition(new Position_Point(getM(x) * itv + itv / 2 + rX, getN(y - itv / 2) * itv + itv / 2 + rY));
                    }
                    x += itv * xDir;
                    y += itv * yDir;
                }
                return(result);
            }

            //斜率不为0且不为无限大的情况
            float k = (eY - sY) / (eX - sX);
            //float nextX, nextY;

            //if (xDir > 0)
            //    nextX = (getM(sX) + 1) * itv + rX;
            //else
            //    nextX = getM(sX) * itv + rX - MIN_ITV;

            //if (yDir > 0)
            //    nextY = (getN(sY) + 1) * itv + rY;
            //else
            //    nextY = getN(sY) * itv + rY - MIN_ITV;

            //while ((eX - x) * xDir >= 0 || (eY - y) * yDir >= 0)
            //{
            //    float px = getM(x) * itv + itv / 2 + rX;
            //    float py = getN(y) * itv + itv / 2 + rY;
            //    result.AddPosition(new SimplePosition(px, py));

            //    float dx = Math.Abs(nextX - x);
            //    float dy = Math.Abs(nextY - y);
            //    double dk = dy / dx;
            //    if (dk > Math.Abs(k))
            //    {
            //        x = nextX;
            //        nextX += itv * xDir;
            //        y = sY + (x - sX) * k;
            //    }
            //    else if (dk < Math.Abs(k))
            //    {
            //        y = nextY;
            //        nextY += itv * yDir;
            //        x = sX + (y - sY) / k;
            //    }
            //    else
            //    {
            //        x = nextX;
            //        y = nextY;
            //        nextX += itv * xDir;
            //        nextY += itv * yDir;
            //    }
            //}

            //xDir>0 yDir>0
            ///找出所有边境交点

            int sxx = getM(sX);
            int exx = getM(eX);
            int syy = getN(sY);
            int eyy = getN(eY);
            List <Position_Point> pointList = new List <Position_Point>();

            if (xDir > 0)
            {
                for (int i = 0; i + sxx < exx + 1; i++)
                {
                    float xx = (i + sxx) * itv + rX;
                    float yy = k * xx + (eY - k * eX);
                    pointList.Add(new Position_Point(xx, yy));
                }
            }
            else
            {
                for (int i = 0; i + exx < sxx + 1; i++)
                {
                    float xx = (i + exx) * itv + rX;
                    float yy = k * xx + (eY - k * eX);
                    pointList.Add(new Position_Point(xx, yy));
                }
            }



            if (yDir > 0)
            {
                for (int i = 0; i + syy < eyy + 1; i++)
                {
                    float yy = (i + syy) * itv + rY;
                    float xx = (yy - (eY - k * eX)) / k;

                    pointList.Add(new Position_Point(xx, yy));
                }
            }
            else
            {
                for (int i = 0; i + eyy < syy + 1; i++)
                {
                    float yy = (i + eyy) * itv + rY;
                    float xx = (yy - (eY - k * eX)) / k;
                    pointList.Add(new Position_Point(xx, yy));
                }
            }

            pointList.Sort(new pointXcomparer());

            for (int i = 1; i < pointList.Count; i++)
            {
                Position_Point p1 = pointList[i - 1];
                Position_Point p2 = pointList[i];
                //把两个交点的中点添加进去
                float px = getM((p1.GetX() + p2.GetX()) / 2) * itv + itv / 2 + rX;
                float py = getN((p1.GetY() + p2.GetY()) / 2) * itv + itv / 2 + rY;
                result.AddPosition(new Position_Point(px, py));

                if (i == pointList.Count - 1)
                {
                    if (eX % itv == 0)
                    {
                        px = getM(eX + (itv / 2)) * itv + itv / 2 + rX;
                        py = getN(eY) * itv + itv / 2 + rY;
                        result.AddPosition(new Position_Point(px, py));
                        px = getM(eX - (itv / 2)) * itv + itv / 2 + rX;
                        result.AddPosition(new Position_Point(px, py));
                    }
                    if (eY % itv == 0)
                    {
                        px = getM(eX) * itv + itv / 2 + rX;
                        py = getN(eY + (itv / 2)) * itv + itv / 2 + rY;
                        result.AddPosition(new Position_Point(px, py));
                        py = getN(eY - (itv / 2)) * itv + itv / 2 + rY;
                        result.AddPosition(new Position_Point(px, py));
                    }
                }
                else if (i == 0)
                {
                    if (sX % itv == 0)
                    {
                        px = getM(sX + (itv / 2)) * itv + itv / 2 + rX;
                        py = getN(sY) * itv + itv / 2 + rY;;
                        result.AddPosition(new Position_Point(px, py));
                        px = getM(sX - (itv / 2)) * itv + itv / 2 + rX;
                        result.AddPosition(new Position_Point(px, py));
                    }
                    if (sY % itv == 0)
                    {
                        px = getM(sX) * itv + itv / 2 + rX;
                        py = getN(sY + (itv / 2)) * itv + itv / 2 + rY;
                        result.AddPosition(new Position_Point(px, py));
                        py = getN(sY - (itv / 2)) * itv + itv / 2 + rY;
                        result.AddPosition(new Position_Point(px, py));
                    }
                }

                if (p1.GetX() == p2.GetX() && p1.GetY() == p2.GetY())
                {
                    px = p1.GetX();
                    py = p2.GetY();
                    result.AddPosition(new Position_Point(px - itv / 2, py + itv / 2));
                    result.AddPosition(new Position_Point(px - itv / 2, py - itv / 2));
                    result.AddPosition(new Position_Point(px + itv / 2, py - itv / 2));
                    result.AddPosition(new Position_Point(px + itv / 2, py + itv / 2));
                }
            }

            return(result);
        }