NoPaste

C-Code für Random Problem

von MSfree

SNIPPET_TEXT:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include "jpeglib.h"
  6. #include <setjmp.h>
  7.  
  8. /* simple jpeg writer */
  9. static void write_JPEG_file( char * filename, int quality, JSAMPLE * image_buffer,
  10.                              int image_height, int image_width, int samples_per_pixel )
  11. {
  12.   struct jpeg_compress_struct cinfo;
  13.   struct jpeg_error_mgr jerr;
  14.   FILE * outfile;
  15.   JSAMPROW row_pointer[1];
  16.   int row_stride;
  17.  
  18.   cinfo.err = jpeg_std_error(&jerr);
  19.   jpeg_create_compress(&cinfo);
  20.   if((outfile = fopen(filename, "wb")) == NULL) {
  21.     fprintf(stderr, "can't open %s\n", filename);
  22.     exit(1);
  23.   }
  24.   jpeg_stdio_dest(&cinfo, outfile);
  25.   cinfo.image_width = image_width;
  26.   cinfo.image_height = image_height;
  27.   cinfo.input_components = samples_per_pixel;
  28.   cinfo.in_color_space = ( samples_per_pixel == 1 ) ? ( JCS_GRAYSCALE ) : ( JCS_RGB );
  29.   jpeg_set_defaults(&cinfo);
  30.   jpeg_set_quality(&cinfo, quality, TRUE );
  31.   jpeg_start_compress(&cinfo, TRUE);
  32.   row_stride = image_width * samples_per_pixel;
  33.  
  34.   while(cinfo.next_scanline < cinfo.image_height) {
  35.     row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
  36.     (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
  37.   }
  38.   jpeg_finish_compress(&cinfo);
  39.   fclose(outfile);
  40.   jpeg_destroy_compress(&cinfo);
  41. }
  42.  
  43. /* random function */
  44. double MyRandom( double Bereich )
  45. {
  46.   double rNum = ( (double) rand() / (double) RAND_MAX ) * Bereich;
  47.   return rNum;
  48. }
  49.  
  50. /********************/
  51. void GeneratePoints( int numPts, double *x, double *y )
  52. {
  53.   int i;
  54.  
  55.   for( i=0 ; i<numPts ; i++)
  56.   {
  57.     double winkel= MyRandom( 360.0 );
  58.     double radius= MyRandom( 200.0 );
  59.     x[i] = sin( winkel * (M_PI / 180.0) ) * radius;
  60.     y[i] = cos( winkel * (M_PI / 180.0) ) * radius;
  61.   }
  62. }
  63.  
  64. /********************/
  65. void DrawDots( int numPts, double *x, double *y,
  66.                unsigned char *buffer, int bufWidth, int bufHeight )
  67. {
  68.   double minX, minY, maxX, maxY, scaleX, scaleY, scale;
  69.   int i;
  70.  
  71.   minX = x[0];
  72.   minY = y[0];
  73.   maxX = x[0];
  74.   maxY = y[0];
  75.  
  76.   for( i=1 ; i<numPts ; i++)
  77.   {
  78.     if( minX > x[i] ) minX = x[i];
  79.     if( minY > y[i] ) minY = y[i];
  80.     if( maxX < x[i] ) maxX = x[i];
  81.     if( maxY < y[i] ) maxY = y[i];
  82.   }
  83.   scaleX = (double) bufWidth  / ( maxX - minX );
  84.   scaleY = (double) bufHeight / ( maxY - minY );
  85.   scale = ( scaleX > scaleY ) ? ( scaleX ) : ( scaleY );
  86.  
  87.   memset( buffer, 0, bufWidth * bufHeight );
  88.   for( i=0 ; i<numPts ; i++)
  89.   {
  90.     int col = ( ( x[i] - minX ) / scale + 0.5 );
  91.     int row = ( ( y[i] - minY ) / scale + 0.5 );
  92.     buffer[ row * bufWidth + col ] = 255;
  93.   }
  94. }
  95.  
  96. /********************/
  97. int main( int argc, char *argv[] )
  98. {
  99.   unsigned char *buffer;
  100.   int           bufDim = 400;
  101.   int           numPts = 10000;
  102.   double        *x, *y;
  103.  
  104.   x = (double *) malloc( numPts * sizeof( double ) );
  105.   y = (double *) malloc( numPts * sizeof( double ) );
  106.   buffer = (unsigned char *) malloc( bufDim*bufDim * sizeof( unsigned char ) );
  107.  
  108.   GeneratePoints( numPts, x, y );
  109.   DrawDots( numPts, x, y, buffer, bufDim, bufDim );
  110.   write_JPEG_file( "test.jpg", 85, (JSAMPLE *) buffer, bufDim, bufDim, 1 );
  111. }

Quellcode

Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. PASTEBIN_DOWNLOAD_SNIPPET_EXPLAIN