KEMBAR78
CG 7 Bto 10 | PDF | Computer Programming | Computing
0% found this document useful (0 votes)
47 views27 pages

CG 7 Bto 10

The document describes the source code for implementing a scenery view using graphics programming in C. The code first initializes a window and draws hills in the background using lines and floodfill. It then draws a bird flying across the screen. Finally, it draws two clouds moving across the sky using ellipses, fill styles and floodfill to color them in. The code animates the bird and clouds moving from right to left to simulate movement in the scenery view.

Uploaded by

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

CG 7 Bto 10

The document describes the source code for implementing a scenery view using graphics programming in C. The code first initializes a window and draws hills in the background using lines and floodfill. It then draws a bird flying across the screen. Finally, it draws two clouds moving across the sky using ellipses, fill styles and floodfill to color them in. The code animates the bird and clouds moving from right to left to simulate movement in the scenery view.

Uploaded by

Ayush Poojari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Practical No.

- 8[A]

Cohen-Surtherland Clipping Algorithm

Source Code:

1. #include<graphics.h>

2. #include<conio.h>

3. #include<stdio.h>

4. #include<math.h>

5. int main()

6. {

7. int rcode_begin[4]={0,0,0,0},rcode_end[4]={0,0,0,0},region_code[4];

8. int W_xmax,W_ymax,W_xmin,W_ymin,flag=0;

9. float slope;

10. int x,y,x1,y1,i, xc,yc;

11. int gr=DETECT,gm;

12. initgraph(&gr,&gm,"C:\\TURBOC3\\BGI");

13. printf("\n****** Cohen Sutherland Line Clipping algorithm ***********");

14. printf("\n Now, enter XMin, YMin =");

15. scanf("%d %d",&W_xmin,&W_ymin);

16. printf("\n First enter XMax, YMax =");

17. scanf("%d %d",&W_xmax,&W_ymax);

18. printf("\n Please enter intial point x and y= ");

19. scanf("%d %d",&x,&y);

20. printf("\n Now, enter final point x1 and y1= ");

21. scanf("%d %d",&x1,&y1);

22. cleardevice();
23. rectangle(W_xmin,W_ymin,W_xmax,W_ymax);

24. line(x,y,x1,y1);

25. line(0,0,600,0);

26. line(0,0,0,600);

27. if(y>W_ymax) {

28. rcode_begin[0]=1; // Top

29. flag=1 ;

30. }

31. if(y<W_ymin) {

32. rcode_begin[1]=1; // Bottom

33. flag=1;

34. }

35. if(x>W_xmax) {

36. rcode_begin[2]=1; // Right

37. flag=1;

38. }

39. if(x<W_xmin) {

40. rcode_begin[3]=1; //Left

41. flag=1;

42. }

43. //end point of Line

44. if(y1>W_ymax){

45. rcode_end[0]=1; // Top

46. flag=1;

47. }
48. if(y1<W_ymin) {

49. rcode_end[1]=1; // Bottom

50. flag=1;

51. }

52. if(x1>W_xmax){

53. rcode_end[2]=1; // Right

54. flag=1;

55. }

56. if(x1<W_xmin){

57. rcode_end[3]=1; //Left

58. flag=1;

59. }

60. if(flag==0)

61. {

62. printf("No need of clipping as it is already in window");

63. }

64. flag=1;

65. for(i=0;i<4;i++){

66. region_code[i]= rcode_begin[i] && rcode_end[i] ;

67. if(region_code[i]==1)

68. flag=0;

69. }

70. if(flag==0)

71. {

72. printf("\n Line is completely outside the window");


73. }

74. else{

75. slope=(float)(y1-y)/(x1-x);

76. if(rcode_begin[2]==0 && rcode_begin[3]==1) //left

77. {

78. y=y+(float) (W_xmin-x)*slope ;

79. x=W_xmin;

80. }

81. if(rcode_begin[2]==1 && rcode_begin[3]==0) // right

82. {

83. y=y+(float) (W_xmax-x)*slope ;

84. x=W_xmax;

85.

86. }

87. if(rcode_begin[0]==1 && rcode_begin[1]==0) // top

88. {

89. x=x+(float) (W_ymax-y)/slope ;

90. y=W_ymax;

91.

92. }

93. if(rcode_begin[0]==0 && rcode_begin[1]==1) // bottom

94. {

95. x=x+(float) (W_ymin-y)/slope ;

96. y=W_ymin;

97. }
98. // end points

99. if(rcode_end[2]==0 && rcode_end[3]==1) //left

100.{

101.y1=y1+(float) (W_xmin-x1)*slope ;

102.x1=W_xmin;

103.}

104.if(rcode_end[2]==1 && rcode_end[3]==0) // right

105.{

106.y1=y1+(float) (W_xmax-x1)*slope ;

107.x1=W_xmax;

108.

109.}

110.if(rcode_end[0]==1 && rcode_end[1]==0) // top

111.{

112.x1=x1+(float) (W_ymax-y1)/slope ;

113.y1=W_ymax;

114.

115.}

116.if(rcode_end[0]==0 && rcode_end[1]==1) // bottom

117.{

118.x1=x1+(float) (W_ymin-y1)/slope ;

119.y1=W_ymin;

120.}

121.}

122.delay(1000);
123.clearviewport();

124.rectangle(W_xmin,W_ymin,W_xmax,W_ymax);

125.line(0,0,600,0);

126.line(0,0,0,600);

127.setcolor(RED);

128.line(x,y,x1,y1);

129.getch();

130.closegraph();

131.}

Output:
Practical No.-8[B]

Liang-Barsky Line Clipping Algorithm

Source Code:
1. #include<graphics.h>

2. #include<dos.h>

3. #include<stdio.h>

4. #include<math.h>

5. int main()

6. {

7. int i,gd=DETECT,gm;

8. int x1,y1,x2,y2,xmin,xmax,ymin,ymax,xx1,xx2,yy1,yy2,dx,dy;

9. float t1,t2,p[4],q[4],temp;

10. x1=120;

11. y1=120;

12. x2=300;

13. y2=300;

14. xmin=100;

15. ymin=100;

16. xmax=250;

17. ymax=250;

18. initgraph(&gd,&gm,"c:\\turboc3\\bgi");

19. rectangle(xmin,ymin,xmax,ymax);

20. dx=x2-x1;

21. dy=y2-y1;

22. p[0]=-dx;
23. p[1]=dx;

24. p[2]=-dy;

25. p[3]=dy;

26. q[0]=x1-xmin;

27. q[1]=xmax-x1;

28. q[2]=y1-ymin;

29. q[3]=ymax-y1;

30. for(i=0;i<4;i++)

31. {

32. if(p[i]==0)

33. {

34. printf("line is parallel to one of the clipping boundary");

35. if(q[i]>=0)

36. {

37. if(i<2)

38. {

39. if(y1<ymin)

40. {

41. y1=ymin;

42. }

43. if(y2>ymax)

44. {

45. y2=ymax;

46. }

47. line(x1,y1,x2,y2);

48.

49. }

50. if(i>1)
51. {

52. if(x1<xmin)

53. {

54. x1=xmin;

55. }

56. if(x2>xmax)

57. {

58. x2=xmax;

59. }

60. line(x1,y1,x2,y2);

61. }

62. }

63. }

64. }

65. t1=0;

66. t2=1;

67. for(i=0;i<4;i++)

68. {

69. temp=q[i]/p[i];

70. if(p[i]<0)

71. {

72. if(t1<=temp)

73. t1=temp;

74. }

75. else

76. {

77. if(t2>temp)

78. t2=temp;
79. }

80. }

81. if(t1<t2)

82. {

83. xx1=x1+t1*p[1];

84. xx2=x1+t2*p[1];

85. yy1=y1+t1*p[3];

86. yy2=y1+t2*p[3];

87. line(xx1,yy1,xx2,yy2);

88. }

89. delay(5000);

90. closegraph();

91. }

Output:
Practical No.-9[A]

Flood Fill Algorithm

Source Code:
1. #include<stdio.h>

2. #include<conio.h>

3. #include<graphics.h>

4. #include<dos.h>

5. int flood(int x, int y ,int newcolor, int oldcolor)

6. {

7. if(getpixel(x,y)== oldcolor)

8. {

9. putpixel(x,y,newcolor);

10. delay(10);

11. flood(x+1,y, newcolor, oldcolor);

12. flood(x,y+1, newcolor, oldcolor);

13. flood(x-1,y, newcolor, oldcolor);

14. flood(x,y-1, newcolor, oldcolor);

15. }

16. }

17. int main()

18. {

19. int gd = DETECT,gm;

20. initgraph(&gd,&gm, "c:/tc/bgi");

21. rectangle(250,200,300,250);

22. flood(251,201,4,0);

23. getch();

24. closegraph();
25. }

Output:
Practical No.-9[B]

Boundary Fill Algorithm

Source Code:
1. #include<graphics.h>

2. void boundaryfill4(int x, int y ,int fill_color, int boun_color)

3. {

4. if(getpixel(x,y)!=boun_color && getpixel(x,y)!=fill_color)

5. {

6. putpixel(x,y,fill_color);

7. delay(5);

8. boundaryfill4(x+1,y, fill_color, boun_color);

9. boundaryfill4(x,y+1, fill_color, boun_color);

10. boundaryfill4(x-1,y, fill_color, boun_color);

11. boundaryfill4(x,y-1, fill_color, boun_color);

12. }

13. }

14. int main()

15. {

16. int gm,gd = DETECT;

17. initgraph(&gm,&gd,"c:/tc/bgi");

18. int x = 200, y = 140, radius = 60;

19. circle(x,y,radius);

20. boundaryfill4(x,y,6,15);

21. getch();

22. closegraph();

23. return 0;
24. }

Output:
Practical No.-10

Scenery View
Source Code:

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<cmath>

int main()

initwindow(1200,600,"SIMPLE CITY SCENARIO");

for(int j=0;j<1200;j+=3)

//HILLS

setcolor(6);

setfillstyle(SOLID_FILL,6);

line(0,350,50,250);

line(50,250,150,350);

line(150,350,250,200);

line(250,200,450,350);

line(450,350,700,190);

line(700,190,900,350);

line(900,350,1050,250);

line(1050,250,1200,350);

line(0,350,1200,350);
floodfill(5,340,6);

setbkcolor(2);

// Drawing the bird

line(50+j,50,70+j,70);

line(70+j,70,50+j,90);

line(80+j,80,100+j,100);

line(100+j,100,80+j,110);

line(150+j,60,170+j,80);

line(170+j,80,150+j,100);

//1st cloud

setcolor(15);

setfillstyle(SOLID_FILL,15);

ellipse(100-j,50,-20,200,25,20);

ellipse(125-j,70,240,110,20,20);

ellipse(100-j,90,170,380,20,20);

ellipse(80-j,70,70,290,20,20);

floodfill(100-j,60,15);

//2nd cloud

setcolor(15);

setfillstyle(SOLID_FILL,15);

ellipse(500-j,105,0,180,30,25);

ellipse(530-j,130,285,90,40,30);

ellipse(515-j,160,210,360,25,30);

ellipse(480-j,150,180,300,30,30);

ellipse(470-j,130,85,235,30,25);
floodfill(500-j,140,15);

// 3rd clouds

setcolor(WHITE);

setfillstyle(SOLID_FILL,WHITE);

ellipse(900-j,50,-20,200,25,20);

ellipse(925-j,70,240,110,20,20);

ellipse(900-j,90,170,380,20,20);

ellipse(880-j,70,70,290,20,20);

floodfill(900-j,60,WHITE);

// Draw Sun

setcolor(YELLOW);

setfillstyle(SOLID_FILL, YELLOW);

circle(700, 100, 40);

floodfill(700, 100, YELLOW);

// Draw Sun Rays

setcolor(YELLOW);

for (int i = 0; i < 360; i += 30) {

line(700, 100, 700 + 60 * cos(i * 3.14 / 180), 100 + 60 * sin(i * 3.14 / 180));

// building

setcolor(4);

setfillstyle(HATCH_FILL,4);

setbkcolor(3);

rectangle(50,500,200,300);
floodfill(51,499,4);

setcolor(13);

setfillstyle(HATCH_FILL,13);

setbkcolor(12);

rectangle(350,500,450,300);

floodfill(351,499,13);

setcolor(12);

setfillstyle(HATCH_FILL,12);

setbkcolor(5);

rectangle(450,500,500,350);

floodfill(451,499,12);

setcolor(9);

setfillstyle(HATCH_FILL,9);

setbkcolor(13);

rectangle(500,500,600,300);

floodfill(501,499,9);

setcolor(3);

setfillstyle(HATCH_FILL,3);

setbkcolor(14);

rectangle(800,500,1000,300);

floodfill(801,499,3);

setcolor(12);

setfillstyle(HATCH_FILL,12);

setbkcolor(5);

rectangle(1000,500,1100,330);
floodfill(1001,499,12);

//road

setcolor(8);

setfillstyle(SOLID_FILL,8);

rectangle(0,500,1200,600);

floodfill(10,510,8);

setcolor(15);

for(int i=0;i<1200;i+=70)

line(0+i,550,50+i,550);

//CAR BODY

setcolor(4);

setfillstyle(SOLID_FILL,4);

ellipse(0+j,537,0,180,16,16);

ellipse(70+j,537,0,180,16,16);

line(14+j,535,56+j,535);//center line

line(84+j,535,101+j,535);//front line

line(100+j,536,100+j,508);//front height

line(101+j,510,93+j,504); //front bonet

line(94+j,505,87+j,484);//driver view

line(89+j,485,36+j,485);//ceiling

line(37+j,484,37+j,516);//half join

line(-14+j,535,-36+j,535);//back line

line(-35+j,536,-35+j,514);//back height
line(-36+j,515,38+j,515);//back height to half line

floodfill(20+j,533, 4);

//wheel 1

setcolor(0);

setfillstyle(SOLID_FILL,0);

pieslice(0+j,535,0-j,90-j,10);

pieslice(0+j,535,180-j,270-j,10);

circle(0+j,535,10);

ellipse(0+j,537,0,180,16,16);

//wheel 2

pieslice(70+j,535,0-j,90-j,10);

pieslice(70+j,535,180-j,270-j,10);

circle(70+j,535,10);

ellipse(70+j,537,0,180,16,16);

//mirror

setcolor(0);

setfillstyle(SLASH_FILL,0);

line(43+j,505,87+j,505);

line(43+j,505,43+j,490);

line(43+j,490,83+j,490);

line(83+j,490,87+j,505);

floodfill(78+j,500,0);

//mirror driver

setcolor(0);

setfillstyle(SLASH_FILL,0);
rectangle(61.5+j,490,63.5+j,505);

floodfill(62.5+j,500,0);

//background color

setbkcolor(11);

//floodfill(j,520,6);

delay(50);

cleardevice();

getch();

closegraph();

Output:
Practical No. -7[B]

Source Code:

#include <stdio.h>

#include <graphics.h>

#include <stdlib.h>

#include <math.h>

#include <conio.h>

void reset (int h[][2])

int val[9][2] = {

{ 50, 50 },{ 75, 50 },{ 75, 75 },{ 100, 75 },

{ 100, 50 },{ 125, 50 },{ 125, 100 },{ 87, 125 },{ 50, 100 }

};

int i;

for (i=0; i<9; i++)

h[i][0] = val[i][0]-50;

h[i][1] = val[i][1]-50;

void draw (int h[][2])

int i;

setlinestyle (DOTTED_LINE, 0, 1);

line (320, 0, 320, 480);

line (0, 240, 640, 240);

setlinestyle (SOLID_LINE, 0, 1);


for (i=0; i<8; i++)

line (320+h[i][0], 240-h[i][1], 320+h[i+1][0], 240-h[i+1][1]);

line (320+h[0][0], 240-h[0][1], 320+h[8][0], 240-h[8][1]);

void rotate (int h[][2], float angle)

int i;

for (i=0; i<9; i++)

int xnew, ynew;

xnew = h[i][0] * cos (angle) - h[i][1] * sin (angle);

ynew = h[i][0] * sin (angle) + h[i][1] * cos (angle);

h[i][0] = xnew; h[i][1] = ynew;

void scale (int h[][2], int sx, int sy)

int i;

for (i=0; i<9; i++)

h[i][0] *= sx;

h[i][1] *= sy;

void translate (int h[][2], int dx, int dy)

int i;

for (i=0; i<9; i++)

{
h[i][0] += dx;

h[i][1] += dy;

void reflect (int h[][2], int m, int c)

int i;

float angle;

for (i=0; i<9; i++)

h[i][1] -= c;

angle = M_PI/2 - atan (m);

rotate (h, angle);

for (i=0; i<9; i++)

h[i][0] = -h[i][0];

angle = -angle;

rotate (h, angle);

for (i=0; i<9; i++)

h[i][1] += c;

void ini()

int gd=DETECT,gm;

initgraph(&gd,&gm,"..\\bgi");

void dini()

getch();

closegraph();

}
int main()

int h[9][2],sx,sy,x,y,m,c,choice;

do

printf("1. Scaling about the origin.\n");

printf("2. Scaling about an arbitrary point.\n");

printf("3. Reflection about the line y = mx + c.\n");

printf("4. Exit\n");

printf("Enter the choice: ");

scanf("%d",&choice);

switch(choice)

case 1: printf ("Enter the x- and y-scaling factors: ");

scanf ("%d%d", &sx, &sy);

ini();

reset (h);

draw (h);getch();

scale (h, sx, sy);

cleardevice();

draw (h);

dini();

break;

case 2: printf ("Enter the x- and y-scaling factors: ");

scanf ("%d%d", &sx, &sy);

printf ("Enter the x- and y-coordinates of the point: ");

scanf ("%d%d", &x, &y);

ini();

reset (h);
translate (h, x, y);// Go to arbitrary point

draw(h); getch();//Show its arbitrary position

cleardevice();

translate(h,-x,-y);//Take it back to origin

draw(h);

getch();

cleardevice();

scale (h, sx, sy);//Now Scale it

draw(h);

getch();

translate (h, x, y);//Back to Arbitrary point

cleardevice();

draw (h);

putpixel (320+x, 240-y, WHITE);

dini();

break;

case 3: printf ("Enter the values of m and c: ");

scanf ("%d%d", &m, &c);

ini();

reset (h);

draw (h); getch();

reflect (h, m, c);

cleardevice();

draw (h);

dini();

break;

case 4: exit(0);

}while(choice!=4);
}

Output:

You might also like