Koch th


Objective:
The objective of this program is to draw the Koch Curve, a type of fractal, using recursive algorithms and the graphics library in C++. The Koch curve is a mathematical curve and one of the earliest fractals to be described.

šŸ“Œ What is a Koch Curve?
A Koch Curve is a recursive geometric shape constructed as follows:

Start with a straight line.

Divide it into 3 equal segments.

Replace the middle segment with two sides of an equilateral triangle (removing the base).

Repeat the process recursively for each segment.

As the number of iterations increases, the curve becomes more complex and resembles a snowflake edge.

šŸ”§ Key Concepts in the Program:
āœ… 1. Recursion
The function koch() is a recursive function that divides a line into smaller segments and constructs the Koch pattern on each segment.

The recursion continues until the specified number of iterations (it) becomes 0.

āœ… 2. Graphics in C++
The program uses <graphics.h> for drawing lines and initializing the graphics window.

initwindow() creates a graphical window.

line(x1, y1, x2, y2) is used to draw each line segment.

delay() and getch() are used to pause the output so the user can see the result.

āœ… 3. Trigonometry
The angle used to form the "peak" of the triangle in the Koch curve is 60 degrees, converted to radians.

cos() and sin() from <math.h> are used to calculate the coordinates of the third point.

🧮 Important Calculations:
Let the initial line be from point A (x1, y1) to point B (x2, y2).

We calculate:

Point C: 1/3 distance from A to B

Point D: 2/3 distance from A to B

Point E: Peak of the equilateral triangle, calculated using rotation formulas with trigonometric functions.

šŸ” Recursive Steps:
For each recursion:

Break the line into 4 parts:

Line from A to C

Line from C to E

Line from E to D

Line from D to B

Call koch() on each segment with it - 1.

šŸŽÆ Output:
As the recursion depth increases, the line turns into a more detailed fractal pattern.

For iteration = 0, it draws a simple straight line.

For higher iterations (e.g., 3 or 4), it starts to resemble a snowflake pattern.

āœ… Conclusion:
This program demonstrates how recursion and basic geometry can be combined to generate complex visual patterns using computer graphics. The Koch Curve is a classical example of a fractal and is useful in learning recursive drawing, geometry, and graphics programming.

1: Start
Step 2: Initialize graphics
Use initwindow() to create a graphics window.

Define the initial coordinates of the line segment:
x1 = 100, y1 = 300, x2 = 500, y2 = 300.

Step 3: Input number of iterations
Prompt the user to enter the number of iterations a.

Step 4: Call the recursive koch() function
Pass the endpoints of the line and the number of iterations to the function:
koch(x1, y1, x2, y2, a)

šŸ” Recursive Function: koch(x1, y1, x2, y2, it)
Step 5: If it > 0 (i.e., more iterations are needed)
Calculate points x3, y3 as 1/3 distance from start.

Calculate points x4, y4 as 2/3 distance from start.

Calculate the peak point (x, y) of the equilateral triangle using:

Rotation formulas with 60° angle converted to radians.

Recursively call:

koch(x1, y1, x3, y3, it - 1)

koch(x3, y3, x, y, it - 1)

koch(x, y, x4, y4, it - 1)

koch(x4, y4, x2, y2, it - 1)

Step 6: If it == 0 (base case)
Draw four lines to form the Koch segment:

line(x1, y1, x3, y3)

line(x3, y3, x, y)

line(x, y, x4, y4)

line(x4, y4, x2, y2)

Step 7: End of recursion
Step 8: Wait for key press using getch()
Display the result until a key is pressed.

Step 9: End