Задача: Рисование прямоугольника
Исходник: Рисование прямоугольника, язык: C++ [code #137, hits: 7327]
автор: - [добавлен: 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. // determine dx and dy
  43. delta_x=x2-x1;
  44. delta_y=y2-y1;
  45. // determine steps by x and y axes (it will be +1 if we move in forward
  46. // direction and -1 if we move in backward direction
  47. if (delta_x>0) incx=1;
  48. else if (delta_x==0) incx=0;
  49. else incx=-1;
  50. if (delta_y>0) incy=1;
  51. else if (delta_y==0) incy=0;
  52. else incy=-1;
  53. delta_x=abs(delta_x);
  54. delta_y=abs(delta_y);
  55. // select greatest from deltas and use it as a main axe
  56. if (delta_x!=0)
  57. for (int i=0;i<delta_x;i++) PutPixel(x1+incx*i,y1,color);
  58. else
  59. for (int i=0;i<delta_y;i++) PutPixel(x1,y1+incy*i,color);
  60. }
  61. // this function draws a rectangle
  62. void Rectangle(int x1, int y1, int x2, int y2, int color)
  63. {
  64. Line(x1,y1,x1,y2,color);
  65. Line(x1,y2,x2,y2,color);
  66. Line(x2,y2,x2,y1,color);
  67. Line(x2,y1,x1,y1,color);
  68. }
  69. int main(void)
  70. {
  71. // initializing graphics mode
  72. init_gr();
  73. /* examples */
  74. Rectangle(0,0,639,479,10);
  75. Rectangle(100,200,300,400,11);
  76. Rectangle(600,100,20,11,12);
  77. /* clean up */
  78. getch();
  79. end_gr();
  80. return 0;
  81. }
//////////////////////////////////////////////////////////////////////////////
//
// Rectangle drawing
// (c) Johna Smith, 1996
//
// Method description:
// Draw four lines, using simplified line drawing method, cause we know
// that only vertical and horizontal lines will be drawn.
// ! This program is NOT optimized for best performance !
// To do so don't use putpixel function - change bytes in video memory directly.
//
//////////////////////////////////////////////////////////////////////////////

+добавить реализацию