Exemplo n.º 1
0
        /// <summary>
        /// Replaces a box of the receiver with the contents of another matrix's box.
        /// The source box ranges from <i>[sourceColumn,sourceRow]</i> to <i>[sourceColumn+width-1,sourceRow+height-1]</i>, all inclusive.
        /// The destination box ranges from <i>[column,row]</i> to <i>[column+width-1,row+height-1]</i>, all inclusive.
        /// Does nothing if <i>width &lt;= 0 || height &lt;= 0</i>.
        /// If <i>source==this</i> and the source and destination box intersect in an ambiguous way, then replaces as if using an intermediate auxiliary copy of the receiver.
        /// </summary>
        /// <param name="column">the index of the column-coordinate.</param>
        /// <param name="row">the index of the row-coordinate.</param>
        /// <param name="width">the width of the box.</param>
        /// <param name="height">the height of the box.</param>
        /// <param name="source">the source matrix to copy from(may be identical to the receiver).</param>
        /// <param name="sourceColumn">the index of the source column-coordinate.</param>
        /// <param name="sourceRow">the index of the source row-coordinate.</param>
        /// <exception cref="">if <i>column&lt;0 || column+width&gt;columns() || row&lt;0 || row+height&gt;rows()</i></exception>
        /// <exception cref="">if <i>sourceColumn&lt;0 || sourceColumn+width&gt;source.Columns || sourceRow&lt;0 || sourceRow+height&gt;source.Rows</i></exception>
        public void ReplaceBoxWith(int column, int row, int width, int height, BitMatrix source, int sourceColumn, int sourceRow)
        {
            this.ContainsBox(column, row, width, height);
            source.ContainsBox(sourceColumn, sourceRow, width, height);
            if (width <= 0 || height <= 0)
            {
                return;
            }

            if (source == this)
            {
                Rectangle destRect   = new Rectangle(column, row, width, height);
                Rectangle sourceRect = new Rectangle(sourceColumn, sourceRow, width, height);
                if (destRect.IntersectsWith(sourceRect))
                { // dangerous intersection
                    source = source.Copy();
                }
            }

            BitVector sourceVector  = source.ToBitVector();
            BitVector destVector    = this.ToBitVector();
            int       sourceColumns = source.Columns;

            for (; --height >= 0; row++, sourceRow++)
            {
                int offset       = row * _columns + column;
                int sourceOffset = sourceRow * sourceColumns + sourceColumn;
                destVector.ReplaceFromToWith(offset, offset + width - 1, sourceVector, sourceOffset);
            }
        }