// Returns true if rectangle could be placed into position. (also calculates perimeter coverage in the position). protected bool ExaminePosition(ArrayList placedRectangles, Position position, ItemRectangle itemRectangle, out int coverage) { // ----- Trying to place exact at specified position ----- itemRectangle.MoveToPosition(position); itemRectangle.Neighbours.Clear(); coverage = 0; // ...intersects with page borders? if (!IsInPageBorders(itemRectangle)) { return(false); } // ...intersects with other rectangles? foreach (ItemRectangle placedRect in placedRectangles) { if (itemRectangle.Intersects(placedRect)) { return(false); } bool haveTouch; coverage += itemRectangle.CalculateCoverage(placedRect, out haveTouch); if (haveTouch) { itemRectangle.Neighbours.Add(placedRect); } } // ...additional coverage values - when contacts page borders if (itemRectangle.X == _pageLeft) { coverage += itemRectangle.Height; } if (itemRectangle.Y == _pageTop) { coverage += itemRectangle.Width; } if (itemRectangle.RightX == _pageRight) { coverage += itemRectangle.Height; } if (itemRectangle.BottomY == _pageBottom) { coverage += itemRectangle.Width; } return(true); }
protected static void PlaceRectangle(int dip, Position dstPosition, ItemRectangle itemRectangle, ArrayList placedRectangles) { itemRectangle.MoveToPosition(dstPosition); // ...Generate all possible positions Position[] hostedPoints = new Position[4]; // Left-top corner Point pnt = itemRectangle.Location; hostedPoints[0] = new Position(pnt); // Left-bottom corner pnt.Y += itemRectangle.Height - 1; hostedPoints[1] = new Position(pnt); // Right-bottom corner pnt.X += itemRectangle.Width - 1; hostedPoints[2] = new Position(pnt); // Right-top corner pnt = itemRectangle.Location; pnt.X += itemRectangle.Width - 1; hostedPoints[3] = new Position(pnt); // ...Closing position that is closed by the pItemRectangle himself hostedPoints[0].Close(dip, true); for (int i = 0; i < 4; i++) { // Closing closed positions in pPositionsArray IEnumerator neighboursEnumerator = itemRectangle.Neighbours.GetEnumerator(); bool toContinue = true; while (neighboursEnumerator.MoveNext() && toContinue) { ItemRectangle neighbour = (ItemRectangle)neighboursEnumerator.Current; for (int j = 0; j < neighbour.HostedPositions.Count; j++) { Position neighbourPosition = (Position)neighbour.HostedPositions[j]; if (hostedPoints[i].EqualCoordinates(neighbourPosition)) { if (hostedPoints[i].Closed(dip) || neighbourPosition.Closed(dip)) { hostedPoints[i].Close(dip, true); neighbourPosition.Close(dip, true); toContinue = false; break; } else { hostedPoints[i] = neighbourPosition; } } } } } // ...Adding new possible positions into pPositions array, adding new placed rect. foreach (Position pos in hostedPoints) { if (!pos.Closed(dip)) { itemRectangle.HostedPositions.Add(pos); } } placedRectangles.Add(itemRectangle); }