How to resolve the algorithm Koch curve step by step in the C programming language
Published on 7 June 2024 03:52 AM
How to resolve the algorithm Koch curve step by step in the C programming language
Table of Contents
Problem Statement
Draw a Koch curve. See details: Koch curve
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Koch curve step by step in the C programming language
This C program draws the Koch curve, which is a fractal curve known for its self-similarity and infinite perimeter. The program uses the graphics.h library to draw the curve in a graphics window. It takes three command-line arguments: the width and height of the graphics window and the number of recursion levels for the Koch curve.
Here's a breakdown of the code:
-
Header Includes:
- The program includes several header files:
<graphics.h>
: For graphics operations.<stdlib.h>
: For functions likeatoi
.<stdio.h>
: For input and output.<math.h>
: For mathematical functions likeM_PI
.
- The program includes several header files:
-
Data Structure:
- The program defines a
point
structure to represent 2D points withx
andy
coordinates.
- The program defines a
-
kochCurve
Function:- This recursive function draws the Koch curve between two points
p1
andp2
for a given number of recursion levelstimes
. - If
times
is greater than 0, it divides the curve into four segments using three new points (p3
,p4
, andp5
) and recursively callskochCurve
for each segment with one less recursion level. - If
times
is 0, it draws a straight line betweenp1
andp2
.
- This recursive function draws the Koch curve between two points
-
main
Function:- Command-Line Arguments: It checks if exactly three command-line arguments are provided. If not, it prints an error message and exits.
- Window Parameters: It reads the window width (
w
), height (h
), and recursion level (r
) from the command line arguments and initializes a graphics window with these parameters. - Initialization: It initializes two points,
p1
andp2
, which define the endpoints of the initial Koch curve. - Koch Curve Drawing: It calls the
kochCurve
function to draw the Koch curve betweenp1
andp2
with recursion levelr
. - User Input: It calls
getch()
to wait for the user to press any key before closing the graphics window. - Cleanup: It closes the graphics window using
closegraph()
.
Source code in the c programming language
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#define pi M_PI
typedef struct{
double x,y;
}point;
void kochCurve(point p1,point p2,int times){
point p3,p4,p5;
double theta = pi/3;
if(times>0){
p3 = (point){(2*p1.x+p2.x)/3,(2*p1.y+p2.y)/3};
p5 = (point){(2*p2.x+p1.x)/3,(2*p2.y+p1.y)/3};
p4 = (point){p3.x + (p5.x - p3.x)*cos(theta) + (p5.y - p3.y)*sin(theta),p3.y - (p5.x - p3.x)*sin(theta) + (p5.y - p3.y)*cos(theta)};
kochCurve(p1,p3,times-1);
kochCurve(p3,p4,times-1);
kochCurve(p4,p5,times-1);
kochCurve(p5,p2,times-1);
}
else{
line(p1.x,p1.y,p2.x,p2.y);
}
}
int main(int argC, char** argV)
{
int w,h,r;
point p1,p2;
if(argC!=4){
printf("Usage : %s <window width> <window height> <recursion level>",argV[0]);
}
else{
w = atoi(argV[1]);
h = atoi(argV[2]);
r = atoi(argV[3]);
initwindow(w,h,"Koch Curve");
p1 = (point){10,h-10};
p2 = (point){w-10,h-10};
kochCurve(p1,p2,r);
getch();
closegraph();
}
return 0;
}
You may also check:How to resolve the algorithm Sorting algorithms/Selection sort step by step in the AWK programming language
You may also check:How to resolve the algorithm Sorting algorithms/Cocktail sort step by step in the Prolog programming language
You may also check:How to resolve the algorithm Partition function P step by step in the Haskell programming language
You may also check:How to resolve the algorithm 100 doors step by step in the Dc programming language
You may also check:How to resolve the algorithm Doubly-linked list/Traversal step by step in the Delphi programming language