void Matrix<T>::submatrix(int sr, int sc, Matrix<T> &a) { int rwz,coz,i,j; if ( this->rows() % a.rows() != 0 || this->cols() % a.cols() != 0 || this->rows() < a.rows() || this->cols() < a.cols() ) { #ifdef USE_EXCEPTION throw WrongSize2D(this->rows(),this->cols(),a.rows(),a.cols()) ; #else Error error("Matrix<T>::submatrix"); error << "Matrix and submatrix incommensurate" ; error.fatal() ; #endif } if ( sr >= this->rows()/a.rows() || sr < 0 || sc >= this->cols()/a.cols() || sc < 0 ) { #ifdef USE_EXCEPTION throw OutOfBound2D(sr,sc,0,this->rows()/a.rows()-1,0,this->cols()/a.cols()-1) ; #else Error error("Matrix<T>::submatrix"); error << "Submatrix location out of bounds.\nrowblock " << sr << ", " << rows()/a.rows() << " colblock " << sc << ", " << a.cols() << endl ; error.fatal() ; #endif } rwz = sr*a.rows(); coz = sc*a.cols(); #ifdef COLUMN_ORDER for ( i = a.rows()-1; i >= 0; --i ) for(j=a.cols()-1;j>=0;--j) this->elem(i+rwz,j+coz) = a(i,j) ; #else T *ptr, *aptr ; aptr = a.m - 1; for ( i = a.rows()-1; i >= 0; --i ) { ptr = &m[(i+rwz)*cols()+coz]-1 ; for ( j = a.cols(); j > 0; --j) *(++ptr) = *(++aptr) ; } #endif }
T Basic2DArray<T>::elem(const int i,const int j) const { if ( i < 0 || rows() <= i || j < 0 || cols() <= j ) { #ifdef USE_EXCEPTION throw OutOfBound2D(i,j,0,rows()-1,0,cols()-1) ; #else Error error("T Basic2DArray<T>::elem(int,int) const") ; if (i < 0 || rows() <= i){ error << "bad first index " << i << endl ; } if (j < 0 || cols() <= j){ error << "bad second index " << j << endl ; } error.fatal() ; #endif } #ifdef COLUMN_ORDER return vm[j][i] ; #else return vm[i][j] ; #endif }
void MatrixImage<T>::drawLine(int i1, int j1, int i2, int j2, T color) { int i,j ; double mx,b ; if(i1<0 || j1<0 || i1>rows() || j1>=cols() ) { #ifdef USE_EXCEPTION throw OutOfBound2D(i1,j1,0,rows()-1,0,cols()-1) ; #else Error error("MatrixImage<T>::drawLine") ; error << "Error in drawing line\n Invalid index ("<< i1 << ", " << j1 << ") to ( " << i2 << ", " << j2 << ") \n" ; error.warning() ; #endif return ; } if(i2 <0 || j2<0 || i2>rows() || j2>=cols() ) { #ifdef USE_EXCEPTION throw OutOfBound2D(i2,j2,0,rows()-1,0,cols()-1) ; #else Error error("MatrixImage<T>::drawLine") ; error << "Error in drawing line\n Invalid index ("<< i1 << ", " << j1 << ") to ( " << i2 << ", " << j2 << ") \n" ; error.warning() ; #endif return ; } // check if line is vertical if(j1==j2) { for(i=minimum(i1,i2); i<=maximum(i1,i2); i++) operator()(i,j1) = color ; return ; } mx = (double)(i1-i2)/(double)(j1-j2) ; b = (double)i1 - mx*j1 ; if(absolute(i1-i2)>absolute(j1-j2)) { // draw vertically if(i1>i2) { for(i=i1; i>=i2; i--) { j = int(((double)i-b)/mx) ; operator()(i,j) = color ; } } else { for(i=i1; i<=i2; i++) { j = (int)((i-b)/mx) ; operator()(i,j) = color ; } } } else { if(j1>j2) { for(j=j1; j>=j2; j--) { i = (int)(mx*j+b) ; operator()(i,j) = color ; } } else { for(j=j1; j<=j2; j++) { i = (int)(mx*j+b) ; operator()(i,j) = color ; } } } }