1
Experiment no: 01
Experiment name: Draw a star pattern using C_Curve Algorithm.
Description: The C-Curve algorithm is a recursive method used in computer graphics to
generate fractal curves. It works by repeatedly dividing a line segment into two smaller
segments and rotating one part by 90 degrees to create a bent shape. To draw a star pattern
using the C-Curve algorithm, several initial line segments are drawn from a common center
point at equal angles. The C-Curve is then applied to each segment, and the recursive process
produces a symmetric star-like fractal pattern.
Algorithm:
1. Input two end points of a line: (x1,y1)(x_1, y_1)(x1,y1) and (x2,y2)(x_2, y_2)(x2,y2).
2. If recursion depth = 0, then draw the line directly.
3. Otherwise:
4. Find the midpoint (xm,ym)(x_m, y_m)(xm,ym) and rotate it 45° to get a new point.
5. Divide the line into two smaller segments:
(x1,y1)→(xm,ym)(x_1, y_1) \rightarrow (x_m, y_m)(x1,y1)→(xm,ym) and
(xm,ym)→(x2,y2)(x_m, y_m) \rightarrow (x_2, y_2)(xm,ym)→(x2,y2).
6. Apply recursion for both smaller segments with depth – 1.
7. Stop when depth reaches 0.
Source code:
#include<bits/stdc++.h>
#include<windows.h>
#include<GL/glut.h>
#define PI acos(-1)
using namespace std;
float x, y, len, alpha, x2, y2 ;
int n ;
void C_curve(float x, float y,float len,float alpha,int n)
if(n>0)
len = len / sqrt(2.0);
C_curve(x,y,len,alpha+45,n-1);
2
x = x + len * cos( (alpha + 45)*PI/180 );
y = y + len * sin ( (alpha + 45)*PI/180 );
C_curve(x,y,len,alpha - 45, n-1);
else
glBegin(GL_LINES);
glVertex2f(x,y);
glVertex2f(x + len * cos(alpha*PI/180), y + len * sin(alpha*PI/180) );
x2 = x + len * cos(alpha*PI/180), y2 = y + len * sin(alpha*PI/180) ;
glEnd();
void display(void)
C_curve(x,y,200,0,1);
x = x2, y = y2 ;
C_curve(x,y,200,72,1);
x = x2, y = y2 ;
C_curve(x,y,200,144,1);
x = x2, y = y2 ;
C_curve(x,y,200,216,1);
x = x2, y = y2 ;
C_curve(x,y,200,288,1);
glFlush();
}
3
void init(void)
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0,0,0,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-300,300,-300,300);
int main(int argc, char** argv)
printf("Enter the value of x & y : ");
scanf("%f %f",&x,&y);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("Star using C_Curve");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
Input & Output:
4
5
6
Discussion: Here ,the C-Curve function works recursively: each line segment is divided
into two smaller segments at a 90° angle until the recursion depth is reached. When the depth
becomes zero, a straight line is drawn using OpenGL line primitives. In the display()
function, the C-Curve is called five times with different initial angles (0°, 72°, 144°, 216°,
and 288°) to form the five arms of the star. I took initial coordinates (x,y)(x, y)(x,y) from the
user and calculated the end points of each recursive segment using trigonometric functions
(cos and sin). The star pattern will be displayed within a 2D orthographic projection using
gluOrtho2D.
7
Experiment no: 02
Experiment name: Draw a star pattern using Koch Curve Algorithm.
Description: The Koch Curve algorithm is a recursive method used in computer graphics
to generate fractal curves. It works by dividing a line segment into three equal parts,
replacing the middle part with two sides of an equilateral triangle, and then recursively
repeating the process on each resulting segment. This creates the well-known Koch
snowflake structure with a jagged, self-similar appearance.
Algorithm:
1. Input the two end points of a line: (x1,y1) and (x2,y2).
2. If the recursion depth is 0, draw the line directly.
3. Otherwise:
4. Divide the line into 3 equal parts: point A (1/3) and point B (2/3).
5. Construct an equilateral triangle on the middle segment AB and find the peak point P.
6. Now the line (x1,y1) to (x2,y2) is replaced by 4 segments:
a. (x1,y1)→A
b. A→P
c. P→B
d. B→(x2,y2)
7. Apply recursion on each segment with depth - 1.
8. Stop when depth reaches 0.
Source code:
#include<bits/stdc++.h>
#include<windows.h>
#include<GL/glut.h>
#define PI acos(-1)
using namespace std;
float x, y, len, alpha, x2, y2 ;
int n ;
void kach_curve(float x, float y,float len,float alpha,int n)
if(n>0)
{
8
len = len / 3;
kach_curve(x,y,len,alpha,n-1);
x = x + len * cos(alpha*(PI/180));
y = y + len * sin (alpha*(PI/180));
kach_curve(x,y,len,alpha-60,n-1);
x = x + len * cos((alpha-60)*(PI/180));
y = y + len * sin((alpha-60)*(PI/180));
kach_curve(x,y,len,alpha+60,n-1);
x = x + len * cos((alpha+60)*(PI/180));
y = y + len * sin((alpha+60)*(PI/180));
kach_curve(x,y,len,alpha, n-1);
else
glBegin(GL_LINES);
glVertex2f(x,y);
glVertex2f(x + len * cos(alpha*PI/180), y + len * sin(alpha*PI/180) );
x2 = x + len * cos(alpha*PI/180), y2 = y + len * sin(alpha*PI/180) ;
glEnd();
void display(void)
kach_curve(x,y,200,0,1);
x = x2, y = y2 ;
kach_curve(x,y,200,120,1);
x = x2, y = y2 ;
9
kach_curve(x,y,200,240,1);
x = x2, y = y2 ;
glFlush();
void init(void)
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1,1,1,1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-300,300,-300,300);
int main(int argc, char** argv)
printf("Enter the value of x & y : ");
scanf("%f %f",&x,&y);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800,800);
glutInitWindowPosition(100,100);
glutCreateWindow("Star using C_Curve");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
Input & Output:
10
11
12
Discussion: I used OpenGL and recursion to draw a star-like pattern with Koch curves.
The korch_curve function implemented the Koch Curve algorithm by recursively dividing
each line segment into four parts, with angular rotations of ±60° to form the characteristic
triangular [Link] the display function, the Koch Curve is applied three times at angles 0°,
120°, and 240° from a given starting point, which together form a star-like fractal structure.
Iinitialized an 800×800 window with a white background, took user input for the starting
coordinates, and drawn the fractal star using recursive line generation.
13
Experiment no: 03
Experiment name: Implementation of Gasket Algorithm.
Description: The Sierpinski Gasket algorithm is a recursive method used in computer
graphics to generate fractal patterns within a triangle. It works by dividing an equilateral
triangle into four smaller congruent triangles, removing the central one, and then recursively
repeating the process on each of the remaining smaller triangles. This creates the well-known
Sierpinski gasket structure with a self-similar, triangular appearance.
Algorithm:
1. Input the coordinates of a triangle: (x1,y1),(x2,y2),(x3,y3)(x1, y1), (x2, y2), (x3,
y3)(x1,y1),(x2,y2),(x3,y3).
2. If recursion depth = 0, then draw the triangle directly.
3. Otherwise:
4. Find the midpoints of each side:
• M1 between (x1,y1)(x1, y1)(x1,y1) and (x2,y2)(x2, y2)(x2,y2)
• M2 between (x2,y2)(x2, y2)(x2,y2) and (x3,y3)(x3, y3)(x3,y3)
• M3 between (x1,y1)(x1, y1)(x1,y1) and (x3,y3)(x3, y3)(x3,y3)
5. Connect these midpoints, which divides the triangle into 4 smaller triangles.
6. Remove the central triangle.
7. Apply recursion on the remaining 3 corner triangles with depth – 1.
8. Stop when depth reaches 0.
Source code:
#include<bits/stdc++.h>
#include<windows.h>
#include<GL/glut.h>
#define PI acos(-1)
using namespace std;
int n;
void draw_triangle( float x1, float y1, float x2, float y2, float x3, float y3 ) {
glBegin(GL_TRIANGLES);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glVertex2f(x3, y3);
glEnd();
14
void gasket( float x1, float y1, float x2, float y2, float x3, float y3, int n ) {
if ( n > 0 ) {
float x12 = (x1 + x2) / 2;
float y12 = (y1 + y2) / 2;
float x23 = (x2 + x3) / 2;
float y23 = (y2 + y3) / 2;
float x13 = (x1 + x3) / 2;
float y13 = (y1 + y3) / 2;
gasket( x1, y1, x12, y12, x13, y13, n-1 );
gasket( x2, y2, x12, y12, x23, y23, n-1 );
gasket( x3, y3, x13, y13, x23, y23, n-1 );
} else {
draw_triangle( x1, y1, x2, y2, x3, y3 );
void display() {
glClear(GL_COLOR_BUFFER_BIT);
gasket(10, 10, 90, 10, 50, 90, n);
glFlush();
void init() {
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0, 0, 0, 0);
glColor3f(1,1,1);
15
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 100, 0, 100);
int main(int argc, char** argv) {
cout <<"Enter n: ";
cin>> n;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 800);
glutCreateWindow("Sierpinski Gasket");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
Input & Output:
16
17
Discussion: I implemented a Sierpinski Gasket using OpenGL and recursion. My program
takes an integer n for recursion depth. I wrote draw_triangle() to draw basic triangles and
gasket() to recursively subdivide them, removing the center. Used display() and init(), I have
given the fractal in an 800×800 GLUT window, demonstrating how recursion creates a self-
similar pattern.