Задача: Вращение фигуры в плоскости
Исходник: Вращение фигуры в плоскости, язык: C++ [code #130, hits: 11501]
автор: - [добавлен: 17.05.2006]
  1. #include <graphics.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <math.h>
  6. #define Pi 3.1415926536
  7. // this function initializes graphics mode
  8. // it will work only if you're using Borland C++ compiler & BGI drivers
  9. // if you're using another compiler you should overwrite body of this function
  10. void init_gr(void)
  11. {
  12. /* request autodetection */
  13. int gdriver = DETECT, gmode, errorcode;
  14. /* initialize graphics mode */
  15. initgraph(&gdriver, &gmode, "");
  16. /* read result of initialization */
  17. errorcode = graphresult();
  18. if (errorcode != grOk) /* an error occurred */
  19. {
  20. printf("Graphics error: %s\n", grapherrormsg(errorcode));
  21. printf("Press any key to halt:");
  22. getch();
  23. exit(1); /* return with error code */
  24. }
  25. }
  26. // this function shuts graphics mode down
  27. // it will work only if you're using Borland C++ compiler & BGI drivers
  28. // if you're using another compiler you should overwrite body of this function
  29. void end_gr(void)
  30. {
  31. closegraph();
  32. }
  33. // this function moves CP to (x,y) position
  34. // it will work only if you're using Borland C++ compiler & BGI drivers
  35. // if you're using another compiler you should overwrite body of this function
  36. void MoveTo(int x, int y)
  37. {
  38. moveto(x,y);
  39. }
  40. // this function draws a line to (x,y) position
  41. // it will work only if you're using Borland C++ compiler & BGI drivers
  42. // if you're using another compiler you should overwrite body of this function
  43. void LineTo(int x, int y)
  44. {
  45. lineto(x,y);
  46. }
  47. const N=6; // number of points in the figure
  48. // coordinates of all given points
  49. enum actions {MOVE,DRAW};
  50. struct
  51. {
  52. actions action;
  53. int x;
  54. int y;
  55. } figure[N]={{MOVE,360,270},{DRAW,360,260},{DRAW,355,260},{DRAW,360,250},
  56. {DRAW,365,260},{DRAW,360,260}};
  57. int x0,y0,dx,dy;
  58. float phi;
  59. int main(void)
  60. {
  61. // initializing graphics mode
  62. init_gr();
  63. // rotating about (x0,y0)
  64. x0=300;
  65. y0=260;
  66. // by 10 degrees
  67. phi=45.0*Pi/180.0;
  68. // main loop
  69. for(int i=0;i<8;i++)
  70. {
  71. // rotating the figure
  72. for (int j=0;j<N;j++)
  73. {
  74. dx=figure[j].x-x0;
  75. dy=figure[j].y-y0;
  76. figure[j].x=x0+dx*cos(phi)-dy*sin(phi);
  77. figure[j].y=y0+dx*sin(phi)+dy*cos(phi);
  78. }
  79. // drawing rotated figure
  80. for (j=0;j<N;j++)
  81. if (figure[j].action==MOVE)
  82. MoveTo(figure[j].x,figure[j].y);
  83. else
  84. LineTo(figure[j].x,figure[j].y);
  85. }
  86. // clean up
  87. getch();
  88. end_gr();
  89. return 0;
  90. }
//////////////////////////////////////////////////////////////////////////////
//
// Rotations in 2 dimensions
// (c) Johna Smith, 1996
//
// Method description:
// There is the following formulas for rotating in 2 dimensions:
// x'=x0+(x-x0)*cos(phi)-(y-y0)*sin(phi)
// y'=y0+(x-x0)*sin(phi)+(y-y0)*cos(phi)
// To rotate the figure we should rotate each of its points
//
//////////////////////////////////////////////////////////////////////////////

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