How to resolve the algorithm Barnsley fern step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Barnsley fern step by step in the C programming language

Table of Contents

Problem Statement

A Barnsley fern is a fractal named after British mathematician Michael Barnsley and can be created using an iterated function system (IFS).

Create this fractal fern, using the following transformations: Starting position: x = 0, y = 0

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Barnsley fern step by step in the C programming language

The provided C program generates a fractal pattern known as the "Barnsley Fern" using the chaos game algorithm. Here's a detailed explanation of the code:

  1. Header Files:

    • The program includes necessary header files:
      • <graphics.h>: For graphics functions to draw the fractal.
      • <stdlib.h>: For random number generation.
      • <stdio.h>: For input and output operations.
      • <time.h>: For time-based operations.
  2. barnsleyFern Function:

    • This function generates the Barnsley Fern fractal with the given window width and number of iterations.
    • It initializes variables x0, y0 to represent the starting point of the fractal.
    • A dice roll (random number between 0 and 99) determines which transformation to apply at each iteration.
    • Based on the dice roll, it calculates x1 and y1, the next point in the fractal.
    • It draws the pixel at the computed coordinates using putpixel.
    • The function updates x0 and y0 to continue the fractal generation.
  3. main Function:

    • It prompts the user to enter the number of iterations.
    • Initializes the graphics window using initwindow.
    • Calls the barnsleyFern function to generate the fractal.
    • Waits for user input using getch.
    • Closes the graphics window using closegraph.
  4. Program Flow:

    • The user enters the number of iterations.
    • The graphics window opens.
    • The barnsleyFern function generates the fractal and draws it on the window.
    • The program waits for the user to press a key to close the window.

Source code in the c programming language

#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>

void barnsleyFern(int windowWidth, unsigned long iter){
	
	double x0=0,y0=0,x1,y1;
	int diceThrow;
	time_t t;
	srand((unsigned)time(&t));
	
	while(iter>0){
		diceThrow = rand()%100;
		
		if(diceThrow==0){
			x1 = 0;
			y1 = 0.16*y0;
		}
		
		else if(diceThrow>=1 && diceThrow<=7){
			x1 = -0.15*x0 + 0.28*y0;
			y1 = 0.26*x0 + 0.24*y0 + 0.44;
		}
		
		else if(diceThrow>=8 && diceThrow<=15){
			x1 = 0.2*x0 - 0.26*y0;
			y1 = 0.23*x0 + 0.22*y0 + 1.6;
		}
		
		else{
			x1 = 0.85*x0 + 0.04*y0;
			y1 = -0.04*x0 + 0.85*y0 + 1.6;
		}
		
		putpixel(30*x1 + windowWidth/2.0,30*y1,GREEN);
		
		x0 = x1;
		y0 = y1;
		
		iter--;
	}

}

int main()
{
	unsigned long num;
	
	printf("Enter number of iterations : ");
	scanf("%ld",&num);
	
	initwindow(500,500,"Barnsley Fern");
	
	barnsleyFern(500,num);
	
	getch();
	
	closegraph();
	
	return 0;
}


  

You may also check:How to resolve the algorithm Levenshtein distance/Alignment step by step in the Tcl programming language
You may also check:How to resolve the algorithm Hello world/Newline omission step by step in the PL/I programming language
You may also check:How to resolve the algorithm Matrix transposition step by step in the D programming language
You may also check:How to resolve the algorithm Doubly-linked list/Element definition step by step in the BASIC programming language
You may also check:How to resolve the algorithm K-means++ clustering step by step in the Scheme programming language