Advertisement
Guest User

random art

a guest
Jan 15th, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. #include <time.h>
  4. #include <math.h>
  5. #include <SDL/SDL.h>
  6.  
  7. #define PI 3.14159265
  8. SDL_Surface* screen = NULL;
  9.  
  10. int width = 256;
  11. int height = 256;
  12. //start SDL
  13. bool init(){
  14.  
  15.     if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) {
  16.         return false;
  17.     }
  18.     screen = SDL_SetVideoMode( width, height, 32, SDL_SWSURFACE);
  19.     if(!screen){
  20.         return false;
  21.     }
  22.     srand( time(NULL) );
  23.  
  24.     return true;
  25. }
  26.  
  27. //make a color
  28. Uint32 color(int r, int g, int b){
  29.     Uint32 c = SDL_MapRGB(screen->format, r, g, b);
  30.  
  31.     return(c);
  32. }
  33.  
  34. //make a random number
  35. int randomNum(int min, int max){
  36.     int ranNum = rand() % max + min;
  37.     return(ranNum);
  38. }
  39.  
  40. void point(float x, float y, Uint32 c){
  41.     SDL_FillRect(screen, &SDL_Rect{x,y,1,1}, c);
  42. }
  43.  
  44. float avg(long double x, long double y){
  45.     return ((x+y)/2);
  46. }
  47.  
  48. //SDL_FillRect(screen, &SDL_Rect {x, y, w, h}, color(r, g, b) );
  49. //    SDL_FillRect(screen, &screen->clip_rect, color(0,0,0) );
  50. // sin(PI*_/180)
  51. int main ( int argc, char** argv )
  52. {
  53.     long double r=0;
  54.     long double g=0;
  55.     long double b=0;
  56.     double pi = PI/180;
  57.  
  58.     if(!init()){
  59.         return(1);
  60.     }
  61.  
  62.     for(int x=1; x < width; x++){
  63.         //cout << sin(pi*/180)*128+128 << endl;
  64.         for(int y=1; y<height; y++){
  65.             r = avg(sin(x*x), cos(y*y))*sin(pi*x);
  66.             //r = x*x + y*y;
  67.             g = sin(pi*x*cos(pi*y*x));
  68.             b = 0.7*(sin(pi*x*x)*cos(pi*y*y));
  69.             cout << r << endl;
  70.             point(x, y, color(r*128+128,g*128+128, b*128+128));
  71.            
  72.         }
  73.  
  74.     }
  75.     SDL_Flip(screen);
  76.     SDL_SaveBMP(screen, "picture");
  77.     SDL_Delay(2000);
  78.     SDL_FreeSurface(screen);
  79.     SDL_Quit();
  80.  
  81.     return(0);
  82.  
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement