0% found this document useful (0 votes)
5 views33 pages

Data Science

This document is a lab report from the Computer Graphics Laboratory at Manipur Technical University, detailing various experiments conducted using algorithms for line and shape drawing in computer graphics. It includes a list of experiments, program codes, and outputs for algorithms such as DDA and Bresenham’s Line Drawing, as well as implementations using OpenGL. The report is submitted by a student named Deemson Pukhrambam and includes specific dates for each experiment conducted.

Uploaded by

elangbamhitler6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views33 pages

Data Science

This document is a lab report from the Computer Graphics Laboratory at Manipur Technical University, detailing various experiments conducted using algorithms for line and shape drawing in computer graphics. It includes a list of experiments, program codes, and outputs for algorithms such as DDA and Bresenham’s Line Drawing, as well as implementations using OpenGL. The report is submitted by a student named Deemson Pukhrambam and includes specific dates for each experiment conducted.

Uploaded by

elangbamhitler6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Manipur Technical University

Department of Computer Science and Engineering


Takyelpat Imphal West 795003

Computer Graphics Laboratory


(CS3644)

Lab Report

Submitted to
Ingudam Gomita

Submitted by
Name : Deemson Pukhrambam
Reg No. : 2201CS0113
List of Experiments

Sl.No Expt. Experiment Name Date Date of Remarks


. No. of Submis-
Expt. sion

1 1 DDA Line Drawing 20-03-2025


Algorithm
2 2 DDA Line Drawing 20-03-2025
Algorithm using
OpenGL
3 3 Bresenham’s Line 27-03-2025
Drawing
Algorithm
4 4 Drawing a 27-03-2025
Red Square
using
OpenGL
5 5 Mid Point Circle 08-05-2025
Drawing
Algorithm
6 6 Drawing a 15-05-2025
Circle using
OpenGL
7 7 2D 15-05-2025
Transformations on
a Line using
OpenGL
8 8 Generating a 3D 15-05-2025
Sierpinski Gasket

1
Contents
1 Experiment 1:
DDA Line Drawing Algorithm 4
1.1 Aim ....................................... 4
1.2 Program Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

2 Experiment 2:
DDA Line Drawing Algorithm using OpenGL 7
2.1 Aim ....................................... 7
2.2 Program Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

3 Experiment 3:
Bresenham’s Line Drawing Algorithm 9
3.1 Aim ....................................... 9
3.2 Program Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.3 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

4 Experiment 4:
Drawing a Red Square using OpenGL 13
4.1 Aim ....................................... 13
4.2 Program Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.3 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

5 Experiment 5:
Mid Point Circle Drawing Algorithm 15
5.1 Aim ....................................... 15
5.2 Program Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5.3 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

6 Experiment 6:
Drawing a Circle using OpenGL 19
6.1 Aim ....................................... 19
6.2 Program Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
6.3 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

7 Experiment 7:
2D Transformations on a Line using OpenGL 22
7.1 Aim ....................................... 22
7.2 Program Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
7.3 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

2
8 Experiment 8:
Generating a 3D Sierpinski Gasket 25
8.1 Aim ....................................... 25
8.2 Program Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
8.3 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

List of Figures
1 Terminal output of DDA Line Drawing Algorithm . . . . . . . . . . . . . . 6
2 Graphical output of DDA Line Drawing Algorithm . . . . . . . . . . . . . 6
3 Output of DDA Line Drawing Algorithm using OpenGL . . . . . . . . . . 8
4 Terminal output of Bresenham’s Line Drawing Algorithm . . . . . . . . . . 11
5 Graphical output of Bresenham’s Line Drawing ......... 12
6 Using OpenGL to draw a red square ..................... 14
7 Graphical output of Midpoint Circle Drawing Algorithm . . . . . . . . . . 18
8 Graphical output of drawing a red circle with OpenGL . . . . . . . . . . . 21
9 Line Transformation: Example 1 (e.g., Initial state or after some 24
10 Line Transformation: Example 2 (e.g., After ............ 24
11 Line Transformation: Example 3 (e.g., After translation) . . . . . . . . . . 24
12 Graphical output showing the generated 3D Sierpinski Gasket . . . . . . . 27

3
1 Experiment 1:
DDA Line Drawing
Algorithm
1.1 Aim
To implement the Digital Differential Analyzer (DDA) algorithm to draw a
straight line between two points.

1.2 Program Code


The following C program implements the DDA Line Drawing Algorithm:
1 #include <stdio.h>
2 #include <math.h>
3 #include <GL/glut.h>
4 double X1 ,Y1 ,X2 ,Y2;
5

6 float round_value(float v){


7 return floor(v+0.5);
8 }
9

10 void Line_DDA(void){
11 double dx=(X2 -X1);
double dy=(Y2 -Y1);
12
13 double steps;
float xlnc ,ylnc ,x=X1 ,y=Y1;
14 steps = (abs(dx)>abs(dy))?(abs(dx)):(abs(dy)); xlnc =
dx/( float)steps;
15 ylnc = dy/( float)steps;
glClear( GL_COLOR_BUFFER_BIT );
16 glBegin(GL_POINTS);
20 glVertex2d(x,y);
int k;
21
22 for(k=0;k<steps;k++){
x+= xlnc;
23 y+= ylnc;
glVertex2d(round_value(x),round_value(y)); }
24 glEnd ();
glFlush ();
25

29 }
30

31 void Init (){


32 glClearColor (1.0 ,1.0 ,1.0 ,0.0);
33 glColor3f (0.0 ,0.0 ,0.0);
34 glViewport (0 ,0 ,640 ,480);
35 glMatrixMode(GL_PROJECTION);
36 glLoadIdentity ();
37 gluOrtho2D (0 ,640 ,0 ,480);
38 }
39

40 int main(int argc ,char ** argv){


41 printf("Enter two end points of the line to be drawn :\n");

4
42 printf("\n ********************************************* ");
43 printf("\nEnter Point1(x1 ,y1):\n");
scanf("%lf%lf" ,&X1 ,&Y1);
44
45 printf("\n ********************************************* ");
printf("\nEnter point2(x2 ,y2);\n");
46 scanf("%lf%lf" ,&X2 ,&Y2); // Changed X1 ,Y1 to X2 ,Y2
glutInit (&argc ,argv);
47 glutInitWindowSize (640 ,480);
glutInitWindowPosition (0,0);
48 glutInitDisplayMode (GLUT_RGB|GLUT_SINGLE);
52 glutCreateWindow ("Line"); Init ();

53
54 glutDisplayFunc (Line_DDA);
55 glutMainLoop ();
56 }

Listing 1: DDA Line Drawing Algorithm Code

5
1.3 Output
The program was compiled and executed. The output is shown in Figure 1 and
Figure 2.

Figure 1: Terminal output of DDA Line Drawing Algorithm

Figure 2: Graphical output of DDA Line Drawing Algorithm

6
2 Experiment 2:
DDA Line Drawing Algorithm using
OpenGL
2.1 Aim
To implement the Digital Differential Analyzer (DDA) algorithm to draw a
straight line between two points using OpenGL.

2.2 Program Code


The following C program implements the DDA Line Drawing Algorithm using
OpenGL:
1 #include <GL/gl.h>
2 #include <GL/glut.h>
3 void display (){
4 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
5 glLineWidth (2.5);
6 glColor3f (1.0 ,0.0 ,0.0);
7 glBegin(GL_LINES);
8 glVertex3f (0.0 ,0.0 ,0.0);
9 glVertex3f (1 ,1 ,0.0);
10 glEnd ();
11 glFlush ();
12 }
13

14 void init(void){
15 glClearColor (1.0 ,1.0 ,1.0 ,1.0);
16 glMatrixMode(GL_PROJECTION);
17 glLoadIdentity ();
18 glOrtho (0.0 ,1.0 ,0.0 ,1.0 , -1.0 ,1.0);
19 }
20

21 int main(int argc ,char ** argv){


22 glutInit (&argc ,argv);
23 glutInitWindowSize (500 ,500);
24 glutInitWindowPosition (100 ,100);
25 glutInitDisplayMode (GLUT_RGB|GLUT_SINGLE);
26 glutCreateWindow ("Line");
27 init ();
28 glutDisplayFunc (display);
29 glutMainLoop ();
30 return 0;
31 }

Listing 2: DDA Line Drawing with OpenGL Code

7
2.3 Output
The program was compiled and executed. The output is shown in Figure 3.

Figure 3: Output of DDA Line Drawing Algorithm using OpenGL

8
3 Experiment 3:
Bresenham’s Line Drawing
Algorithm
3.1 Aim
To implement the Bresenham’s algorithm to draw a straight line between two
points.

3.2 Program Code


The following C program implements Bresenham’s Line Drawing Algorithm:
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <GL/gl.h>
4 #include <GL/glut.h>
5

6 // Changed variable names to avoid potential conflicts


7 int start_x , start_y , end_x , end_y;
8

9 void myInit () {
10 glClear( GL_COLOR_BUFFER_BIT );
11 glClearColor (0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
1213 gluOrtho2D (0, 500, 0, 500);
14 }
15

16 void draw_pixel(int x, int y)


17 { glBegin(GL_POINTS);
glVertex2i(x, y);
18 glEnd ();
20 }
21

22 void draw_line(int x1 , int y1 , int x2 , int y2) { int dx , dy , i, e;


23 int incx , incy , inc1 , inc2;
int x, y;
24

26
27 dx = abs(x2 - x1);
dy = abs(y2 - y1);
2829

30 incx = (x2 >= x1) ? 1 : -1;


31 incy = (y2 >= y1) ? 1 : -1;
32
33 x = x1;
y = y1;
3435

36 if (dx > dy) {


draw_pixel(x, y);
37 e = 2 * dy - dx;
inc1 = 2 * (dy - dx);
38
40 inc2 = 2 * dy;
41
42 for (i = 0; i < dx; i++) {
9
43 if (e >= 0) {
44 y += incy;
45 e += inc1;
46 } else {
47 e += inc2;
48 }
49 x += incx;
50 draw_pixel(x, y);
51 }
52 } else {
53 draw_pixel(x, y);
54 e = 2 * dx - dy;
55 inc1 = 2 * (dx - dy);
56 inc2 = 2 * dx;
57
58 for (i = 0; i < dy; i++) { if
(e >= 0) {
59 x += incx;
e += inc1;
60 } else {
e += inc2;
61
64 }
65 y += incy;
66 draw_pixel(x, y);
67 }
68 }
69 }
70

71 void myDisplay () {
72 draw_line(start_x , start_y , end_x , end_y);
73 glFlush ();
74 }
75

76 int main(int argc , char ** argv) {


77 printf("Enter (x1 , y1 , x2 , y2): ");
78 scanf("%d %d %d %d", &start_x , &start_y , &end_x , &end_y);
79
80 glutInit (&argc , argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
81
82 glutInitWindowSize (500, 500);
glutInitWindowPosition (0, 0);
83
84 glutCreateWindow ("Bresenham ’s Line Drawing Algorithm");
85
86 myInit ();
glutDisplayFunc (myDisplay);
87 glutMainLoop ();
89

90 return 0;
91 }

Listing 3: Bresenham’s Line Drawing Algorithm Code

10
3.3 Output
The program was compiled and executed. The output is shown in Figure 4 and
Figure 5.

Figure 4: Terminal output of Bresenham’s Line Drawing Algorithm

11
Figure 5: Graphical output of Bresenham’s Line Drawing Algorithm

12
4 Experiment 4:
Drawing a Red Square using
OpenGL
4.1 Aim
To implement a C++ program using OpenGL to draw a red square.

4.2 Program Code


The following C++ program uses OpenGL to draw a red square:
1 #include <GL/glut.h>
using namespace std;
2
3 void init(void){

4 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );


5 glClearColor (1,1,1,0);
6 glMatrixMode(GL_PROJECTION);
7 glLoadIdentity ();
8 glOrtho ( -120.0 ,120.0 , -120.0 ,120.0 , -120.0 ,120.0);
9 glMatrixMode(GL_MODELVIEW);
10 }
11

12 void display (){


13 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glBegin(GL_POLYGON);
14 glColor3f (1.0 ,0.0 ,0.0);
glVertex2f (0.0 ,0.0);
15 glVertex2f (0 ,60.0);
glVertex2f (60.0 ,60.0);
16 glVertex2f (60.0 ,0.0);
20 glEnd ();
21 glFlush ();
22 }
23

24 int main(int argc , char ** agrv){


25 glutInit (&argc , agrv);
26 glutInitDisplayMode (GLUT_RGB|GLUT_SINGLE);
27 glutInitWindowPosition (100 ,100);
28 glutInitWindowSize (500 ,500);
29 glutCreateWindow ("Square");
30 init ();
31 glutDisplayFunc (display);
32 glutMainLoop ();
33 return 0;
34 }

Listing 4: OpenGL Red Square Drawing Code

13
4.3 Output
The program was compiled and executed. The output is shown in Figure 6.

Figure 6: Using OpenGL to draw a red square

14
5 Experiment 5:
Mid Point Circle Drawing
Algorithm
5.1 Aim
To implement the Midpoint Circle Drawing Algorithm to plot the points of a circle.

5.2 Program Code


The following C++ program implements the Midpoint Circle Drawing Algorithm:
1 // 11. Mid Point Line:
2

3 #include <GL/glut.h>
4 #include <iostream >
5 #include <cstdlib >

6 #include <ctime >


7

8 using namespace std;


9

10 float wid = 400;


11 float height = wid;
12 int numbers = 20;
13 float t = wid / numbers;
14

15 int x1 = -300, y1 = -400, x2 = 400, y2 = 100;


16

17 voiddraw_point(float x, float y, int k_kind , int d_kind); 18 float


translater(int x);
19 void swap(int &a, int &b) {
20 int tmp = b;
b = a;
2122 a = tmp;
23 }
24

25 void bresenham(int x1 , int y1 , int x2 , int y2) { int k_kind = 0;


26 int d_kind = 0;
27

28
29 if (x1 > x2) {
swap(x1 , x2);
30
31 swap(y1 , y2);
32 }
33
34 int dx = abs(x2 - x1), dy = abs(y2 - y1);
35
36 if (y1 > y2) {
y1 = -y1;
37 y2 = -y2;
d_kind = 1;
38
40 }
41
42 if (dy > dx) {
15
43 swap(x1 , y1);
44 swap(x2 , y2);
45 swap(dx , dy);
46 k_kind = 1;
47 }
48
49 float d = (dy + dy - dx) * t;
float x = x1 + 0.0, y = y1 + 0.0;
5051

52 draw_point(translater(x), translater(y), k_kind , d_kind);


53
54 while (x < x2) {
55 if (d < 0) {
56 d += 2 * dy * t;
57 } else {
58 d += 2 * (dy - dx) * t;
59 y += t;
60 }
61 x += t;
62 draw_point(translater(x), translater(y), k_kind , d_kind);
63 }
64 }
65

66 float translater(int x) {
67 return x / wid;
68 }
69

70 void draw_point(float x, float y, int k_kind , int d_kind) { glPointSize (7);


71 glColor3f (0.0, 0.0, 0.1);
72

73 glBegin(GL_POINTS);
74 cout << "k:" << k_kind << " d:" << d_kind << endl;
75
76 if (k_kind == 0 && d_kind == 1) {
y = -y;
77 } else if (k_kind == 1 && d_kind == 1) { x = -x;
swap(x, y);
78 } else if (k_kind == 1 && d_kind == 0) {

79
82 swap(x, y);
83 }
84

85 glVertex3f(x, y, 0.0);
86 glEnd ();
87 glFlush ();
88 }
89

90 void grid () {
91 glClearColor (0, 0, 0, 0);
92 glClear( GL_COLOR_BUFFER_BIT );
93
94 int wid_number = numbers;
95 int hei_number = numbers;
float delta_wid = wid / wid_number;
96

16
97 float delta_hei = height / hei_number;
98
99 glColor3f (1.0, 1.0, 0.0);
100 for (int i = 1; i < 40; i++) {
glBegin(GL_LINES);
101 glVertex2f (-1 + i * delta_hei / height , -1);
glVertex2f (-1 + i * delta_hei / height , 1); glVertex2f (-1, -1 + i
102 * delta_hei / height); glVertex2f (1, -1 + i * delta_hei / height);
glEnd ();
103

107 glFlush ();


108 }
109
110 glColor3f (1.0, 0.0, 0.0);
glBegin(GL_LINES);
111 glVertex2f (-1, 0);
glVertex2f (1, 0);
112 glVertex2f (0, -1);
glVertex2f (0, 1);
113 glEnd ();
glFlush ();
114
118
119 glBegin(GL_LINES);
120 glColor3f (1.0, 0.0, 0.0);
glVertex2f(translater(x1), translater(y1));
121 glVertex2f(translater(x2), translater(y2)); glEnd ();
glFlush ();
122

125

126 bresenham(x1 , y1 , x2 , y2);


127 }
128

129 int main(int argc , char *argv []) {


130 glutInit (&argc , argv);
131
132 glutInitWindowSize (700, 700);
glutInitWindowPosition (300, 200);
133 glutInitDisplayMode (GLUT_RGBA);
glutCreateWindow ("Mid Point Line");
134
136 glutDisplayFunc (grid);
glutMainLoop ();
137
138

139 return 0;
140 }

Listing 5: Midpoint Circle Drawing Algorithm Code

17
5.3 Output
The program was compiled and executed. The output is shown in Figure 7.

Figure 7: Graphical output of Midpoint Circle Drawing Algorithm

18
6 Experiment 6:
Drawing a Circle using
OpenGL
6.1 Aim
To implement a C++ program using OpenGL to draw a circle, colored red.

6.2 Program Code


The following C++ program uses OpenGL to draw a red circle by plotting vertices
cal-culated along its circumference:
// 4. OpenGL
1 Circle:
2 #include <GL/glut.h>
3 #include <math.h>
4

5 #define PI 3.14
6

7 using namespace std;


8

void init(void)
9
10 {
11 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glClearColor (1, 1, 1, 0);
12
13 glMatrixMode(GL_PROJECTION);
14 glLoadIdentity ();
15 glOrtho ( -120.0 , 120.0 , -120.0, 120.0 , -120.0, 120.0);
16 glMatrixMode(GL_MODELVIEW);
17 }
18

19 void CIRCLE ()
20 {
21 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
22 glLineWidth (2.0);
glBegin(GL_LINE_LOOP);
23 glColor3f (1, 0, 0);
25
26 GLint r = 30;
GLdouble i = 0.0;
2728

29 for (i = 0; i <= 2 * PI; i += PI / 180) {

30
31 glVertex2d(r * cos(i), r * sin(i));
32 }
33

34 glEnd ();
35 glFlush ();
36 }
37

38 int main(int argc , char ** argv)


39 {
40 glutInit (&argc , argv);
glutInitDisplayMode (GLUT_RGB | GLUT_SINGLE);
41
19
42 glutInitWindowPosition (100, 100);
43 glutInitWindowSize (500, 500);
glutCreateWindow ("CIRCLE");
44
45 init ();
46 glutDisplayFunc (CIRCLE);
47 glutMainLoop ();
48 return 0;
49 }

Listing 6: OpenGL Red Circle Drawing Code

20
6.3 Output
The program was compiled and executed. The output is shown in Figure 8.

Figure 8: Graphical output of drawing a red circle with OpenGL

21
7 Experiment 7:
2D Transformations on a Line using
OpenGL
7.1 Aim
To implement a C++ program using OpenGL to perform 2D transformations
(translation, rotation, and scaling) on a line segment. The transformations are
controlled via keyboard inputs.

7.2 Program Code


The following C++ program implements 2D transformations on a line segment
using OpenGL:
1 #include <stdio.h>
2 #include <stdarg.h>
3 #include <math.h>
4 #define GL_GLEXT_PROTOTYPES
5 #include <GL/glut.h>
6

7 void 8 display ();


void specialKeys ();
9

10 double rotate_y = 0.1;


11 double rotate_x = 0.1;
12 double scale = 2.5;
13 double translate = 0.0;
14

15 void display ()
16 {
17 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
18 glLoadIdentity ();
19
20 glTranslatef(translate , 0.0, 0.0);
glRotatef(rotate_x , 1.0, 0.0, 0.0);
21 glRotatef(rotate_y , 0.0, 1.0, 0.0);
23 glScalef(scale , scale , 0.0);
24
25 glLineWidth (2.5);
glBegin(GL_LINES);
26 glVertex3f (0.0, 0.0, 0.0);
glVertex3f (1.0, 1.0, 0.0); glEnd ();
27

30

31 glFlush ();
32 glutSwapBuffers ();
33 }
34

35 void specialKeys(int key , int x, int y)


36 {
37 if (key == GLUT_KEY_RIGHT) {
38 rotate_y += 5.0;

22
39 }
40 else if (key == GLUT_KEY_LEFT) {
rotate_y -= 5.0;
41
42 }
else if (key == GLUT_KEY_UP) { rotate_x
43 += 5.0;
}
44 else if (key == GLUT_KEY_DOWN) {
rotate_x -= 5.0;
45 }
49 else if (key == GLUT_KEY_F1) { scale
+= 0.1;
50
51 }
else if (key == GLUT_KEY_F2) {
52 scale -= 0.1;
}
53 else if (key == GLUT_KEY_F3) {
translate += 0.1;
54 }
else if (key == GLUT_KEY_F4) {
55 translate -= 0.1;
60 }
61
62 glutPostRedisplay ();
63 }
64

65 int main(int argc , char* argv [])


66 {
67 glutInit (&argc , argv);
68 glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
69 glutCreateWindow ("Lines and Transformations ");
70
71 glEnable(GL_DEPTH_TEST);
glutDisplayFunc (display);
72 glutSpecialFunc (specialKeys);
glutMainLoop ();
7375

76 return 0;
77 }

Listing 7: 2D Line Transformations with OpenGL Code

23
7.3 Output
The program was compiled and executed. Different transformations applied to
the line
are shown in Figure 9, Figure 10, and Figure 11.

Figure 9: Line Transformation: Example 1 (e.g., Initial state or after some


rotations)

Figure 10: Line Transformation: Example 2 (e.g., After scaling)

Figure 11: Line Transformation: Example 3 (e.g., After translation)


24
8 Experiment 8:
Generating a 3D Sierpinski
Gasket
8.1 Aim
To implement a C++ program using OpenGL to generate and display a 3D
Sierpinski Gasket by iteratively plotting midpoints.

8.2 Program Code


The following C++ program implements the algorithm to generate a 3D Sierpinski Gasket:
1 //8. Gasket 3D:-
2

3 #include <GL/glut.h>
4

5 void display(void)
6 {
7 GLfloat vertices
[4][3]={{ -50.0 , -50.0 , -50.0} ,{0.0 ,50.0 ,10.0} ,{60.0 , -10.0 ,0.0} ,{50.0 , -50.0 ,0.0}}

8 int i,j,k;
9 int rand ();
10 GLfloat p[3]={25.0 ,10.0 ,25.0};
11
12 glClear( GL_COLOR_BUFFER_BIT );
glBegin(GL_POINTS);
13 for(k=0;k <20000;k++)
{
14 j=rand ()%4;
p[0]=(p[0]+ vertices[j][0]) /2.0;
15
18 p[1]=(p[1]+ vertices[j][1]) /2.0;
19 p[2]=(p[2]+ vertices[j][2]) /2.0;
20 glColor3f(p[0]/250.0 ,p[1]/250.0 ,p[2]/250.0);
21 glVertex3fv(p);
22 }
23 glEnd ();
24 glFlush ();
25 }
26
27

28 void myinit ()
29 {
30 glClearColor (1.0 ,1.0 ,1.0 ,1.0); glColor3f
31 (0.0 ,0.0 ,0.0);
32

33 glMatrixMode(GL_PROJECTION);
34 glLoadIdentity ();
35 glOrtho ( -100.0 ,100.0 , -100.0 ,100.0 , -100.0 ,100.0);
36 glMatrixMode(GL_MODELVIEW);
37 }
38

39 int main(int argc ,char ** argv)


25
40 {
41 glutInit (&argc ,argv);
glutInitDisplayMode (GLUT_SINGLE|GLUT_RGB);
42
43 glutInitWindowSize (500 ,500);
44 glutInitWindowPosition (0,0);
45 glutCreateWindow ("Sierpinsky Gasket 3D");
46 glutDisplayFunc (display);
47 myinit ();
48 glutMainLoop ();
49 return 0;
50 }

Listing 8: 3D Sierpinski Gasket Generation Code

26
8.3 Output
The program was compiled and executed. The output is shown in Figure 12.

Figure 12: Graphical output showing the generated 3D Sierpinski


Gasket

27

You might also like