private Rect CreateCandidateRect(Point2D <int> start, int deltaX, int deltaY, Size requiredSize) { Double42 x = deltaX > 0 ? _verticalPartitions[start.X] : (Double42)(_verticalPartitions[start.X + 1] - requiredSize.Width); Double42 y = deltaY > 0 ? _horizontalPartitions[start.Y] : (Double42)(_horizontalPartitions[start.Y + 1] - requiredSize.Height); return(new Rect(new Point(x, y), requiredSize)); }
// Returns true if the candidate is available, and expansion // will be set to a non-null value if the candidate causes // the subspace bounds to expand. Returns false if the candidate // overlaps other rectangles. public bool EvaluateCandidate(Rect candidateRect, out Double42 expansion) { expansion = 0.0; Int32Rect subspaceBounds = GetSubspaceRect(candidateRect); for (int iY = 0; iY < subspaceBounds.Height; iY++) { int y = iY + subspaceBounds.Y; for (int iX = 0; iX < subspaceBounds.Width; iX++) { int x = iX + subspaceBounds.X; if (_subspaces[x, y]) { // There is already a rect in this subspace location. // We can fail immediately. return(false); } } } Rect oldBounds = Bounds; if (!oldBounds.Contains(candidateRect)) { Rect newBounds = oldBounds; newBounds.Union(candidateRect); expansion = (newBounds.Width * newBounds.Height) - (oldBounds.Width * oldBounds.Height); } return(true); }
private void InsertVerticalPartition(Double42 partition) { if (_verticalPartitions.Add(partition)) { int index; _verticalPartitions.TryFind(partition, out index); _subspaces.InsertColumn(index - 1); } }
private void InsertHorizontalPartition(Double42 partition) { if (_horizontalPartitions.Add(partition)) { int index; _horizontalPartitions.TryFind(partition, out index); _subspaces.InsertRow(index - 1); } }
// Find the last subspace index that spans space that contains the value. // A key assumption is that this is only used for mapping candidate rects // onto the subspace grid, and candidate rects always intersect the // subspace grid. Thus, if the value is greater than the partitions, we // assume that we should still end with the last index. private static int FindLastSubspaceIndex(NumberSet <Double42> partitions, Double42 value, int iSubspaceStart) { int last = partitions.Count - 1; for (int iPartition = iSubspaceStart + 1; iPartition < partitions.Count; iPartition++) { if (partitions[iPartition] >= value) { last = iPartition - 1; break; } } return(last); }
// Find the first subspace index that spans space that contains the value. // A key assumption is that this is only used for mapping candidate rects // onto the subspace grid, and candidate rects always intersect the // subspace grid. Thus, if the value is less than the partitions, we // assume that we should still start with index 0. private static int FindFirstSubspaceIndex(NumberSet <Double42> partitions, Double42 value) { int first = 0; for (int iPartition = 1; iPartition < partitions.Count; iPartition++) { if (partitions[iPartition] > value) { first = iPartition - 1; break; } } return(first); }