00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef _WORD_DRAW_H_
00014 #define _WORD_DRAW_H_
00015
00016 #include "AImage.h"
00017 #include "Word.h"
00018
00019 class WordDraw
00020 {
00021 public:
00022 WordDraw( int n, const Word& w, bool draw_grid = true ):
00023 ss( 6 ),
00024
00025 N(n),
00026 betBraids(0),
00027 theLength( 0 )
00028 {
00029 theImage = new CImage( w.length()*(6+1) + 1 , n*(6+1) + 1 );
00030
00031 for (int i=0;i<theImage->getWidth();i++)
00032 for (int j=0;j<theImage->getHeight();j++){
00033 theImage->setRedPixel(i,j,255);
00034 theImage->setBluePixel(i,j,255);
00035 theImage->setGreenPixel(i,j,255);
00036 }
00037
00038 drawCompressedBraid( w );
00039
00040 if (draw_grid){
00041 for (int i=0;i<=(theLength+1)*(ss+1);i+=(ss+1))
00042 drawVerticalGrid( i, (i/(ss+1) % 10 ? 220 :0) );
00043 for (int i=0;i<theImage->getHeight();i+=(ss+1))
00044 drawHorizontalGrid( i );
00045 }
00046
00047 }
00048
00049 WordDraw( int n, const list<Word>& w, bool draw_grid = true ):
00050 ss( 6 ),
00051
00052 N(n),
00053 betBraids(20),
00054 theLength( 0 )
00055 {
00056 int max_length = 0;
00057 for (list<Word>::const_iterator I=w.begin();I!=w.end();I++)
00058 if (max_length < I->length())
00059 max_length = I->length();
00060
00061 int one_braid_width = n*(6+1)+1;
00062 theImage = new CImage( max_length*(6+1) + 1 , w.size()*one_braid_width + 1 + (w.size()-1)*betBraids );
00063
00064 for (int i=0;i<theImage->getWidth();i++)
00065 for (int j=0;j<theImage->getHeight();j++){
00066 theImage->setRedPixel(i,j,255);
00067 theImage->setBluePixel(i,j,255);
00068 theImage->setGreenPixel(i,j,255);
00069 }
00070
00071 int i_offset=0;
00072 for (list<Word>::const_iterator I=w.begin();I!=w.end();I++,i_offset+=one_braid_width+betBraids)
00073 drawCompressedBraid( *I, i_offset );
00074
00075 if (draw_grid){
00076 for (int i=0;i<=(theLength+1)*(ss+1);i+=(ss+1))
00077 drawVerticalGrid( i , ( i/(ss+1) % 10 ? 220 : 0 ));
00078
00079 i_offset=one_braid_width;
00080 for (int i=0;i<theImage->getHeight();i+=(ss+1)){
00081 if ( i >= i_offset){
00082 i+=betBraids-ss;
00083 i_offset+=one_braid_width+betBraids;
00084 }
00085 drawHorizontalGrid( i );
00086 }
00087 }
00088
00089 }
00090 ~WordDraw() { delete theImage; }
00091 void saveTo( const string& f_name ) {theImage->saveTo(f_name);}
00092 private:
00093
00094
00095
00096
00097
00098
00099
00100
00101 void drawCompressedBraid( const Word& theWord, int vert_offset = 0){
00102 vector<int> positions(N+2,-1);
00103
00104 int i=0;
00105 for (ConstWordIterator wI=theWord.begin();wI!=theWord.end();wI++,i++){
00106 Generator g = *wI;
00107 int absg = abs(g);
00108 int pos = max(positions[absg],max(positions[absg-1],positions[absg+1])) + 1;
00109 drawGenerator( g,pos,vert_offset );
00110 positions[absg] = pos;
00111 if (theLength < pos) theLength = pos;
00112 }
00113
00114 }
00115
00116 void drawGenerator( Generator g, int pos, int vert_offset ){
00117 for (int iss=0;iss<ss;iss++)
00118 for (int jss=0;jss<ss;jss++)
00119 if ( g > 0 ){
00120 theImage->setRedPixel( pos*ss + iss + pos+1, vert_offset+ g + (g - 1)*ss + jss,168);
00121 theImage->setBluePixel( pos*ss + iss + pos+1, vert_offset+ g + (g - 1)*ss + jss,168);
00122 theImage->setGreenPixel( pos*ss + iss + pos+1, vert_offset+g + (g - 1)*ss + jss,168);
00123 } else {
00124 int ag = abs(g);
00125 theImage->setRedPixel( pos*ss +iss + pos+1, vert_offset+ ag + (ag-1)*ss + jss,0);
00126 theImage->setBluePixel( pos*ss +iss + pos+1, vert_offset+ ag + (ag-1)*ss + jss,0);
00127 theImage->setGreenPixel( pos*ss +iss + pos+1,vert_offset+ ag + (ag-1)*ss + jss,0);
00128 }
00129 }
00130
00131 void drawVerticalGrid( int vpos, int color){
00132
00133 for (int i=0;i<theImage->getHeight();i+=2){
00134 theImage->setRedPixel( vpos, i,color);
00135 theImage->setBluePixel( vpos, i, color );
00136 theImage->setGreenPixel( vpos, i, color );
00137 }
00138 }
00139
00140 void drawHorizontalGrid( int hpos){
00141 for (int i=0;i<=(theLength+1)*(ss+1);i+=2){
00142 theImage->setRedPixel( i, hpos,220);
00143 theImage->setBluePixel( i, hpos, 220 );
00144 theImage->setGreenPixel( i, hpos, 220 );
00145 }
00146 }
00147
00148
00149 int N;
00150 CImage* theImage;
00151 int ss;
00152 int theLength;
00153 int betBraids;
00154 };
00155
00156 #endif