Logo by Pauldelbrot - 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: Check out the originating "3d Mandelbulb" thread here
 
*
Welcome, Guest. Please login or register. May 02, 2026, 11:49:30 AM


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 29452 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 536 times.)
* mandelbrot.cpp.zip (0.44 KB - downloaded 568 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 582 times.)
* burning_ship.txt (31.75 KB - downloaded 489 times.)
* perpendicular_burning_ship.txt (31.75 KB - downloaded 415 times.)
* celtic.txt (31.75 KB - downloaded 417 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:  

Related Topics
Subject Started by Replies Views Last post
Programmer needed to add a few lines of code to RenderFlam3 Let's collaborate on something! « 1 2 » 3dpete 15 10724 Last post August 01, 2012, 12:21:35 PM
by thargor6
two mandelbrot number lines Images Showcase (Rate My Fractal) Eric B 0 4908 Last post September 24, 2012, 04:25:34 PM
by Eric B
Fractal Fun: Tweet-a-Program Mandelbrot Code Challenge Competitions and Contests Geonat 0 8456 Last post November 18, 2014, 12:06:17 PM
by Geonat
Easy tutorial: Mandelbrot fractals in Excel - only 16 lines of code Programming excel 1 10146 Last post November 11, 2016, 02:10:59 PM
by TheRedshiftRider
Source code: Mandelbrot, C#, OpenCL and OpenGL Programming woronoi 3 9108 Last post November 16, 2016, 04:26:47 PM
by woronoi

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.133 seconds with 28 queries. (Pretty URLs adds 0.012s, 2q)