[C++] Thuật toán Bresenham vẽ đường thẳng và đường tròn

1 bài đăng
Tags:
23.03.2018 / 21:06
hanhphucao
Bài đăng: 2470
Admin
Admin là người tận tâm và luôn hành xử đúng mực.

Dưới đây là thuật toán Bresenham đầy đủ các trường hợp để vẽ đường thẳng và đường tròn:

[IMAGE]

Thuật toán Bresenham vẽ đường thẳng:

CPP
  1. #include <iostream>
  2. #include <graphics.h>
  3. #define DELAY 10
  4. using namespace std;
  5.  
  6. void doiCho(int &a, int &b)
  7. {
  8. int tg=a;
  9. a=b;
  10. b=tg;
  11. }
  12.  
  13. void input(int &x, int &y, char k)
  14. {
  15. cout<<"Nhap toa do diem "<<k<<endl;
  16. cout<<"x=";cin>>x;
  17. cout<<"y=";cin>>y;
  18. }
  19.  
  20. void drawBresenham(int x1, int y1, int x2, int y2, int c)
  21. {
  22. int x,y,Const1,Const2,p;
  23. int dx=x2-x1;
  24. int dy=y2-y1;
  25. float m=(float)dy/dx;
  26.  
  27. if(x1==x2){
  28. if(y1>y2) doiCho(y1,y2);
  29. y=y1;
  30. while(y<=y2)
  31. {
  32. putpixel(x1,y,c);
  33. y++;
  34. Sleep(DELAY);
  35. }
  36. }else if(y1==y2){
  37. if(x1>x2) doiCho(x1,x2);
  38. x=x1;
  39. while(x<=x2)
  40. {
  41. putpixel(x,y1,c);
  42. x++;
  43. Sleep(DELAY);
  44. }
  45. }else if(m>0&&m<=1){
  46. if(x1>x2){
  47. doiCho(x1,x2);
  48. doiCho(y1,y2);
  49. }
  50. dx=x2-x1;dy=y2-y1;
  51. Const1=2*dy;
  52. Const2=2*(dy-dx);
  53. p=2*dy-dx;
  54. x=x1;y=y1;
  55. while(x<x2)
  56. {
  57. putpixel(x,y,c);
  58. x++;
  59. if(p<0) p+=Const1;
  60. else{
  61. p+=Const2;
  62. y++;
  63. }
  64. Sleep(DELAY);
  65. }
  66. }else if(m>1){
  67. if(x1>x2){
  68. doiCho(x1,x2);
  69. doiCho(y1,y2);
  70. }
  71. dx=x2-x1;dy=y2-y1;
  72. Const1=2*dx;
  73. Const2=2*(dx-dy);
  74. p=2*dx-dy;
  75. x=x1;y=y1;
  76. while(y<y2)
  77. {
  78. putpixel(x,y,c);
  79. y++;
  80. if(p<0) p+=Const1;
  81. else{
  82. p+=Const2;
  83. x++;
  84. }
  85. Sleep(DELAY);
  86. }
  87. }else if(m<0&&m>=-1){
  88. if(x1>x2){
  89. doiCho(x1,x2);
  90. doiCho(y1,y2);
  91. }
  92. dx=x2-x1;dy=y2-y1;
  93. Const1=2*dy;
  94. Const2=2*(dy+dx);
  95. p=2*dy+dx;
  96. x=x1;y=y1;
  97. while(x<x2)
  98. {
  99. putpixel(x,y,c);
  100. x++;
  101. if(p>0) p+=Const1;
  102. else{
  103. p+=Const2;
  104. y--;
  105. }
  106. Sleep(DELAY);
  107. }
  108. }else if(m<-1){
  109. if(y1>y2){
  110. doiCho(x1,x2);
  111. doiCho(y1,y2);
  112. }
  113. dx=x2-x1;dy=y2-y1;
  114. Const1=2*dx;
  115. Const2=2*(dy+dx);
  116. p=2*dx+dy;
  117. x=x1;y=y1;
  118. while(y<y2)
  119. {
  120. putpixel(x,y,c);
  121. y++;
  122. if(p>0) p+=Const1;
  123. else{
  124. p+=Const2;
  125. x--;
  126. }
  127. Sleep(DELAY);
  128. }
  129. }
  130. }
  131.  
  132. int main()
  133. {
  134. int x1,y1,x2,y2,c=5;
  135. input(x1,y1,'A');
  136. input(x2,y2,'B');
  137. initwindow(500,600);
  138. drawBresenham(x1,y1,x2,y2,c);
  139. line(x1,y1,x2,y2);
  140. getch();
  141. return 0;
  142. }

Thuật toán Bresenham vẽ đường tròn:

CPP
  1. #include <iostream>
  2. #include <graphics.h>
  3. #define DELAY 10
  4. using namespace std;
  5.  
  6. void input(int &x, int &y, int &R)
  7. {
  8. cout<<"Nhap toa do tam va ban kinh:"<<endl;
  9. cout<<"x=";cin>>x;
  10. cout<<"y=";cin>>y;
  11. cout<<"R=";cin>>R;
  12. }
  13.  
  14. void put8pixel(int x, int y, int x0, int y0, int c)
  15. {
  16. putpixel(x+x0,y+y0,c);
  17. putpixel(x+x0,-y+y0,c);
  18. putpixel(-x+x0,y+y0,c);
  19. putpixel(-x+x0,-y+y0,c);
  20. putpixel(y+x0,x+y0,c);
  21. putpixel(-y+x0,x+y0,c);
  22. putpixel(y+x0,-x+y0,c);
  23. putpixel(-y+x0,-x+y0,c);
  24. }
  25.  
  26. void drawBresenham(int x0, int y0, int R, int c)
  27. {
  28. int x=0,y=R,p=3-2*R;
  29. while(x<=y)
  30. {
  31. put8pixel(x,y,x0,y0,c);
  32. if(p<0) p+=4*x-6;
  33. else{
  34. p=p-4*y+4*x+10; //icon App on Samsung phone: p-=4*y+4*x+10;
  35. y--;
  36. }
  37. x++;
  38. Sleep(DELAY);
  39. }
  40. }
  41.  
  42. int main()
  43. {
  44. int x,y,R,c=5;
  45. input(x,y,R);
  46. initwindow(500,600);
  47. drawBresenham(x,y,R,c);
  48. getch();
  49. return 0;
  50. }

Chúc các bạn học tốt!

Code: PhoNho.Net / HanhPhucAo

Đã chỉnh sửa. hanhphucao (24.03.2018 / 00:34)