<< | к задаче | главная | печатать | обсудить(0 сообщений) >>
Задача: Рисование линии (по Брезенхэму)
Исходник: Пример кода, рисование линии по Брезенхэму [C++, code #131, hits: 5745, рейтинг: 3/7,4.84(2468)] +
автор: - [добавлен: 17.05.2006] управление:
  1. #include <graphics.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <math.h>
  6. // this function initializes graphics mode
  7. // it will work only if you're using Borland C++ compiler & BGI drivers
  8. // if you're using another compiler you should overwrite body of this function
  9. void init_gr(void)
  10. {
  11. /* request autodetection */
  12. int gdriver = DETECT, gmode, errorcode;
  13. /* initialize graphics mode */
  14. initgraph(&gdriver, &gmode, "");
  15. /* read result of initialization */
  16. errorcode = graphresult();
  17. if (errorcode != grOk) /* an error occurred */
  18. {
  19. printf("Graphics error: %s\n", grapherrormsg(errorcode));
  20. printf("Press any key to halt:");
  21. getch();
  22. exit(1); /* return with error code */
  23. }
  24. }
  25. // this function shuts graphics mode down
  26. // it will work only if you're using Borland C++ compiler & BGI drivers
  27. // if you're using another compiler you should overwrite body of this function
  28. void end_gr(void)
  29. {
  30. closegraph();
  31. }
  32. // this function puts pixel on the screen in (x,y) position using color 'color'
  33. // it will work only if you're using Borland C++ compiler & BGI drivers
  34. // if you're using another compiler you should overwrite body of this function
  35. void PutPixel(int x, int y, int color)
  36. {
  37. putpixel(x,y,color);
  38. }
  39. void BLine(int x1,int y1,int x2, int y2, int color)
  40. {
  41. int delta_x,delta_y,incx,incy,t,distance;
  42. int xerr=0,yerr=0;
  43. // determine dx and dy
  44. delta_x=x2-x1;
  45. delta_y=y2-y1;
  46. // determine steps by x and y axes (it will be +1 if we move in forward
  47. // direction and -1 if we move in backward direction
  48. if (delta_x>0) incx=1;
  49. else if (delta_x==0) incx=0;
  50. else incx=-1;
  51. if (delta_y>0) incy=1;
  52. else if (delta_y==0) incy=0;
  53. else incy=-1;
  54. delta_x=abs(delta_x);
  55. delta_y=abs(delta_y);
  56. // select largest from deltas and use it as a main distance
  57. if (delta_x>delta_y) distance=delta_x;
  58. else distance=delta_y;
  59. for (t=0;t<=distance+1;t++)
  60. {
  61. PutPixel(x1,y1,color);
  62. // increasing error
  63. xerr+=delta_x;
  64. yerr+=delta_y;
  65. // if error is too big then we should decrease it by changing
  66. // coordinates of the next plotting point to make it closer
  67. // to the true line
  68. if(xerr>distance)
  69. {
  70. xerr-=distance;
  71. x1+=incx;
  72. }
  73. if (yerr>distance)
  74. {
  75. yerr-=distance;
  76. y1+=incy;
  77. }
  78. }
  79. }
  80. int main(void)
  81. {
  82. // initializing graphics mode
  83. init_gr();
  84. /* examples */
  85. BLine(0, 0, 640, 480,15);
  86. BLine(320, 0, 320, 480,10);
  87. BLine(0, 240, 640, 240,11);
  88. BLine(0, 480, 640, 0,12);
  89. BLine(320, 11, 10, 5,13);
  90. BLine(320, 11, 630, 5,13);
  91. /* clean up */
  92. getch();
  93. end_gr();
  94.  
  95. return 0;
  96. }
//////////////////////////////////////////////////////////////////////////////
//
// Bresengham line drawing
// (c) Johna Smith, 1996
//
// Method description:
// Determine dy=(y2-y1), dx=(x2-x1)
// We plot first point of the line and increase errors of plotting
// (xerr and yerr) by dx and dy respectively. If the error is greater
// than largest from dx and dy then we should correct next point and
// shift it to 1 pixel by that axe where error was too big.
//
// ! This program is NOT optimized for best performance !
// To do so don't use putpixel function - change bytes in video memory directly.
//
//////////////////////////////////////////////////////////////////////////////

+добавить реализацию
 
каталог | задачи | паттерны | исходники | стат | форумы | карта сайта | контакты | ссылки 
© 2000-2018 CodeLAB Group
  Все права защищены
Страница сгенерирована за 0.002747 секунд
Количество запросов к БД: 6, gzip: 4.7kb/21.5kb(79%)