<< | к задаче | главная | печатать | обсудить(0 сообщений) >>
Задача: Рисование линии
Исходник: Рисование линии [C, code #136, hits: 9140, рейтинг: 3/7,4.9(2847)] +
автор: - [добавлен: 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 Line(int x1,int y1,int x2, int y2, int color)
  40. {
  41. int delta_x,delta_y,incx,incy;
  42. float k;
  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 greatest from deltas and use it as a main axe
  57. if (delta_x>delta_y && delta_x!=0)
  58. {
  59. k=(float)delta_y/delta_x;
  60. for (int i=0;i<delta_x;i++) PutPixel(x1+incx*i,y1+incy*floor(k*i),color);
  61. }
  62. else
  63. {
  64. k=(float)delta_x/delta_y;
  65. for (int i=0;i<delta_y;i++) PutPixel(x1+incx*floor(k*i),y1+incy*i,color);
  66. }
  67. }
  68. int main(void)
  69. {
  70. // initializing graphics mode
  71. init_gr();
  72. /* examples */
  73. Line(0, 0, 640, 480,15);
  74. Line(320, 0, 320, 480,10);
  75. Line(0, 240, 640, 240,11);
  76. Line(0, 480, 640, 0,12);
  77. Line(320, 11, 10, 5,13);
  78. Line(320, 11, 630, 5,13);
  79. /* clean up */
  80. getch();
  81. end_gr();
  82. return 0;
  83. }
//////////////////////////////////////////////////////////////////////////////
//
// Line drawing
// (c) Johna Smith, 1996
//
// Method description:
// Determine k=dy/dx, dy=(y2-y1), dx=(x2-x1)
// Line equation is: y(x)=y1+x*k so we need only to plot all
// these points. This algorithm is very slow, because it use
// floating point math (requires coprocessor for best productivity)
// ! This program is NOT optimized for best performance !
// To do so don't use putpixel function - change bytes in video memory directly.
//
//////////////////////////////////////////////////////////////////////////////

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