/// <summary> /// add new draw method in region below /// </summary> /// <param name="pSet">position set being draw</param> #region draw method public void DrawPositionSet(IPositionSet pSet) { layers.Add(new Layer_PositionSet_Point(new PositionSet_Cloned(pSet))); IfNotHoldingShowFormInShowMode(); }
public BackwardsSubsetSolver(Level originalLevel, Level level, int capacity, bool assessPossibility) : base(level, incremental) { this.originalLevel = originalLevel; this.boxCoordinates = level.BoxCoordinates; this.boxes = boxCoordinates.Length; this.assessPossibility = assessPossibility; this.capacity = capacity; useReliablePositionSet = true; // Create a set of visited positions, // assuming three sokoban squares per position. if (useReliablePositionSet) { // A useReliablePositionSet position set does not need to be validated. visited = ReliablePositionSet.CreateInstance(level, 3 * capacity); } else { // An unreliable position set might miss some achievable // positions and as a result deadlocks calculated using // it need to be validated as truly unsolvable. visited = new UnreliablePositionSet(level, 3 * capacity); } // Create scratch arrays for solving. regions = new Coordinate2D[level.InsideSquares]; solvedArray = new bool[level.InsideSquares]; }
public void TraversalEveryLevelAndBuild(IM2MStructure m2mStructure) { IPart rootPart = m2mStructure.GetLevel(0).GetPartRefByPartIndex(0, 0); IPositionSet bottonLevelPositionSet = m2mStructure.GetBottonLevelPositionSetByAncestorPart(rootPart, 0); bottonLevelPositionSet.InitToTraverseSet(); while (bottonLevelPositionSet.NextPosition()) { ((IPosition_Connected)(bottonLevelPositionSet.GetPosition())).SetAttachment(new Tag_M2M_Position()); } //遍历每一层 for (int levelSequence = m2mStructure.GetLevelNum() - 1; levelSequence >= 0; levelSequence--) { Queue <PositionAndPositon> partAndPositonQueue = new Queue <PositionAndPositon>(); bool isPart = true; ILevel currentLevel = m2mStructure.GetLevel(levelSequence); IPositionSet currrentLevelPositionSet = null; //如果处于最底层 if (levelSequence == m2mStructure.GetLevelNum() - 1) { isPart = false; } if (levelSequence == 0) { IPositionSetEdit temp = new Position_Implement.PositionSetEdit_ImplementByICollectionTemplate(); temp.AddPosition(rootPart); currrentLevelPositionSet = temp; } else { currrentLevelPositionSet = m2mStructure.GetDescendentPositionSetByAncestorPart(levelSequence, rootPart, 0); } currrentLevelPositionSet.InitToTraverseSet(); //遍历每一个分块 while (currrentLevelPositionSet.NextPosition()) { List <IPart_Connected> SubPartList = new List <IPart_Connected>(); IPart_Multi currentMultiPart = (IPart_Multi)(currrentLevelPositionSet.GetPosition()); //计算part的边界 float minSequenceX; float maxSequenceX; float minSequenceY; float maxSequenceY; if (isPart) { float unitNumInLength = (int)(m2mStructure.GetLevel(levelSequence + 1).GetUnitNumInHeight() / currentLevel.GetUnitNumInHeight()); minSequenceX = currentMultiPart.GetX() * unitNumInLength; maxSequenceX = minSequenceX + unitNumInLength; minSequenceY = currentMultiPart.GetY() * unitNumInLength; maxSequenceY = minSequenceY + unitNumInLength; } else { minSequenceX = currentLevel.ConvertPartSequenceXToRealValue(currentMultiPart.GetX()); maxSequenceX = minSequenceX + currentLevel.GetGridWidth(); minSequenceY = currentLevel.ConvertPartSequenceYToRealValue(currentMultiPart.GetY()); maxSequenceY = minSequenceY + currentLevel.GetGridHeight(); } IPositionSet childPositionSetOfCurrentPart = m2mStructure.GetChildPositionSetByParentPart(levelSequence, currentMultiPart); childPositionSetOfCurrentPart.InitToTraverseSet(); //遍历每一个子分块或点 while (childPositionSetOfCurrentPart.NextPosition()) { if (isPart) { IPart_Multi childPartMulti = (IPart_Multi)childPositionSetOfCurrentPart.GetPosition(); foreach (IPosition_Connected currentPosition_Connected in childPartMulti.GetSubPartSet()) { BFSInOnePoint(partAndPositonQueue, isPart, SubPartList, currentMultiPart, minSequenceX, maxSequenceX, minSequenceY, maxSequenceY, currentPosition_Connected); } } else { IPosition_Connected currentPosition_Connected = (IPosition_Connected)childPositionSetOfCurrentPart.GetPosition(); BFSInOnePoint(partAndPositonQueue, isPart, SubPartList, currentMultiPart, minSequenceX, maxSequenceX, minSequenceY, maxSequenceY, currentPosition_Connected); } } //遍历每一个子分块结束 currentMultiPart.SetSubPartSet(SubPartList); } //遍历完某层的每个分块 //处理还没建立链接的点和分块对 foreach (PositionAndPositon pap in partAndPositonQueue) { IPosition_Connected position_Connected = pap.position; IPart_Connected currentParentPart = GetParentPart(position_Connected, isPart); IPart_Connected currentParentPart2 = GetParentPart(pap.positionInExistPart, isPart); float distance = CalculateTheDistanceBetweenTwoPosition(currentParentPart2, currentParentPart); IPositionSet_Connected_AdjacencyEdit positionSet_Connected_Adjacency = currentParentPart2.GetAdjacencyPositionSetEdit(); bool isContain = false; positionSet_Connected_Adjacency.InitToTraverseSet(); while (positionSet_Connected_Adjacency.NextPosition()) { if (positionSet_Connected_Adjacency.GetPosition() == currentParentPart) { isContain = true; break; } } if (isContain) { continue; } else { positionSet_Connected_Adjacency.AddAdjacency(currentParentPart, distance); } positionSet_Connected_Adjacency = currentParentPart.GetAdjacencyPositionSetEdit(); isContain = false; positionSet_Connected_Adjacency.InitToTraverseSet(); while (positionSet_Connected_Adjacency.NextPosition()) { if (positionSet_Connected_Adjacency.GetPosition() == currentParentPart2) { isContain = true; break; } } if (isContain) { continue; } else { positionSet_Connected_Adjacency.AddAdjacency(currentParentPart2, distance); } } #region code for algorithm demo if (GetPartSetInSpecificLevel != null) { GetPartSetInSpecificLevel(m2mStructure.GetLevel(levelSequence), levelSequence, m2mStructure.GetDescendentPositionSetByAncestorPart(levelSequence, rootPart, 0)); } #endregion } //遍历每一层结束 }
//添加一个点序列 public void addCurve(string name, IPositionSet ps) { nameList.Add(name); psList.Add(ps); }
public override void Draw(Graphics graphics) { if (Visible) { SetDrawerGraphic(graphics); List <IPosition_Connected> PartMiddlePointList = new List <IPosition_Connected>(); List <PointF[]> PointArrayList = new List <PointF[]>(); ILevel level = m2mStructure.GetLevel(levelSequence); IPart rootPart = m2mStructure.GetLevel(0).GetPartRefByPartIndex(0, 0); IPositionSet positionSet = m2mStructure.GetDescendentPositionSetByAncestorPart(levelSequence, rootPart, 0); Dictionary <IPosition, IPosition_Connected_Edit> partToPositionDictionary = new Dictionary <IPosition, IPosition_Connected_Edit>(); positionSet.InitToTraverseSet(); while (positionSet.NextPosition()) { Position_Connected_Edit PartDeputyPosition_Connected = new Position_Connected_Edit(); partToPositionDictionary.Add(positionSet.GetPosition(), PartDeputyPosition_Connected); } positionSet.InitToTraverseSet(); while (positionSet.NextPosition()) { IPart_Connected currentPart = (IPart_Connected)positionSet.GetPosition(); IPositionSet bottomLevelPositionSet = m2mStructure.GetBottonLevelPositionSetByAncestorPart(currentPart, levelSequence); IPosition tempPoint = PositionSetAttribute.GetGravityCenter(bottomLevelPositionSet); IPosition_Connected_Edit PartDeputyPosition_Connected; partToPositionDictionary.TryGetValue(positionSet.GetPosition(), out PartDeputyPosition_Connected); PartDeputyPosition_Connected.SetX(tempPoint.GetX()); PartDeputyPosition_Connected.SetY(tempPoint.GetY()); currentPart.GetAdjacencyPositionSet().InitToTraverseSet(); while (currentPart.GetAdjacencyPositionSet().NextPosition()) { IPosition_Connected_Edit adjDeputy; partToPositionDictionary.TryGetValue(currentPart.GetAdjacencyPositionSet().GetPosition(), out adjDeputy); PartDeputyPosition_Connected.GetAdjacencyPositionSetEdit().AddAdjacency( adjDeputy, currentPart.GetAdjacencyPositionSet().GetDistanceToAdjacency()); } PartMiddlePointList.Add(PartDeputyPosition_Connected); IPositionSet ConvexHullPointSet = null; if (bottomLevelPositionSet.GetNum() > 1) { ConvexHullPointSet = new QuickHull().ConvexHull(bottomLevelPositionSet); } else { ConvexHullPointSet = bottomLevelPositionSet; } PointF[] tempArray = new PointF[ConvexHullPointSet.GetNum()]; int sequence = 0; ConvexHullPointSet.InitToTraverseSet(); while (ConvexHullPointSet.NextPosition()) { tempArray[sequence].X = ConvertRealXToScreenX(ConvexHullPointSet.GetPosition().GetX()); tempArray[sequence].Y = ConvertRealYToScreenY(ConvexHullPointSet.GetPosition().GetY()); sequence++; } PointArrayList.Add(tempArray); } Pen pen = new Pen(partColor, partBorderWidth); Brush brush = new SolidBrush(Color.FromArgb(alpha, partColor)); foreach (PointF[] pointArray in PointArrayList) { if (pointArray.Length >= 2) { graphics.DrawPolygon(pen, pointArray); graphics.FillPolygon(brush, pointArray); } } this.positionSet = new PositionSet_ConnectedEdit(PartMiddlePointList); positionSetDrawerPump.ResetPump(); positionSetDrawerPump.DrawCoordinate(pointCoordinateDrawer); positionSetDrawerPump.DrawPoint(pointDrawer); positionSetDrawerPump.DrawConnection(connectionDrawer); positionSetDrawerPump.Run(); } }
private void BFSInOnePoint(Queue <PositionAndPositon> PositionAndPositonQueue, bool isPart, List <IPart_Connected> SubPartList, IPart_Multi currentMultiPart, float minSequenceX, float maxSequenceX, float minSequenceY, float maxSequenceY, IPosition_Connected currentPosition_Connected) { //如果没有被搜索 if (GetParentPart(currentPosition_Connected, isPart) == null) { IPart_Connected currentPart_Connected = currentMultiPart.CreateSubPart(); currentPart_Connected.SetAttachment(new Tag_M2M_Part()); currentPart_Connected.SetX(currentMultiPart.GetX()); currentPart_Connected.SetY(currentMultiPart.GetY()); bool isNeedToAddToSubPartList;// = true; SubPartList.Add(currentPart_Connected); currentPart_Connected.AddToSubPositionList(currentPosition_Connected); SetParentPart(currentPosition_Connected, currentPart_Connected, isPart); //以当前点为起始点做墨滴搜索 Queue <IPosition_Connected> OpenTable = new Queue <IPosition_Connected>(); OpenTable.Enqueue(currentPosition_Connected); //如果Open表还有元素则继续取出 while (OpenTable.Count > 0) { IPosition_Connected currentCenterPosition_Connected = OpenTable.Dequeue(); IPositionSet_Connected_Adjacency currentPositionAdjacencyPositionSet = currentCenterPosition_Connected.GetAdjacencyPositionSet(); currentPositionAdjacencyPositionSet.InitToTraverseSet(); while (currentPositionAdjacencyPositionSet.NextPosition()) { IPosition_Connected adjPosition_Connected = currentPositionAdjacencyPositionSet.GetPosition_Connected(); float pX = adjPosition_Connected.GetX(); float pY = adjPosition_Connected.GetY(); //如果分块在当前分块之内 if (pX >= minSequenceX && pX < maxSequenceX && pY >= minSequenceY && pY < maxSequenceY) { IPart_Connected parentPart = GetParentPart(adjPosition_Connected, isPart); //如果该分块还没被搜索 if (parentPart == null) { currentPart_Connected.AddToSubPositionList(adjPosition_Connected); SetParentPart(adjPosition_Connected, currentPart_Connected, isPart); //放进Open表 OpenTable.Enqueue(adjPosition_Connected); } //如果父分块不为null,则表明该分块之前已经被搜索过,刚搜索到的分块与原分块连通 else if (parentPart != currentPart_Connected) { isNeedToAddToSubPartList = false; //把原来的父分块设为新的父分块 IPositionSet tempPositionSet = parentPart.GetTrueChildPositionSet(); tempPositionSet.InitToTraverseSet(); while (tempPositionSet.NextPosition()) { IPosition_Connected temp = (IPosition_Connected)tempPositionSet.GetPosition(); SetParentPart(temp, currentPart_Connected, isPart); currentPart_Connected.AddToSubPositionList(temp); } SubPartList.Remove(parentPart); } } //如果不在当前分块里面 else { //如果在当前分块外的点还没被搜索(即还没被制定其父分块)则把其放进partAndPositonQueue IPart_Connected currentParentPart = GetParentPart(adjPosition_Connected, isPart); PositionAndPositonQueue.Enqueue(new PositionAndPositon(currentCenterPosition_Connected, adjPosition_Connected)); //if (currentParentPart == null) //{ // PositionAndPositonQueue.Enqueue(new PositionAndPositon(currentCenterPosition_Connected, adjPosition_Connected)); //} ////如果在当前分块外的点已经分配父分块,则建立两父分块间的链接关系。 //else //{ // currentPart_Connected.GetAdjacencyPositionSetEdit().AddAdjacency(currentParentPart, CalculateTheDistanceBetweenTwoPosition(currentPart_Connected, currentParentPart)); //} } } } } }
public ConvexHullCompare(IPositionSet ps, IPositionSet cps, IPositionSet cps_ref) { this.ps = ps; this.cps = cps; this.cps_ref = cps_ref; }
/// <summary> /// 遍历positionSet并产生相应的事件(如果有新的时间请在里面添加,并在ResetPainter函数里面设为null) /// </summary> /// <param name="ps"></param> public void TraversePositionSetAndSpringEvent(IPositionSet ps) { ps.InitToTraverseSet(); IPosition[] positionSet_ary = (IPosition[])ps.ToArray(); for (int i = 0; i < positionSet_ary.Length; i++) { if (OnGetRealCoordinate != null) { this.OnGetRealCoordinate(layer.ConvertPositionSetXToRealX(positionSet_ary[i].GetX()), layer.ConvertPositionSetYToRealY(positionSet_ary[i].GetY())); } //DrawPointX和DrawPointY是屏幕上显示的坐标 float DrawPointX = layer.ConvertPositionSetXToScreenX(positionSet_ary[i].GetX()); float DrawPointY = layer.ConvertPositionSetYToScreenY(positionSet_ary[i].GetY()); if (i == 0) { if (OnGetFirstPoint != null) { this.OnGetFirstPoint(DrawPointX, DrawPointY); } } else if (i == positionSet_ary.Length - 1) { if (OnGetLastPoint != null) { this.OnGetLastPoint(DrawPointX, DrawPointY); } } else { if (OnGetMiddlePoint != null) { this.OnGetMiddlePoint(DrawPointX, DrawPointY); } } if (OnGetPoint != null) { this.OnGetPoint(DrawPointX, DrawPointY); } } #region old implement //if (OnGetLastPoint != null) //{ // this.OnGetLastPoint(x, y); //} //if (ps.NextPosition()) //{ // if (OnGetRealPoint != null) // { // this.OnGetRealPoint(ps.GetPosition().GetX(), ps.GetPosition().GetY()) // } // x = ps.GetPosition().GetX() * scaleX + translationX; // y = -(ps.GetPosition().GetY() * scaleY + translationY); // if (OnGetFirstPoint != null) // { // this.OnGetFirstPoint(x, y); // } // if (OnGetPoint != null) // { // this.OnGetPoint(x, y); // } //} //while (ps.NextPosition()) //{ // if (OnGetRealPoint != null) // { // this.OnGetRealPoint(ps.GetPosition().GetX(), ps.GetPosition().GetY()) // } // x = ps.GetPosition().GetX() * scaleX + translationX; // y = ps.GetPosition().GetY() * scaleY + translationY; // if (OnGetMiddlePoint != null) // { // if(ps.NextPosition()) // { // this.OnGetMiddlePoint(x, y); // } // } // if (OnGetPoint != null) // { // this.OnGetPoint(x, y); // } //} //if (OnGetLastPoint != null) //{ // this.OnGetLastPoint(x, y); //} #endregion }
private void button1_Click(object sender, EventArgs e) { IConvexHullEngine convexhull; int n; switch (comboBox1.SelectedIndex) { case 0: convexhull = new GrahamScan(); break; case 1: //convexhull = new QuickHull(); convexhull = new M2M_CH(); break; default: return; } try { n = Int32.Parse(textBox1.Text); } catch (Exception) { return; } //Point[] ps = new Point[n]; PositionSetEdit_ImplementByICollectionTemplate ps = new PositionSetEdit_ImplementByICollectionTemplate(); Random r = new Random(DateTime.Now.Millisecond); //if (true) { int minx = (int)(pb.Width * 0.05); int miny = (int)(pb.Height * 0.05); for (int i = 0; i < n; i++) { int x = r.Next(minx, pb.Width - minx); int y = r.Next(miny, pb.Height - miny); //ps[i] = new Point(x, y); ps.AddPosition(new Position_Point(x, y)); System.Console.Write(x); System.Console.Write(","); System.Console.Write(y); System.Console.WriteLine(","); } System.Console.WriteLine(); } //else //{ // ps = testData(); //} IPositionSet convexPoints = convexhull.ConvexHull(ps); pb.Image = new Bitmap(pb.Width, pb.Height); Graphics g = Graphics.FromImage(pb.Image); convexPoints.InitToTraverseSet(); g.Clear(Color.White); /* * for (int i = 0; i < ps.Length; i++) * { * g.FillEllipse(Brushes.Black, ps[i].X - 2, pb.Height - ps[i].Y - 2, 4, 4); * g.DrawString("(" + ps[i].X + "," + ps[i].Y + ")", Font, Brushes.Blue, ps[i].X, pb.Height - ps[i].Y); * } * for (int i = 0; i < convexPoints.Length; i++) * { * if (i != convexPoints.Length - 1) * g.DrawLine(new Pen(Color.Green), new PointF(convexPoints[i].X, pb.Height - convexPoints[i].Y), new PointF(convexPoints[i + 1].X, pb.Height - convexPoints[i + 1].Y)); * else * { * g.DrawLine(new Pen(Color.Green), new PointF(convexPoints[i].X, pb.Height - convexPoints[i].Y), new PointF(convexPoints[0].X, pb.Height - convexPoints[0].Y)); * } * } * */ ps.InitToTraverseSet(); IPosition p = ps.GetPosition(); while (p != null) { g.FillEllipse(Brushes.Black, p.GetX() - 2, pb.Height - p.GetY() - 2, 4, 4); g.DrawString("(" + p.GetX() + "," + p.GetY() + ")", Font, Brushes.Blue, p.GetX(), pb.Height - p.GetY()); p = ps.GetPosition(); } IPosition p1 = convexPoints.GetPosition(); IPosition p2 = convexPoints.GetPosition(); while (p2 != null) { g.DrawLine(new Pen(Color.Green), new PointF(p1.GetX(), pb.Height - p1.GetY()), new PointF(p2.GetX(), pb.Height - p2.GetY())); p1 = p2; p2 = convexPoints.GetPosition(); } convexPoints.InitToTraverseSet(); p2 = convexPoints.GetPosition(); g.DrawLine(new Pen(Color.Green), new PointF(p1.GetX(), pb.Height - p1.GetY()), new PointF(p2.GetX(), pb.Height - p2.GetY())); }
//private bool ISThePartInsideSearchBound(IPart currentCheckedPart) //{ // return ((currentCheckedPart.GetX()) <= rightBound) && ((currentCheckedPart.GetX()) >= leftBound) // && ((currentCheckedPart.GetY()) <= upperBoundInGrid) && ((currentCheckedPart.GetX()) >= lowerBound); //} private void SearchNearestPointInOnePart(IPosition targetPoint, int targetPartLevelSequence, IPart currentCheckedPart) { bool NearestPointIsChange = false; bool SearchBoundIsChange = false; ILevel targetPartLevel = m2mStructure.GetLevel(targetPartLevelSequence); #region code for algorithm demo if (SearchOnePartBeginAndGetSearchPartSequence != null) { SearchOnePartBeginAndGetSearchPartSequence(SearchPartSequence); } if (GetSearchPart != null) { GetSearchPart(targetPartLevel, targetPartLevelSequence, currentCheckedPart); } #endregion //travelThePointInPart.InitToGetTheNearestPointInPart(currentCheckedPart, targetPartLevelSequence, m2mStructure, targetPoint); //IPosition CurrentPartNearestPoint = travelThePointInPart.GetTheNearestPointInPart(); //float CurrentPartNearestDistanceSquare = travelThePointInPart.GetNearestDistanceSquare(); IPositionSet BottonLevelPositionSet = m2mStructure.GetBottonLevelPositionSetByAncestorPart(currentCheckedPart, targetPartLevelSequence); BottonLevelPositionSet.InitToTraverseSet(); while (BottonLevelPositionSet.NextPosition()) { IPosition comparePoint = BottonLevelPositionSet.GetPosition(); if (GetOnePointAndShrinkSearchBoundAndDetermineIfShinkSearchBoundInGird != null) { if (GetOnePointAndShrinkSearchBoundAndDetermineIfShinkSearchBoundInGird(targetPoint, comparePoint) == true) { SearchBoundIsChange = true; } } if (GetOnePointAndDetermineNearestPointIsChanged != null) { if (GetOnePointAndDetermineNearestPointIsChanged(targetPoint, comparePoint) == true) { NearestPointIsChange = true; } } } if (GetOnePointAndShrinkSearchBoundAndDetermineIfShinkSearchBoundInGird != null) { if (SearchBoundIsChange) { UpdateSearchBoundInGrid(targetPartLevel); } } if (GetOnePointAndDetermineNearestPointIsChanged != null) { if (NearestPointIsChange) { ShrinkSearchBound(targetPoint); UpdateSearchBoundInGrid(targetPartLevel); } } #region code for algorithm demo SearchPartSequence++; #endregion }
public override void AddPositionSet(IPositionSet positionSet) { throw new Exception("PositionSetEditSet Can not Add IPositionSet"); }