Advertisement
Guest User

Mandel

a guest
Apr 16th, 2014
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.27 KB | None | 0 0
  1. // std headers
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <assert.h>
  5.  
  6. // logic defines
  7. #define TRUE   1
  8. #define FALSE  0
  9.  
  10. // ascii-brot size
  11. #define HEIGHT 64
  12. #define WIDTH  64
  13. #define MAX_ITERATIONS 255
  14.  
  15. int escapeSteps(int pixelX, int pixelY, int zoom);
  16.  
  17. int main(int argc, char * argv[]) {
  18.     printf(" UNSW Computing 1 - ASCII-BROT\n");
  19.    
  20.     int pixelX = 0;
  21.     int pixelY = 0;
  22.     int zoom = 7;
  23.    
  24.     int iterations = 0;
  25.    
  26.     while (pixelY < HEIGHT) {
  27.         // iterate through each column
  28.         while (pixelX < WIDTH) {
  29.             // iterate through the whole row, until it reaches the end
  30.             iterations = mandelbrot(pixelX, pixelY, zoom);
  31.  
  32.             if (iterations == MAX_ITERATIONS) {
  33.                 printf("*");
  34.             } else {
  35.                 printf(" ");
  36.             }
  37.             pixelX++;
  38.         }
  39.         // where it then goes back to the beginning and moves up a pixel
  40.         printf("\n");
  41.         pixelX = 0;
  42.         pixelY++;
  43.     }
  44.    
  45.     return EXIT_SUCCESS;
  46. }
  47.  
  48. // Finish off the function below, let me know if you have any questions =)
  49.  
  50. int escapeSteps(int pixelX, int pixelY, int zoom) {
  51.     // For now, just to keep things simple, just say that we are going off
  52.     // (0,0) as the centre. Later on, you will have to pass centreX and
  53.     // centreY into the function since you get it from input
  54.     int centreX = 0;
  55.     int centreY = 0;
  56.    
  57.     // So zoom is the distance between each point on the cartesian plane,
  58.     // so if it was zoom = 1, distance is 2^-1, which is 0.5, so that means
  59.     // that the furthest point to the left or right that you will be testing
  60.     // must be whatever size you want to print (e.g. for hte BMP that will
  61.     // be 512), times the distance; and exact same concept for up and down.
  62.    
  63.     // So in this case, xIncrements is the distance between each x point,
  64.     // and same for y. We'll just say that they are 1/2^7 for now. Unfortunately,
  65.     // you will need to make your own function which calculates 2^x for you =P
  66.     double xDistance = 1/128.00;
  67.     double yDistance = 1/128.00;
  68.    
  69.     // Now to get the distance to the up/down/left/right you will go from the centre, think about
  70.     // how the xIncrements and the image size relate to this
  71.     double width = 0;
  72.     double height = 0;
  73.    
  74.     // Using the height and the width, you can get the points which are your
  75.     // minimums
  76.     double minX = 0;
  77.     double minY = 0;
  78.    
  79.     // And similarly, get the position of the point, from the pixel ( hint:
  80.     // think about how many xIncrements there will be between the minX and
  81.     // the pixel point
  82.     double x = 0;
  83.     double y = 0;
  84.    
  85.     // Since you're algorithm for finding number of steps was perfect, I'll just
  86.     // put it here, but it turns out that the first point that you check before
  87.     // updating the point is (0,0), so just I'm also going to just set that
  88.    
  89.     int iterations = 0;
  90.     double originalX = x;
  91.     double originalY = y;
  92.     double temp = 0;
  93.    
  94.     x = 0;
  95.     y = 0;
  96.    
  97.     while (x*x + y*y < 4 && iterations < MAX_ITERATIONS){
  98.         temp = x;
  99.         x = x*x - y*y + originalX;
  100.         y = 2*temp*y + originalY;
  101.        
  102.         iterations++;
  103.     }
  104.    
  105.     return iterations;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement