Creating Butterfly themed Fractal in C++ Using Graphics
In Layman’s terms, Fractals are beautiful patterns brought into existence upon the intertwining of computation and mathematics. To get a bit technical they are recursive in nature such that when looked upon a certain subset of a given fractal a similar pattern appears to emerge out of it.
In this article, a new type of fractal pattern is generated, given as below:
Approach:
- Recursively go to the end branch of a Fractal tree.
- When the length of the branch reaches our threshold value, draw out that particular branch and exit out of the function.
- To keep the colors similar for a few iterations, we use type casting since ((colorVal%15000)/1000+1) would give out spectrums of same values, i.e. same colour thousand times before it iterates.
Below is the implementation of the above approach:
#include <math.h>
#include <stdlib.h>
#include <winbgim.h>
#define Y 1080
#define X 1920
#define c 5 * M_PI / 7
int colorVal = 0;
int Pyt_Tree( float x, float y, float r, float ang)
{
if (r < 0.1) {
setcolor((colorVal++ % 15000) / 1000 + 1);
line(x, y, x - r * sin (ang), y - r * cos (ang));
return 0;
}
float r_n;
if (c > M_PI / 4) {
r_n = sin (c);
}
else {
r_n = cos (c);
}
Pyt_Tree(x - r * sin (ang), y - r * cos (ang),
r / (2 * r_n), (ang + c));
Pyt_Tree(x - r * sin (ang), y - r * cos (ang),
r / (2 * r_n), (ang - c));
return 0;
}
int main()
{
initwindow(X, Y);
Pyt_Tree(X / 2, Y, Y * 0.9, 0);
getch();
closegraph();
return 0;
}
|
Output:
Last Updated :
16 Dec, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...