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   Color colors[5] = { Color( 0, 0, 255 ),
00051                       Color( 0, 255, 0 ),
00052                       Color( 255, 0, 0 ),
00053                       Color( 0, 255, 255 ),
00054                       Color( 255, 255, 0 ) };
00055     
00056   char *pc = the_string;
00057   int h = 0;
00058   double thickness = 0.5 / strlen( the_string );
00059 
00060   board.drawRectangle( 0, 0, 1.0, 0.5 );
00061 
00062   double left = 0;
00063   double width = 1.0;
00064   double w = 1.0;
00065   double n = 1;
00066   char str[20];
00067 
00068   board.setLineWidth( 0.5 );
00069   board.clear( Color( 200, 200, 200 ) );
00070   while ( *pc ) {
00071     int i = *pc - 'a';
00072     left += width * lefts[ i ];
00073     width *= proba[i];
00074 
00075     board.setPenColorRGBi( 100, 100, 100 ); 
00076     w /= 2.0;
00077     n *= 2;
00078     for ( int k = 0; k < n; k++ )
00079       board.drawRectangle( k*w, -h * thickness, w, 0.9*thickness );
00080 
00081     board.setPenColor( colors[ i ] );
00082     board.drawRectangle( left, -h * thickness, width, 0.9*thickness );
00083 
00084     sprintf( str, "%c", *pc );
00085     board.drawText( (left + width/2.0 ), -(h+0.5) * thickness, str );    
00086     ++h;
00087     ++pc;
00088   }
00089 
00090   // Draw the letters rectangles.
00091   for ( int k = 0; k < 5; k++ ) {
00092     board.setPenColor( colors[ k ] );
00093     board.drawRectangle( lefts[k], thickness, proba[k], 0.9*thickness );      
00094     sprintf( str, "%c", 'a' + k  );
00095     board.drawText( lefts[k] + proba[k]/2.0, 0.5*thickness, str );    
00096   }
00097 
00098   board.save( "arithm.eps" );
00099   board.save( "arithm.fig" );
00100   board.save( "arithm.svg" );
00101   exit(0);
00102 }

Generated on Mon Aug 27 09:40:08 2007 for Board by  doxygen 1.5.1