Untitled

mail@pastecode.io avatar
unknown
plain_text
23 days ago
4.5 kB
5
Indexable
Never
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. }


Program to draw a circle using Midpoint Algorithm:

1. #include <graphics.h>
2. #include <stdlib.h>
3. #include <math.h>
4. #include <stdio.h>
5. #include <conio.h>
6. #include <iostream.h>
7.
8. class bresen
9. {
10. float x, y,a, b, r, p;
11. public:
12. void get ();
13. void cal ();
14. };
15. void main ()
16. {
17. bresen b;
18. b.get ();
19. b.cal ();
20. getch ();
21. }
22. Void bresen :: get ()
23. {
24. cout<<"ENTER CENTER AND RADIUS";
25. cout<< "ENTER (a, b)";
26. cin>>a>>b;
27. cout<<"ENTER r";
28. cin>>r;
29. }
30. void bresen ::cal ()
31. {
32. /* request auto detection */
33. int gdriver = DETECT,gmode, errorcode;
34. int midx, midy, i;
35. /* initialize graphics and local variables */
36. initgraph (&gdriver, &gmode, " ");
37. /* read result of initialization */
38. errorcode = graphresult ();
39. if (errorcode ! = grOK) /*an error occurred */
40. {
41. printf("Graphics error: %s \n", grapherrormsg (errorcode);
42. printf ("Press any key to halt:");
43. getch ();
44. exit (1); /* terminate with an error code */
45. }
46. x=0;
47. y=r;
48. putpixel (a, b+r, RED);
49. putpixel (a, b-r, RED);
50. putpixel (a-r, b, RED);
51. putpixel (a+r, b, RED);
52. p=5/4)-r;
53. while (x<=y)
54. {
55. If (p<0)
56. p+= (4*x)+6;
57. else
58. {
59. p+=(2*(x-y))+5;
60. y--;
61. }
62. x++;
63. putpixel (a+x, b+y, RED);
64. putpixel (a-x, b+y, RED);
65. putpixel (a+x, b-y, RED);
66. putpixel (a+x, b-y, RED);
67. putpixel (a+x, b+y, RED);
68. putpixel (a+x, b-y, RED);
69. putpixel (a-x, b+y, RED);
70. putpixel (a-x, b-y, RED);
71. }
72. }


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. }


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. }
Leave a Comment