Advertisement
Michaelangel007

Cleaned up Tweetable Mathematical Art

Aug 14th, 2014
1,648
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  g++ tweetart.cpp -o tweetart
  3.  Original version: http://pastebin.com/uQkCQGhz
  4.  Cleaned up version: http://pastebin.com/index/uQkCQGhz
  5. */
  6. #include <stdio.h>
  7. #include <math.h>
  8. #include <stdint.h> // uint8_t
  9. #define DIM 1024
  10. #define DM1 (DIM-1)
  11. #define MAX_COLORS 255 // or 255, or 65535
  12. #define _sq(x) ((x)*(x))                           // square
  13. #define _cb(x) abs((x)*(x)*(x))                    // absolute value of cube
  14. #define _cr(x) (unsigned short)(pow((x),1.0/3.0))  // cube root
  15.  
  16. typedef uint8_t  u8;
  17. typedef uint16_t u16;
  18. u16 GR(int,int);
  19. u16 BL(int,int);
  20.  
  21. u16 RD(int i,int j){
  22.     return (i&j); // YOUR CODE HERE
  23. }
  24. u16 GR(int i,int j){
  25.     return (i&j); // YOUR CODE HERE
  26. }
  27. u16 BL(int i,int j){
  28.     return (i&j); // YOUR CODE HERE
  29. }
  30.  
  31. FILE *fp;
  32. void pixel_write(int i, int j){
  33.     static u16 color[3];
  34.     color[0] = RD(i,j) & MAX_COLORS;
  35.     color[1] = GR(i,j) & MAX_COLORS;
  36.     color[2] = BL(i,j) & MAX_COLORS;
  37.     if (MAX_COLORS < 256){
  38.         color[0] &= 0xFF;
  39.         color[0] |= ((color[1] & 0xFF) << 8);
  40.         color[1] = color[2];
  41.     }
  42.     // 8-bit = 3 bytes
  43.     //16-bit = 6 bytes
  44.     fwrite(color, 1+(MAX_COLORS>255), 3, fp);
  45. }
  46.  
  47. int main(){
  48.     fp = fopen("mathpic.ppm","wb");
  49.     if( !fp ) return -1;
  50.     fprintf(fp, "P6\n%d %d\n%d\n", DIM, DIM, MAX_COLORS);
  51.     for(int j=0;j<DIM;j++)
  52.         for(int i=0;i<DIM;i++)
  53.             pixel_write(i,j);
  54.     fclose(fp);
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement