You are on page 1of 14

1

Bandarban University
Department of Computer Science and Engineering
Course Name: Computer Graphics
Lab Report-1
Submission: 16/10/2022

Submitted to:
Mr. Sultan Mahmud
Lecturer (Adjunct)
Department of Computer Science and Engineering
Bandarban University

Submitted by:
Uhai Mong Marma
ID: 15032004
Semester: 6th
Department of Computer Science and Engineering
Bandarban University
2

Table of Context
List of Problem: Page:
1. Write a program to generate Bresenham’s Line Draw 3
2. A program to generate of the Digital Differential Analyzer(DDA) Line Draw 5
3. Write a program to generate of the Midpoint Circle Draw 7
4. Write a program to generate of Translating an Object 9

5. Mini-project: Drawing a School 11


3

1. Problem title: Write a program to generate Bresenham’s Line Draw


Objective: To generate basic line according to Bresenham’s Line drawing Algorithm.

Algorithm:
1. Input two line endpoint.
2. Calculate the constants dx = x2-x1,dy = y2-y1,i1 = 2*dy,i2= 2*(dy-dx), d = i1 –dx.

Program:
1. #include <iostream>
2. #include<graphics.h>
3. #include<stdio.h>
4. #include<conio.h>
5.
6. void drawline(int x1,int y1, int x2, int y2)
7. {
8. int dx,dy,p,x,y;
9.
10. dx = x2-x1;
11. dy = y2-y1;
12.
13. x = x1;
14. y = y1;
15.
16. p = 2*(dy-dx);
17.
18. while(x<x2)
19. {
20. if(p>=0)
21. {
22. putpixel(x,y,7);
23. y = y +1;
24. p = p + 2 * dy -2 * dx;
25. }
26. else
27. {
28. putpixel(x,y,7);
29. p = p + 2 * dy;
30. }
31. x = x + 1;
32. }
33. }
4

34.
35. main()
36. {
37. int gd = DETECT, gm, error,x1,y1,x2,y2;
38. initgraph(&gd,&gm,"");
39.
40. printf("Please Enter the first Co-ordinates point: \n");
41. scanf("%d%d",&x1,&y1);
42.
43. printf("Please Enter the second Co-ordinates point: \n");
44. scanf("%d%d",&x2,&y2);
45.
46. drawline(x1,y1,x2,y2);
47.
48. getch();
49.
50.
51. }
Outpute:

Result: The Bresenham’s Line drawing is successful. The output picture is generated.
5

2. Problem title: A program to generate of the Digital Differential Analyzer(DDA) Line


Draw.
Objective: To generate basic line according to Digital Differential Analyzer(DDA) Line
Drawing Algorithm.
Algorithm:
1. Input two line endpoints and store the left endpoint in (x1,y1).
2. Set the color for frame-buffer position (x1,y1).
3. Calculate the constant x,y,2y and 2y – 2x, and If x = 1 the next point to plot is yk+1 =
yk +m and If y = 1 the next point to plot is xk+1 = xk + 1/m
4. At each xk along the line, starting a k = 0, perform the following test: no (3)
5. Repeats step 4 (x-1) more times.
Program:
1. #include<graphics.h>
2. #include<stdio.h>
3. #include<math.h>
4. #include<dos.h>
5. #include<conio.h>
6.
7. main()
8. {
9. float x,y,x1,y1,x2,y2,dx,dy,step;
10. int i,gd = DETECT, gm;
11.
12. initgraph(&gd,&gm,"");
13.
14. printf("Please Enter the first Co-ordinate x1 and y1: \n");
15. scanf("%f%f",&x1,&y1);
16.
17. printf("Please Enter the second Co-ordinate x2 and y2: \n");
18. scanf("%f%f",&x2,&y2);
19.
20. dx = abs(x2-x1);
21. dy = abs(y2-y1);
22.
23. if(dx>=dy)
24. {
25. step = dx;
26. }
27. else
28. {
29. step = dy;
30. }
6

31.
32. dx = dx/step;
33. dy = dy/step;
34.
35. x = x1;
36. y = y1;
37.
38. i = 1;
39. while(i<=step)
40. {
41. putpixel(x,y,5);
42. x = x+dx;
43. y = y+dy;
44. i = i+1;
45. }
46. getch();
47. }
Output:

Result: The DDA Line drawing is successful. The output picture is generated.
7

3. Problem title: Write a program to generate of the Midpoint Circle Draw.


Objective: To generate basic circle according to Midpoint Circle Drawing Algorithm.
Algorithm:
1. Input radius r and circle center, then set the coordinates for the first point on the
circumference of a circle centered on the origin as (x0,y0) = (0,r)
2. Calculate the initial value of the decision parameter as p0 = (5/4) – r.
3. At each xk position, starting at k = 0, perform test: If pk <0, the next point along the
circle centered on (0,0) is (xk+1,yk) and pk+1 = pk + 2xk+1. Otherwise, the next
point along the circle is (x k+1 , yk – 1) and pk+1 =pk+2x k+1+1 – 2y k+1 when 2x k+1
=2xk +2 and 2y k+1 = 2yk -2;
4. Determine symmetry points in the other seven octants.
5. Repeat steps 3 through 5 until x>=y.
Program:
1. #include<stdio.h>
2. #include<graphics.h>
3. #include<conio.h>
4.
5. void drawcircle(int x0, int y0, int radius)
6. {
7. int x = radius;
8. int y = 0;
9. int err = 0;
10.
11. while(x>=y)
12. {
13. putpixel(x0+x,y0+y,4);
14. putpixel(x0+y,y0+x,4);
15.
16. putpixel(x0-y, y0+x,4);
17. putpixel(x0-x, y0+y,4);
18.
19. putpixel(x0-x, y0-y,4);
20. putpixel(x0-y, y0-x,4);
21.
22. putpixel(x0+y,y0-x,4);
23. putpixel(x0+x,y0-y,4);
24.
25. if(err<=0)
26. {
27. y+=1;
28. err+= 2*y+1;
29. }
8

30. if(err>0)
31. {
32. x-=1;
33. err-=2*x+1;
34. }
35. }
36. }
37.
38. main()
39. {
40. int gd = DETECT,gm,error,x,y,r;
41. initgraph(&gd,&gm,"");
42.
43. printf("PLease Enter the radius of Circle: \n");
44. scanf("%d",&r);
45.
46. printf("Please Enter the co-ordinate of center x and y: \n");
47. scanf("%d%d",&x,&y);
48. drawcircle(x,y,r);
49.
50. getch();
51. }
Output:

Result: The Midpoint circle drawing is successful. The output picture is generated.
9

4. Problem title: Write a program to generate Translating an object.


Objective: To reposition an object it along a straight line path from on coordinate
location to another coordinate according to Translation Algorithm.
Algorithm:
1. Start
2. Initialize the graphics mode
3. Construct a 2D object.
4. Get translation value tx,ty
5. Move the 2D object with tx,ty(x = x+tx,y = y+ty)
6. Plot(x,y)
Program:
1. #include<stdio.h>
2. #include<conio.h>
3. #include<graphics.h>
4.
5. void translateLine(int P[][2],int T[])
6. {
7. int gd =DETECT,gm,errorcode;
8. initgraph(&gd,&gm,"");
9. line(getmaxx()/2, 0, getmaxx()/2, getmaxy());
10. line(0, getmaxy()/2, getmaxx(), getmaxy()/2);
11.
12. line(P[0][0],P[0][1], P[1][0], P[1][1]);
13.
14. P[0][0] = P[0][0]+T[0];
15. P[0][1] = P[0][1]+T[1];
16. P[1][0] = P[1][0]+T[0];
17. P[1][1] = P[1][1]+T[1];
18. setcolor(3);
19. line(P[0][0],P[0][1],P[1][0],P[1][1]);
20. }
21.
22. main()
23. {
24. int P[2][2] = {160,70,30,160}; // co-ordinate point
25. int T[] = {10,20}; // Translation factor
26. translateLine(P,T);
27. delay(5000);
28. getch();
29. }
10

Output:

Result: The Translating an Object is successful. The output picture is generated.


11

5. Mini-project: Drawing a School


Objective: To learn how to use and draw lines, circles, rectangles, arcs, fill color and
axis position together.
Program:
1. #include<iostream>
2. #include<stdio.h>
3. #include<conio.h>
4. #include<graphics.h>
5. main()
6. {
7. int gd,gm;
8. detectgraph(&gd,&gm);
9. initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
10.
11. //drawing a hill
12. setcolor(LIGHTBLUE);
13. line(0,80,700,80);
14.
15. line(0,80,50,50);
16. line(50,50,100,80);
17.
18. line(100,80,150,50);
19. line(150,50,200,80);
20.
21. line(200,80,250,50);
22. line(250,50,300,80);
23.
24. line(300,80,350,50);
25. line(350,50,400,80);
26.
27. line(400,80,450,50);
28. line(450,50,500,80);
29.
30. line(500,80,550,50);
31. line(550,50,600,80);
32.
33. line(600,80,650,50);
34. line(650,50,700,80);
35.
36. //drawing a sun
37. setcolor(YELLOW);
38. setfillstyle(SOLID_FILL,YELLOW);
39. arc(200,80,30,150,50);
12

40. //floodfill(199,79,YELLOW);
41.
42. //drawing the school.
43.
44. line(50,150,500,150);
45. line(0,200,50,150);
46. line(50,150,100,200);
47. line(500,150,550,200);
48. line(0,200,550,200);
49. line(60,160,20,200);
50. //body
51. rectangle(20,200,530,270);
52. line(100,200,100,290);
53. //door
54. rectangle(180,210,220,270);
55. //under part
56. setcolor(DARKGRAY);
57. setfillstyle(LTSLASH_FILL,DARKGRAY);
58. rectangle(20,270,530,290);
59. floodfill(21,271,DARKGRAY);
60. //window
61. setcolor(CYAN);
62. setfillstyle(LTBKSLASH_FILL,CYAN);
63. rectangle(270,220,300,250);
64. floodfill(271,221,CYAN);
65.
66. setcolor(CYAN);
67. setfillstyle(LTBKSLASH_FILL,CYAN);
68. rectangle(350,220,380,250);
69. floodfill(351,221,CYAN);
70.
71. setcolor(CYAN);
72. setfillstyle(LTBKSLASH_FILL,CYAN);
73. rectangle(430,220,460,250);
74. floodfill(431,221,CYAN);
75.
76. //sign board
77. rectangle(30,220,90,250);
78. //text
79. outtextxy(40,230,"School");
80.
81. //flag pillar
82. rectangle(110,100,120,340);
13

83. rectangle(100,340,130,350);
84. rectangle(90,350,140,360);
85.
86. //flag
87. setcolor(GREEN);
88. setfillstyle(SOLID_FILL,GREEN);
89. rectangle(120,100,200,140);
90. floodfill(121,101,GREEN);
91. setcolor(RED);
92. setfillstyle(SOLID_FILL,RED);
93. circle(160,120,15);
94. floodfill(161,121,RED);
95.
96. //School road left side
97. setcolor(LIGHTGRAY);
98. line(180,300,180,340);
99. line(180,340,140,380);
100. line(140,380,60,390);
101.
102. //School road right side
103. line(220,300,220,340);
104. line(220,340,180,390);
105. line(180,390,80,450);
106.
107. //Grass
108. setcolor(GREEN);
109. setfillstyle(SOLID_FILL,GREEN);
110. line(300,350,270,400);
111. line(300,350,330,400);
112. line(270,400,330,400);
113. floodfill(290,390,GREEN);
114.
115. setcolor(YELLOW);
116. setfillstyle(SOLID_FILL,YELLOW);
117. rectangle(290,400,310,450);
118. floodfill(291,401,YELLOW);
119.
120. //Text
121. setcolor(CYAN);
122. outtextxy(100,460,"Art by Uhai");
123. getch();
124. }
14

Output:

You might also like