Visaulization of Newton's Fractal written in C/C++. This program generates Newton's fractal based on polynomial provided in parameters and saves it to .png
image.
-
Linux (tested on Ubuntu 20.04 LTS)
-
g++ compiler
sudo apt intall g++
- ImageMagick
sudo apt install imagemagick
- Basic polynomials understanding :D
sudo learn polynomials
For better expirience i recommend you to watch awesome 3Blue1Brown's video about Newton's fractal and read Wikipedia article.
Download generate
file from root directory and call it
./generate "z^3 - 1"
If for some reason it doesn't work you can complie source code localy:
g++ -I/src src/main.cpp src/Complex.cpp -o generate -ldl
and call program as shown above.
To generate fractal you have to provide this argument.
Just put it in quotes after ./generate
like this:
./generate "z^4 - 1"
az^n
where:
- a is an integer
- z is variable for polynomial (don't change it)
- n is a power of
z
./generate "-2z^14 - 2z^2 - z - 9"
./generate "5z^9 + 2z^8 - z^4 - 9"
./generate "-z^5 + 2z^4 - 19z^3 + z^2 - 18z - 2"
You can pick any polynomial as long as:
This is NOT allowed:
./generate "(z - 9)(z + 2)(z - 12)(z + 16)"
This is NOT allowed:
./generate "z^9*2 + 2z^2/8 - z^1/2 - 9"
You can also use tygonometric function like sin
, cos
, sinh
, cosh
;
./generate "sin(z)"
./generate "2cosh(z)"
./generate "z^9 - 14z^4 - sinh(z) - 5"
./generate "-4cos(z) + z^8"
For some reason if you pass more than one trygonometric function it get's messy and takes a lot more time to generate, but I'm not smart enough to figure that out :)
Specify desired size of output image.
Default value is 500px x 500px
--size widthxheight
Where width
and height
are int
Example:
./generate "z^3 - 1" --size 1000x1000
This will generate image of size 1000px x 1000px
Specify name of output image
Default value is Newtons-Fractal
--name name
Where name
is string
Example:
./generate "z^3 - 1" --name my_name_for_fractal
This will save the result in my_name_for_fractal.png
Specify zoom of image
Default value is 1
--zoom zoom
where zoom
is double
If zoom < 1
image will be zoomed out.
If zoom > 1
image will be zoomed in.
Example:
./generate "z^3 - 1" --zoom 0.1
This will zoom out 10 times.
Example:
./generate "z^3 - 1" --zoom 10
This will zoom in 10 times.
Turn off saturation
All colors won't have shades.
Example:
--no-saturation
Program contains 4 steps:
It takes polynomial provided in parameters and generates code compatible with Complex
library and saves it to tmp.cpp
.
Then it compiles generated code and creates library called tmp.so
Next step is loading generated library so generator can use functions created by parsing provided polynomial.
Last but not least it generates fractal. If you are interested in how exacly this works I strongly recommend you
awesome 3Blue1Brown's video about this topic. Every time different colors are picked,
so you can easly say that your fractal is unique😊. When fractal is generated
it is saved to .ppm
file. For user's convinience it is later converted to .png
file and .ppm
file is deleted.
./generate "z^4 - 17" --name gallery/1 --zoom 0.7 --size 1920x1080
./generate "-3cos(z) + z^13" --name gallery/2 --zoom 0.7 --size 1920x1080
./generate "-cosh(z) + z^4" --name gallery/3 --zoom 2 --size 1920x1080
./generate "-7z^9 + 14z^7 - sinh(z) + 17" --name gallery/4 --zoom 0.8 --size 1920x1080
./generate "z^6 + z^5 + z^4 + z^3 + z^2 + z + 1" --name gallery/5 --zoom 1 --size 1920x1080
./generate "sin(z) + cos(z) - sinh(z)" --name gallery/6 --zoom 1 --size 1920x1080
./generate "-9sin(z)" --name gallery/7 --zoom 0.1 --size 1920x1080
./generate "cosh(z) - z^20 + cos(z)" --name gallery/8 --zoom 1
./generate "sinh(z) - z^20 + cos(z) - sin(z)" --name gallery/9 --zoom 1 --size 1920x1080