Program to implement DDA Line Drawing Algorithm:
1. #include<graphics.h>
2. #include<conio.h>
3. #include<stdio.h>
4. void main()
5. {
6. intgd = DETECT ,gm, i;
7. float x, y,dx,dy,steps;
8. int x0, x1, y0, y1;
9. initgraph(&gd, &gm, "C:\\TC\\BGI");
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. }
Output:
Program to implement Bresenham's Line Drawing Algorithm:
1. #include<stdio.h>
2. #include<graphics.h>
3. void drawline(int x0, int y0, int x1, int y1)
4. {
5. int dx, dy, p, x, y;
6. dx=x1-x0;
7. dy=y1-y0;
8. x=x0;
9. y=y0;
10. p=2*dy-dx;
11. while(x<x1)
12. {
13. if(p>=0)
14. {
15. putpixel(x,y,7);
16. y=y+1;
17. p=p+2*dy-2*dx;
18. }
19. else
20. {
21. putpixel(x,y,7);
22. p=p+2*dy;}
23. x=x+1;
24. }
25. }
26. int main()
27. {
28. int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
29. initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
30. printf("Enter co-ordinates of first point: ");
31. scanf("%d%d", &x0, &y0);
32. printf("Enter co-ordinates of second point: ");
33. scanf("%d%d", &x1, &y1);
34. drawline(x0, y0, x1, y1);
35. return 0;
36. }
Output:
Program to draw a circle using 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 EightWaySymmetricPlot(int xc,int yc,int x,int y)
8. {
9. putpixel(x+xc,y+yc,RED);
10. putpixel(x+xc,-y+yc,YELLOW);
11. putpixel(-x+xc,-y+yc,GREEN);
12. putpixel(-x+xc,y+yc,YELLOW);
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. EightWaySymmetricPlot(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. EightWaySymmetricPlot(xc,yc,x,y);
37. }
38. }
39.
40. int main(void)
41. {
42. /* request auto detection */
43. int xc,yc,r,gdriver = DETECT, gmode, errorcode;
44. /* initialize graphics and local variables */
45. initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");
46.
47. /* read result of initialization */
48. errorcode = graphresult();
49.
50. if (errorcode != grOk) /* an error occurred */
51. {
52. printf("Graphics error: %s\n", grapherrormsg(errorcode));
53. printf("Press any key to halt:");
54. getch();
55. exit(1); /* terminate with an error code */
56. }
57. printf("Enter the values of xc and yc :");
58. scanf("%d%d",&xc,&yc);
59. printf("Enter the value of radius :");
60. scanf("%d",&r);
61. BresenhamCircle(xc,yc,r);
62.
63. getch();
64. closegraph();
65. return 0;
66. }
Output:
Program to Implement Ellipse Drawing Algorithm:
1. #include<stdio.h>
2. #include<conio.h>
3. #include<graphics.h>
4. #include<math.h>
5. void disp();
6. float x,y;
7. intxc,yc;
8. void main()
9. {
10. intgd=DETECT,gm,a,b;
11. float p1,p2;
12. clrscr();
13. initgraph(&gd,&gm,"c:\\turboc3\\bgi");
14. printf("*** Ellipse Generating Algorithm ***\n");
15. printf("Enter the value of Xc\t");
16. scanf("%d",&xc);
17. printf("Enter the value of yc\t");
18. scanf("%d",&yc);
19. printf("Enter X axis length\t");
20. scanf("%d",&a);
21. printf("Enter Y axis length\t");
22. scanf("%d",&b);
23. x=0;y=b;
24. disp();
25. p1=(b*b)-(a*a*b)+(a*a)/4;
26. while((2.0*b*b*x)<=(2.0*a*a*y))
27. {
28. x++;
29. if(p1<=0)
30. p1=p1+(2.0*b*b*x)+(b*b);
31. else
32. {
33. y--;
34. p1=p1+(2.0*b*b*x)+(b*b)-(2.0*a*a*y);
35. }
36. disp();
37. x=-x;
38. disp();
39. x=-x;
40. delay(50);
41. }
42. x=a;
43. y=0;
44. disp();
45. p2=(a*a)+2.0*(b*b*a)+(b*b)/4;
46. while((2.0*b*b*x)>(2.0*a*a*y))
47. {
48. y++;
49. if(p2>0)
50. p2=p2+(a*a)-(2.0*a*a*y);
51. else
52. {
53. x--;
54. p2=p2+(2.0*b*b*x)-(2.0*a*a*y)+(a*a);
55. }
56. disp();
57. y=-y;
58. disp();
59. y=-y;
60. delay(50);
61. }
62. getch();
63. closegraph();
64. }
65. void disp()
66. {
67. putpixel(xc+x,yc+y,7);
68. putpixel(xc-x,yc+y,7);
69. putpixel(xc+x,yc-y,7);
70. putpixel(xc+x,yc-y,7);
71. }
Output:
1. #include<stdio.h>
2. #include<conio.h>
3. #include<graphics.h>
4. #include<dos.h>
5. void flood(int,int,int,int);
6. void main()
7. {
8. intgd=DETECT,gm;
9. initgraph(&gd,&gm,"C:/TURBOC3/bgi");
10. rectangle(50,50,250,250);
11. flood(55,55,10,0);
12. getch();
13. }
14. void flood(intx,inty,intfillColor, intdefaultColor)
15. {
16. if(getpixel(x,y)==defaultColor)
17. {
18. delay(1);
19. putpixel(x,y,fillColor);
20. flood(x+1,y,fillColor,defaultColor);
21. flood(x-1,y,fillColor,defaultColor);
22. flood(x,y+1,fillColor,defaultColor);
23. flood(x,y-1,fillColor,defaultColor);
24. }
25. }
Output:
Program2: To implement 8-connected flood fill algorithm:
1. #include<stdio.h>
2. #include<graphics.h>
3. #include<dos.h>
4. #include<conio.h>
5. void floodfill(intx,inty,intold,intnewcol)
6. {
7. int current;
8. current=getpixel(x,y);
9. if(current==old)
10. {
11. delay(5);
12. putpixel(x,y,newcol);
13. floodfill(x+1,y,old,newcol);
14. floodfill(x-1,y,old,newcol);
15. floodfill(x,y+1,old,newcol);
16. floodfill(x,y-1,old,newcol);
17. floodfill(x+1,y+1,old,newcol);
18. floodfill(x-1,y+1,old,newcol);
19. floodfill(x+1,y-1,old,newcol);
20. floodfill(x-1,y-1,old,newcol);
21. }
22. }
23. void main()
24. {
25. intgd=DETECT,gm;
26. initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
27. rectangle(50,50,150,150);
28. floodfill(70,70,0,15);
29. getch();
30. closegraph();
31. }
Output:
Program to rotate a line:
1. #include<stdio.h>
2. #include<graphics.h>
3. #include<math.h>
4. int main()
5. {
6. intgd=0,gm,x1,y1,x2,y2;
7. double s,c, angle;
8. initgraph(&gd, &gm, "C:\\TC\\BGI");
9. setcolor(RED);
10. printf("Enter coordinates of line: ");
11. scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
12. cleardevice();
13. setbkcolor(WHITE);
14. line(x1,y1,x2,y2);
15. getch();
16. setbkcolor(BLACK);
17. printf("Enter rotation angle: ");
18. scanf("%lf", &angle);
19. setbkcolor(WHITE);
20. c = cos(angle *3.14/180);
21. s = sin(angle *3.14/180);
22. x1 = floor(x1 * c + y1 * s);
23. y1 = floor(-x1 * s + y1 * c);
24. x2 = floor(x2 * c + y2 * s);
25. y2 = floor(-x2 * s + y2 * c);
26. cleardevice();
27. line(x1, y1 ,x2, y2);
28. getch();
29. closegraph();
30. return 0;
31. }
Output:
Before rotation
After rotation
Program to rotate a Triangle:
1. #include<stdio.h>
2. #include<graphics.h>
3. #include<math.h>
4. main()
5. {
6. intgd=0,gm,x1,y1,x2,y2,x3,y3;
7. double s,c, angle;
8. initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
9. setcolor(RED);
10. printf("Enter coordinates of triangle: ");
11. scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2, &x3, &y3);
12. setbkcolor(WHITE);
13. cleardevice();
14. line(x1,y1,x2,y2);
15. line(x2,y2, x3,y3);
16. line(x3, y3, x1, y1);
17. getch();
18. setbkcolor(BLACK);
19. printf("Enter rotation angle: ");
20. scanf("%lf", &angle);
21. setbkcolor(WHITE);
22. c = cos(angle *M_PI/180);
23. s = sin(angle *M_PI/180);
24. x1 = floor(x1 * c + y1 * s);
25. y1 = floor(-x1 * s + y1 * c);
26. x2 = floor(x2 * c + y2 * s);
27. y2 = floor(-x2 * s + y2 * c);
28. x3 = floor(x3 * c + y3 * s);
29. y3 = floor(-x3 * s + y3 * c);
30. cleardevice();
31. line(x1, y1 ,x2, y2);
32. line(x2,y2, x3,y3);
33. line(x3, y3, x1, y1);
34. getch();
35. closegraph();
36. return 0;
37. }
Output:
Before rotation
After rotation
Program to perform Mirror Reflection about a line:
1. #include <iostream.h>
2. #include <conio.h>
3. #include <graphics.h>
4. #include <math.h>
5. #include <stdlib.h>
6. #define pi 3.14
7. class arc
8. {
9. float x[10],y[10],theta,ref[10][10],ang;
10. float p[10][10],p1[10][10],x1[10],y1[10],xm,ym;
11. int i,k,j,n;
12. public:
13. void get();
14. void cal ();
15. void map ();
16. void graph ();
17. void plot ();
18. void plot1();
19. };
20. void arc::get ()
21. {
22. cout<<"\n ENTER ANGLE OF LINE INCLINATION AND Y INTERCEPT";
23. cin>> ang >> b;
24. cout <<"\n ENTER NO OF VERTICES";
25. cin >> n;
26. cout <<"\n ENTER";
27. for (i=0; i<n; i++)
28. {
29. cout<<"\n x["<<i<<"] and y["<<i<<"]";
30. }
31. theta =(ang * pi)/ 180;
32. ref [0] [0] = cos (2 * theta);
33. ref [0] [1] = sin (2 * theta);
34. ref [0] [2] = -b *sin (2 * theta);
35. ref [1] [0] = sin (2 * theta);
36. ref [1] [1] = -cos (2 * theta);
37. ref [1] [2] = b * (cos (2 * theta)+1);
38. ref [2] [0]=0;
39. ref [2] [1]=0;
40. ref [2] [2] = 1;
41. }
42. void arc :: cal ()
43. {
44. for (i=0; i < n; i++)
45. {
46. p[0] [i] = x [i];
47. p [1] [i] = y [i];
48. p [2] [i] = 1;
49. }
50. for (i=0; i<3;i++)
51. {
52. for (j=0; j<n; j++)
53. {
54. p1 [i] [j]=0;
55. for (k=0;k<3; k++)
56. }
57. p1 [i] [j] + = ref [i] [k] * p [k] [j];
58. }
59. for (i=0; i<n; i++)
60. {
61. x1 [i]=p1[0] [i];
62. y1 [i] = p1 [1] [i];
63. }
64. }
65. void arc :: map ()
66. {
67. int gd = DETECT,gm;
68. initgraph (&gd, &gm, " ");
69. int errorcode = graphresult ();
70. /* an error occurred */
71. if (errorcode ! = grOK)
72. {
73. printf ("Graphics error: %s \n", grapherrormsg (errorcode));
74. printf ("Press any key to halt:");
75. getch ();
76. exit (1); /* terminate with an error code */
77. }
78. }
79. void arc :: graph ()
80. {
81. xm=getmaxx ()/2;
82. ym=getmaxy ()/2;
83. line (xm, 0, xmm 2*ym);
84. }
85. void arc :: plot 1 ()
86. {
87. for (i=0; i <n-1; i++)
88. {
89. circle (x1[i]+xm, (-y1[i]+ym), 2);
90. line (x1[i]+xm, (-y1[i]+ym), x1[i+1]+xm, (-y1[i+1]+ym));
91. }
92. line (x1[n-1)+xm, (-y1[n-1]+ym), x1[0]+xm, (-y1[0]+ym));
93. getch();
94. }
95. void arc :: plot ()
96. {
97. for (i=0; i <n-1; i++)
98. {
99. circle (x1[i]+xm, (-y1[i]+ym, 2);
100. line (x1[i]+xm, (-y1[i]+ym), x[i+1]+xm, (-y1[i+1]+ym));
101. }
102. line (x[n-1]+xm, (-y1[n-1]+ym), x[0]+xm, (-y[0]+ym));
103. getch();
104. }
105. void main ()
106. {
107. class arc a;
108. clrscr();
109. [Link]();
110. [Link]();
111. [Link]();
112. [Link]();
113. [Link]();
114. a.plot1();
115. getch();
116. }
Output: