100% found this document useful (1 vote)
474 views

Computer Graphics Lab Assignment

The document contains the answers to 5 questions regarding computer graphics programming. It includes C++ code to draw pixels and basic shapes using graphics functions, implement DDA and Bresenham's line drawing algorithms, and Bresenham's circle drawing algorithm. The code samples output basic graphics shapes to the screen.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
474 views

Computer Graphics Lab Assignment

The document contains the answers to 5 questions regarding computer graphics programming. It includes C++ code to draw pixels and basic shapes using graphics functions, implement DDA and Bresenham's line drawing algorithms, and Bresenham's circle drawing algorithm. The code samples output basic graphics shapes to the screen.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

National Institute of Technology, Raipur

Computer Graphics Lab Assignment

Submitted By:
Name: Anirudha Shivarkar
Branch: Information Technology
Semester: 6th
Roll No: 19118901
Q1. Write a program to draw the pixel (x,y) and display the color
of the pixel.
Ans: C++ Code:
1. #include <graphics.h>
2. #include <stdio.h>
3.
4. int main()
5. {
6. int gd = DETECT, gm, color;
7. initgraph(&gd, &gm, "");
8.
9. // putpixel function
10. putpixel(45, 60, BLUE);
11. putpixel(20, 100, WHITE);
12. putpixel(200, 100, LIGHTBLUE);
13. putpixel(150, 100, LIGHTGREEN);
14. putpixel(200, 50, YELLOW);
15. putpixel(120, 70, RED);
16.
17. getch();
18. closegraph();
19. return 0;
20. }
21.

Output:
Q2. Write a program to draw hut and smiley using various
graphics function.
Ans: C++ code to draw Hut:

1. #include<iostream>
2. #include<conio.h>
3. #include<graphics.h>
4.
5. int main() {
6. int gd=DETECT,gm,i;
7. initgraph(&gd,&gm,(char*)"");
8. rectangle(100,150,200,280);
9. rectangle(200,150,350,280);
10. rectangle(120,230,170,280);
11.
12. line(150,100,100,150);
13. line(150,100,200,150);
14. line(150,100,320,100);
15. line(320,100,350,150);
16.
17. getch();
18. closegraph();
19. return 0;
20. }
21.

Output:
C++ Code to draw smiley:
1. #include <conio.h>
2. #include <dos.h>
3. #include <graphics.h>
4. #include <stdio.h>
5.
6. int main()
7. {
8. int gr = DETECT, gm;
9. initgraph(&gr, &gm, (char*)"");
10.
11.
12. setcolor(YELLOW);
13.
14. circle(300, 100, 40);
15. setfillstyle(SOLID_FILL, YELLOW);
16. floodfill(300, 100, YELLOW);
17.
18.
19. setcolor(BLACK);
20. setfillstyle(SOLID_FILL, BLACK);
21.
22. // creating eyes
23. fillellipse(310, 85, 2, 6);
24. fillellipse(290, 85, 2, 6);
25.
26. // creating mouth
27. ellipse(300, 100, 205, 335, 20, 9);
28. ellipse(300, 100, 205, 335, 20, 10);
29. ellipse(300, 100, 205, 335, 20, 11);
30.
31. getch();
32. closegraph();
33.
34. return 0;
35. }
36.

Output:
Q3. Write a program to implement DDA line drawing algorithm.
Ans: Code to implement DDA algorithm:
1. #include<graphics.h>
2. #include<conio.h>
3. #include<stdio.h>
4. int main()
5. {
6. int gd = DETECT ,gm, i;
7. float x, y,dx,dy,steps;
8. int x0, x1, y0, y1;
9. initgraph(&gd, &gm, (char*)"");
10. setbkcolor(WHITE);
11. x0 = 100 , y0 = 200, x1 = 500, y1 = 300;
12. dx = (float)(x1 - x0);
13. dy = (float)(y1 - y0);
14. if(dx>=dy)
15. {
16. steps = dx;
17. }
18. else
19. {
20. steps = dy;
21. }
22. dx = dx/steps;
23. dy = dy/steps;
24. x = x0;
25. y = y0;
26. i = 1;
27. while(i<= steps)
28. {
29. putpixel(x, y, RED);
30. x += dx;
31. y += dy;
32. i=i+1;
33. }
34. getch();
35. closegraph();
36. return 0;
37. }
38.

Output:
Q4. Write a program to implement Bresenham’s line drawing
algorithm.
Ans: Code to implement Bresenham’s line drawing algorithm:

1. #include <iostream>
2. #include <conio.h>
3. #include <graphics.h>
4. #include<dos.h>
5. using namespace std;
6. void bsline(int x,int y,int x2,int y2)
7. {
8. int dx,dy,p;
9. dx=x2-x;
10. dy=y2-y;
11. p = 2 * (dy) - (dx);
12. while(x <= x2)
13. {
14. if(p < 0)
15. {
16. x=x+1;
17. y=y;
18. p = p + 2 * (dy);
19. }
20. else
21. {
22. x=x+1;
23. y=y+1;
24. p = p + 2 * (dy - dx);
25. }
26. putpixel(x,y,RED);
27. delay(10);
28. }
29. }
30. int main()
31. {
32. int gd=DETECT,gm;
33. initgraph(&gd,&gm,(char*)"");
34. int x1,x2,y1,y2;
35. cout<<"Enter the x1,y1,x2,y2 values : ";
36. cin>>x1>>y1>>x2>>y2;
37. bsline(x1,y1,x2,y2);
38. getch();
39. closegraph();
40. return 0;
41. }
42.
Output:

Q5. Write a program to implement Bresenham’s Circle drawing


algorithm.
Ans: Code to implement Bresenham’s Circle drawing algorithm:
1. #include <graphics.h>
2. #include <stdlib.h>
3. #include <stdio.h>
4. #include <conio.h>
5. #include <math.h>
6.
7. void EightColors(int xc,int yc,int x,int y)
8. {
9. putpixel(x+xc,y+yc,BLUE);
10. putpixel(x+xc,-y+yc,WHITE);
11. putpixel(-x+xc,-y+yc,GREEN);
12. putpixel(-x+xc,y+yc,RED);
13. putpixel(y+xc,x+yc,12);
14. putpixel(y+xc,-x+yc,14);
15. putpixel(-y+xc,-x+yc,15);
16. putpixel(-y+xc,x+yc,6);
17. }
18.
19. void BresenhamCircle(int xc,int yc,int r)
20. {
21. int x=0,y=r,d=3-(2*r);
22. EightColors(xc,yc,x,y);
23.
24. while(x<=y)
25. {
26. if(d<=0)
27. {
28. d=d+(4*x)+6;
29. }
30. else
31. {
32. d=d+(4*x)-(4*y)+10;
33. y=y-1;
34. }
35. x=x+1;
36. EightColors(xc,yc,x,y);
37. }
38. }
39.
40. int main(void)
41. {
42.
43. int xc,yc,r,gdriver = DETECT, gmode, errorcode;
44. initgraph(&gdriver, &gmode, "");
45. errorcode = graphresult();
46.
47. if (errorcode != grOk) /* an error occurred */
48. {
49. printf("Graphics error: %s\n",
grapherrormsg(errorcode));
50. printf("Press any key to halt:");
51. getch();
52. exit(1);
53. }
54. printf("Enter the values of xc and yc :");
55. scanf("%d%d",&xc,&yc);
56. printf("Enter the value of radius :");
57. scanf("%d",&r);
58. BresenhamCircle(xc,yc,r);
59.
60. getch();
61. closegraph();
62. return 0;
63. }
64.

Output:

You might also like