exercise 2 ascii image and mandel brot set
play

Exercise 2: ASCII Image and Mandel- brot Set Karl Gmeiner 2015 - PDF document

Exercise 2: ASCII Image and Mandel- brot Set Karl Gmeiner 2015 Exercise 2: ASCII Image and Mandelbrot Set 1 Exercise 2: ASCII Image and Mandelbrot Set This exercise consists of two parts: First you should implement a class ASCIIImage, and


  1. Exercise 2: ASCII Image and Mandel- brot Set Karl Gmeiner 2015

  2. Exercise 2: ASCII Image and Mandelbrot Set 1 Exercise 2: ASCII Image and Mandelbrot Set This exercise consists of two parts: First you should implement a class ASCIIImage, and then use this class to draw some nice images. 1.1 ASCII Image An ASCII Image is a black-and-white image that consists of different characters instead of colored pixels. We obtain different shades of gray by using characters with different density. Assuming that the background of the terminal is black and characters are white we will use the following different levels of gray (the leftmost character represents white while the rightmost represents black, do not forget the trailing space character): $@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,"^`'. You find these characters along with some ASCII art images also here: http://paulbourke.net/dataformats/asciiart/. You also may use a different set of characters. We want to create nice images in this exercise, therefore we have to implement a class ASCIIImage: • The constructor of the ASCII Image has two arguments: The width and height of the image (as integers). • The ASCIIImage contains one method void setPix(int x, int y, int color) where color is the level of grey, 0 (and every number below 0) represents the space character, while 69 (and every larger number) is $ (depending on the set of characters you use, this value might be smaller). The left upper corner of the image has coordinates x=0, y=0 , the lower right corner x=width-1, y=height-1 . • In order to print the image, the class contains a method void show() that prints the image using cout . • Do not forget to implement a destructor if you allocate memory in the class (memory leaks will count as errors). • After creation, the image should be blank (only consist of spaces). The following example code should print the following result: ASCIIImage img(16, 16); for(int x = 0; x < 16; ++x) { for(int y = 0; y < 16; ++y) { img.setPix(x, y, x * y); } } img.show(); .'`^",:;Il!i><~ '^,;li<+-]}1(\t `,Ii~-[1|truzUL ^;i+]1\juXCOqk* "l~])tnXLmb*8$$ ,i-1tuUOd*%$$$$ 1

  3. Exercise 2: ASCII Image and Mandelbrot Set :<[\nUZkW$$$$$$ ;+1jXOk&$$$$$$$ I-|uLdW$$$$$$$$ l]tXm*$$$$$$$$$ !}rCb%$$$$$$$$$ i1uO*$$$$$$$$$$ >(zq8$$$$$$$$$$ <\Uk$$$$$$$$$$$ ~tL*$$$$$$$$$$$ 1.2 Fractal drawing Next, we would like to draw a part of the so-called mandelbrot set (after Benoit Mandelbrot, its discoverer). The Mandelbrot Set is a fractal, a mathematical monster, that is nice to look at. Such structures are important for creating realistic maps or plants in computer graphics and they also appear in all kinds of chaotic behavior, from coast lines to weather simulations. Mathematically (disclaimer, you do not need to know the following, but it certainly is not a bad idea): The mandelbrot set is calculated by repeatedly iterating the function f ( z ) = z 2 + c and observe its behavior for a certain point c in the complex plane. This behavior is one of the following four: Either a point is repellant (moving towards infinity), or it is converging towards a fixed point, or it is jumping between various points (periodically), or it has chaotic behavior (which is related to the previous case). We are interested in the points that are repellant. Usually, the fractal is drawn by picking a color depending how repellant the point is. You find more details and some images here: http://en.wikipedia.org/wiki/Mandelbrot_set . Practically: Given two coordinates x and y and a start value zr = 0 and zi = 0 we execute the function until zr, zi exceed a certain value (here we will use 4). Since we cannot do this infinitely long, we also need a maximum level of iterations ( maxi ). We then pick a color based on the iteration level at which zr and zi exceeded this bailout value. If we reach the maximum iteration depth, we simply leave the point blank // input: x and y. zr = 0.; zi = 0.; for(int i = 0, i < maxi; ++i) { // calculate next iteration // For the interested reader: z=zr+i*zi and c=x+i*y are complex numbers // In the next row we calculate z * z + c and check whether |z| > 2 double nextzr = zr * zr - zi * zi + x; double nextzi = 2 * zr * zi + y; // are we done? if(nextzr * nextzr + nextzi * nextzi > 4) { // TODO: set pixel to i % 70 // Insert here code to draw break; } zr = nextzr; zi = nextzi; } 2

  4. Exercise 2: ASCII Image and Mandelbrot Set You can modify the code above, eg. if the condition inside the for-loop is never satisfied, then the pixel is left blank. Another option is to set it to maxi % 70 which is used in the later examples. Of course, we are not that interested of drawing the fractal for the screen coordinates like (0 , 0) – (80 , 25) . We want to input the coordinates of the left upper corner (for instance − 2 , − 2 ) and the right lower corner (for instance 2 , 2 ) of the mandelbrot set. An important task for you is therefore to scale the screen coordinates (integer values from 0 to width-1 and/or height-1 ) to the real world coordinates (double values depending on the input). 1.3 Detailed exercise Create (at least) four files: asciiimage.h , asciiimage.cpp , mandelbrot.cpp and Makefile to build the program. The file mandelbrot.cpp contains the main-procedure. In this procedure, the following arguments should be entered: • Size of the image (width and height, 2 integers), • Position of the left upper point and right lower point (4 doubles), • Number of iterations ( maxi in the code snippet above). 1.4 Examples In these examples, the inner part of the mandelbrot set is set to maxi % 70 . Furthermore, cr and ci are the coordinates of the left upper corner of a pixel. $ ./mandelbrot Enter Width: 80 Enter Height: 25 Enter x0: -3 Enter y0: -1.5 Enter x1: 1 Enter y1: 1.5 Enter max: 50 .............................................. ................................................ ............'''''''''''''''''''.................... .......'''''''''''''''''````^^^```''''.............. .....''''''''''''''''``````^^",<:,,`````''''.......... ...''''''''''''''''```````^^^":;n{l:"^^````'''''....... ..'''''''''''''''```````^^""",:-mmmm>:,"^^^```'''''..... .'''''''''''''''`````^^",n+l;ivm]{mm}+m![:,,],^`''''''... .''''''''''''```^^^^^^"",:Immmmmmmmmmmmmmmmmm>:^^`''''''.. .''''''''```^,;"""""",,,:lnmmmmmmmmmmmmmmmmmmmm:,^``''''''. '''``````^^^",I~?!ixil;;l{mmmmmmmmmmmmmmmmmmmmm<:"``''''''' '`````^^^^^,:I~[mmmmmmmf-mmmmmmmmmmmmmmmmmmmmmm!,^```'''''' ^^"",,"",,;immmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm<,"^```'''''' ^^"",,"",,;immmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm<,"^```'''''' '`````^^^^^,:I~[mmmmmmmf-mmmmmmmmmmmmmmmmmmmmmm!,^```'''''' '''``````^^^",I~?!ixil;;l{mmmmmmmmmmmmmmmmmmmmm<:"``''''''' .''''''''```^,;"""""",,,:lnmmmmmmmmmmmmmmmmmmmm:,^``''''''. .''''''''''''```^^^^^^"",:Immmmmmmmmmmmmmmmmm>:^^`''''''.. .'''''''''''''''`````^^",n+l;ivm]{mm}+m![:,,],^`''''''... ..'''''''''''''''```````^^""",:-mmmm>:,"^^^```'''''..... ...''''''''''''''''```````^^^":;n{l:"^^````'''''....... .....''''''''''''''''``````^^",<:,,`````''''.......... 3

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend