examples/arithmetic.cc

00001 
00011 /*
00012  * @file   arithmetic.cc
00013  * @author Sebastien Fourey <http://www.greyc.ensicaen.fr/~seb>
00014  * @date   Tue Aug 21
00015  * 
00016  * @brief  Example that shows the usage of the Board library by 
00017  *         illustating the arithmetic coding principle.
00018  *         Files arithm.eps, arithm.fig and arithm.svg are created in
00019  *         the current directory.
00020  *
00021  * @copyright 
00022  */
00023 #include "Board.h"
00024 #include <cstdlib>
00025 #include <iostream>
00026 #include <fstream>
00027 #include <cmath>
00028 using namespace std;
00029 using namespace BoardLib;
00030 
00031 int main( int argc, char *argv[] )
00032 {
00033   Board board;
00034   
00035   if ( argc == 2 && !strcmp(argv[1],"-h") ) {
00036     cout << "Usage:   arithmetic [word] \n\n  Where word has letters in {a, b, c, d, e}\n\n";
00037     exit(0);
00038   }
00039 
00040   char the_string[1024];
00041   strcpy( the_string, "cccccccbb" );  
00042   
00043   if ( argc == 2 ) { 
00044     strcpy( the_string, argv[1] );
00045   }
00046   
00047   // char letters[] = "abcde";
00048   double proba[100] = { 0.2, 0.1, 0.6, 0.05, 0.05 };
00049   double lefts[100] = { 0, 0.2, 0.3, 0.9, 0.95  };
00050     
00051   char * pc = the_string;
00052   int h = 0;
00053 
00054   double thickness = 0.5 / strlen( the_string );
00055 
00056   board.drawRectangle( 0, 0, 1.0, 0.5 );
00057   board.setPenColorRGBi( 0, 0, 255 );
00058 
00059   double left = 0;
00060   //double droite = 1.0;
00061   double width = 1.0;
00062   double w = 1.0;
00063   double n = 1;
00064   char str[20];
00065 
00066   board.setLineWidth( 0.5 );
00067   board.clear( Color( 200, 200, 200 ) );
00068   while ( *pc ) {
00069     int i = *pc - 'a';
00070     left += width * lefts[ i ];
00071     width *= proba[i];
00072 
00073     board.setPenColorRGBi( 100, 100, 100 ); 
00074     w /= 2.0;
00075     n *= 2;
00076     for ( int k = 0; k < n; k++ )
00077       board.drawRectangle( k*w, -h * thickness, w, 0.9*thickness );
00078 
00079     switch ( i ) {
00080     case 0:  board.setPenColorRGBi( 0, 0, 255 ); break;
00081     case 1:  board.setPenColorRGBi( 0, 255, 0 ); break;
00082     case 2:  board.setPenColorRGBi( 255, 0, 0 ); break;
00083     case 3:  board.setPenColorRGBi( 0, 255, 255 ); break;
00084     case 4:  board.setPenColorRGBi( 255, 255, 0 ); break;
00085     }
00086     board.drawRectangle( left, -h * thickness, width, 0.9*thickness );
00087 
00088     sprintf( str, "%c", *pc );
00089     board.drawText( (left + width/2.0 ), -(h+0.5) * thickness, str );    
00090     ++h;
00091     ++pc;
00092   }
00093 
00094   // Draw the letters rectangles.
00095   for ( int k = 0; k < 5; k++ ) {
00096     switch ( k ) {
00097     case 0:  board.setPenColorRGBi( 0, 0, 255 ); break;
00098     case 1:  board.setPenColorRGBi( 0, 255, 0 ); break;
00099     case 2:  board.setPenColorRGBi( 255, 0, 0 ); break;
00100     case 3:  board.setPenColorRGBi( 0, 255, 255 ); break;
00101     case 4:  board.setPenColorRGBi( 255, 255, 0 ); break;
00102     }
00103     board.drawRectangle( lefts[k], thickness, proba[k], 0.9*thickness );      
00104     sprintf( str, "%c", 'a' + k  );
00105     board.drawText( lefts[k] + proba[k]/2.0, 0.5*thickness, str );    
00106   }
00107 
00108   board.setPenColorRGBi( 0, 0, 0 );
00109   board.setLineWidth(0.2);
00110   board.save( "arithm.eps" );
00111   board.save( "arithm.fig" );
00112   board.save( "arithm.svg" );
00113   exit(0);
00114 }

Generated on Fri Aug 24 19:07:40 2007 for Board by  doxygen 1.5.1