Write balensley fern in C Language
First, I wrote the program in VS2015, as shown in figureIf you cannot load this file in VS2015, read my previous article and find a solution.
The core of the code is to use the SetPixel (hdc, x, y, color) function to generate the pixel image of bayensley fern.
Directly paste the Code:
#include<graphics.h>
void main() {
HWND hwnd = NULL;
HDC hdc = GetDC(hwnd);
srand(0);
double x = 0.0, y = 0.0;
for (int i = 0; i < 200000; i++) {
int r = rand() % 100;
if (r == 0) { x = 0; y = 0.16*y; }
else if (r > 0 && r <= 85) { x = 0.85*x + 0.04*y; y = (-0.04)*x + 0.85*y + 1.6; }
else if (r>85&&r<=92) { x = 0.2*x + (-0.26)*y; y = 0.23*x + 0.22*y + 1.6; }
else { x = (-0.15)*x + 0.28*y; y = 0.26*x + 0.24*y + 0.44; }
SetPixel(hdc, (int)((x * 25) + 200), (int)((y * 25) + 100), 0x00FF00);
}
}
Running result:
PS: I am also a beginner and have limited capabilities. Therefore, after generating the bayensley fern, try not to shake the mouse as much as possible. Otherwise, the generated image will soon disappear, even if you do not stop for a few seconds, it will disappear.