Logo by mrob - Contribute your own Logo!

END OF AN ERA, FRACTALFORUMS.COM IS CONTINUED ON FRACTALFORUMS.ORG

it was a great time but no longer maintainable by c.Kleinhuis contact him for any data retrieval,
thanks and see you perhaps in 10 years again

this forum will stay online for reference
News: Follow us on Twitter
 
*
Welcome, Guest. Please login or register. March 29, 2024, 12:23:37 PM


Login with username, password and session length


The All New FractalForums is now in Public Beta Testing! Visit FractalForums.org and check it out!


Pages: [1]   Go Down
  Print  
Share this topic on DiggShare this topic on FacebookShare this topic on GoogleShare this topic on RedditShare this topic on StumbleUponShare this topic on Twitter
Author Topic: Mandelbrot with only 18 lines of c++ code!  (Read 20950 times)
Description: Only 443 bytes of code!
0 Members and 1 Guest are viewing this topic.
wes
Alien
***
Posts: 33



« on: March 13, 2017, 10:56:11 AM »

Hello, everybody! cheesy I'm new to this forum. I only started playing with fractals recently but I've been absolutely obsessed. I want to share some code that I think is hilarious:

Code:
#include <complex>
#include <iostream>
constexpr auto max_row = 22, max_column = 78, max_iteration = 20;
int main(){
for(auto row = 0; row < max_row; ++row){
for(auto column = 0; column < max_column; ++column){
std::complex<float> z, c = {
(float)column * 2 / max_column - 1.5f,
(float)row * 2 / max_row - 1
};
int iteration = 0;
while(abs(z) < 2 && ++iteration < max_iteration)
z = pow(z, 2) + c;
std::cout << (iteration == max_iteration ? '#' : '.');
}
std::cout << '\n';
}
}

Thanks to the standard library, working with complex numbers is very simple. std::complex has every operator overload and math function you could ever need and it's templated! Read more here: http://en.cppreference.com/w/cpp/numeric/complex
If you compile and run this code you get this:
Code:
..............................................................................
......................................................#.......................
..................................................########....................
.................................................##########...................
....................................##.#....##################.###............
....................................####################################......
................................########################################......
...............................############################################...
.............###.#####.#.......###########################################....
.........#.#################..############################################....
.....#.#..################################################################....
######################################################################........
.....#.#..################################################################....
.........#.#################..############################################....
.............###.#####.#.......###########################################....
...............................############################################...
................................########################################......
....................................####################################......
....................................##.#....##################.###............
.................................................##########...................
..................................................########....................
......................................................#.......................
I've attached a file named "mandelbrot.cpp" which you can download, compile, and run for yourself. If you are using Ubuntu like me, all you need is
Code:
g++ mandelbrot.cpp


PS Don't be shy. I want to get to know you all. If you're working on something that isn't top secret tell me what it is smiley

* mandelbrot.cpp.zip (0.42 KB - downloaded 397 times.)
* mandelbrot.cpp.zip (0.44 KB - downloaded 442 times.)
« Last Edit: March 13, 2017, 11:56:11 AM by wes, Reason: Edited code. » Logged
Sabine
Fractal Fertilizer
*****
Posts: 373



WWW
« Reply #1 on: March 13, 2017, 02:05:51 PM »

 cheesy cheesy cheesy This is fun! and cool!
Thanks for sharing:)
And welcome to FF!
Logged

sabine62.deviantart.com
DarkBeam
Global Moderator
Fractal Senior
******
Posts: 2512


Fragments of the fractal -like the tip of it


« Reply #2 on: March 13, 2017, 04:39:56 PM »

Benoit Mandelbrot "rendered" the first Mb set using a very similar textual approach
So ... nice try A Beer Cup
Logged

No sweat, guardian of wisdom!
wes
Alien
***
Posts: 33



« Reply #3 on: March 13, 2017, 09:55:25 PM »

Benoit Mandelbrot "rendered" the first Mb set using a very similar textual approach
So ... nice try A Beer Cup
I was aware of his printout. I forgot how nice his looks. Way better than mine lol:
Logged
wes
Alien
***
Posts: 33



« Reply #4 on: March 14, 2017, 05:38:04 AM »

cheesy cheesy cheesy This is fun! and cool!
Thanks for sharing:)
And welcome to FF!
Thanks. I checked out some of your most recent images and they are beautiful!
Logged
wes
Alien
***
Posts: 33



« Reply #5 on: March 14, 2017, 12:02:30 PM »

Sorry, guys, but I've wasted more time on this. I tripled the size of the code for no good reason so I won't to share that, but here's some high resolution text files (255 by 127) of the Mandelbrot set, the Burning Ship set, the Perpendicular Burning Ship set, and the Celtic set. They are 255 columns by 127 rows so I've attached them as files. I changed the pound-signs to asterisks and added periods for anything escape time greater than 1 so you can see the bounding circle of the set.

* mandelbrot.txt (31.75 KB - downloaded 449 times.)
* burning_ship.txt (31.75 KB - downloaded 361 times.)
* perpendicular_burning_ship.txt (31.75 KB - downloaded 305 times.)
* celtic.txt (31.75 KB - downloaded 300 times.)
Logged
3dickulus
Global Moderator
Fractal Senior
******
Posts: 1558



WWW
« Reply #6 on: March 15, 2017, 01:22:53 AM »

you're not the only one to spend time on this, these (when line feeds are added) are 5 or 6 lines. http://www.iwriteiam.nl/SigProgM.html

but my favorite is http://www.fractalforums.com/index.php?action=gallery;sa=view;id=15596 where the code itself looks like the mandelbrot set wink

edit: and it's not at all a waste of time if...
        A) you enjoy it.
        B) you learn from it.
        C) you share it.
        D) others enjoy and learn too.

    Welcome to FF cheesy
« Last Edit: March 15, 2017, 05:42:14 AM by 3dickulus » Logged

Resistance is fertile...
You will be illuminated!

                            #B^] https://en.wikibooks.org/wiki/Fractals/fragmentarium
wes
Alien
***
Posts: 33



« Reply #7 on: March 15, 2017, 09:07:18 PM »

you're not the only one to spend time on this, these (when line feeds are added) are 5 or 6 lines. http://www.iwriteiam.nl/SigProgM.html
Wow, I had no Idea that was a thing. Those are completely unreadable lol.
Logged
FractalStefan
Explorer
****
Posts: 45



WWW
« Reply #8 on: May 14, 2017, 02:30:46 AM »

Thanks to the standard library, working with complex numbers is very simple. std::complex has every operator overload and math function you could ever need and it's templated! Read more here: http://en.cppreference.com/w/cpp/numeric/complex

And what about the speed? Isn't this much slower than using "normal" numbers?

P.S.: Mandelbrot in 14 Lines of HTML/JavaScript: grin

<!DOCTYPE html><html><head><script type="text/javascript">
function Start() {
   canvas = document.getElementById('canvas'); context = canvas.getContext('2d');
   img = context.getImageData(0, 0, canvas.width, canvas.height);
   for (var yPos = 0, iData = 0; yPos < canvas.height; yPos++) {
      var y = 1.2 - yPos * 2.4 / canvas.height;
      for (var xPos = 0; xPos < canvas.width; xPos++) {
         var x = -2.2 + xPos * 3.2 / canvas.width;
         var a = 0, b = 0, a2 = 0, b2 = 0, it = 0;
         while (it < 256 && a2 + b2 < 25) b = 2 * a * b + y, a = a2 - b2 + x, a2 = a * a, b2 = b * b, it++;
         var color = (it == 256) ? 0 : Math.pow((it + 2 - Math.log(Math.log(a2 + b2)) / Math.log(2))/255, 1/5) * 255;
         img.data[iData++] = color, img.data[iData++] = color, img.data[iData++] = 0, img.data[iData++] = 255; } }
   context.putImageData(img, 0, 0); }
</script></head><body onload="Start();"><canvas id="canvas" width="600" height="450"></canvas></body></html>


Result:


Source:
http://www.stefanbion.de/tmp/minimandel.htm
Logged
Pages: [1]   Go Down
  Print  
 
Jump to:  


Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines

Valid XHTML 1.0! Valid CSS! Dilber MC Theme by HarzeM
Page created in 0.188 seconds with 24 queries. (Pretty URLs adds 0.012s, 2q)