Задача: Рисование тора
Исходник: Рисование тора, язык: C++ [code #128, hits: 12261]
автор: - [добавлен: 17.05.2006]
  1. #include <graphics.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <math.h>
  6. #include <dos.h>
  7. #define Pi 3.1415926536
  8. enum Action{move,draw};
  9. struct Point3D
  10. {
  11. int x;
  12. int y;
  13. int z;
  14. Action action;
  15. };
  16. // this function initializes graphics mode
  17. // it will work only if you're using Borland C++ compiler & BGI drivers
  18. // if you're using another compiler you should overwrite body of this function
  19. void init_gr(void)
  20. {
  21. /* request autodetection */
  22. int gdriver = DETECT, gmode, errorcode;
  23. /* initialize graphics mode */
  24. initgraph(&gdriver, &gmode, "");
  25. /* read result of initialization */
  26. errorcode = graphresult();
  27. if (errorcode != grOk) /* an error occurred */
  28. {
  29. printf("Graphics error: %s\n", grapherrormsg(errorcode));
  30. printf("Press any key to halt:");
  31. getch();
  32. exit(1); /* return with error code */
  33. }
  34. }
  35. // this function shuts graphics mode down
  36. // it will work only if you're using Borland C++ compiler & BGI drivers
  37. // if you're using another compiler you should overwrite body of this function
  38. void end_gr(void)
  39. {
  40. closegraph();
  41. }
  42. // this function moves CP to (x,y) position
  43. // it will work only if you're using Borland C++ compiler & BGI drivers
  44. // if you're using another compiler you should overwrite body of this function
  45. void MoveTo(int x, int y)
  46. {
  47. moveto(x,y);
  48. }
  49. // this function draws a line to (x,y) position
  50. // it will work only if you're using Borland C++ compiler & BGI drivers
  51. // if you're using another compiler you should overwrite body of this function
  52. void LineTo(int x, int y)
  53. {
  54. lineto(x,y);
  55. }
  56. void draw3Dobject(Point3D *object, int N, float rho, float theta,
  57. float phi, float dist_to_screen, int xshift, int yshift)
  58. {
  59. int x,y;
  60. float xe,ye,ze,costh,sinph,cosph,sinth,v11,v12,v13,v21,v22,v32,v33,v23,v43;
  61. // calculating coefficients
  62. costh=cos(theta);
  63. sinth=sin(theta);
  64. cosph=cos(phi);
  65. sinph=sin(phi);
  66. v11=-sinth; v12=-cosph*costh; v13=-sinph*costh;
  67. v21=costh; v22=-cosph*sinth; v23=-sinph*sinth;
  68. v32=sinph; v33=-cosph;
  69. v43=rho;
  70. for (int i=0;i<N;i++)
  71. {
  72. // calculating eye coordinates
  73. xe=v11*(object+i)->x+v21*(object+i)->y;
  74. ye=v12*(object+i)->x+v22*(object+i)->y+v32*(object+i)->z;
  75. ze=v13*(object+i)->x+v23*(object+i)->y+v33*(object+i)->z+v43;
  76.  
  77. // calculating screen coordinates
  78. x=dist_to_screen*xe/ze+xshift;
  79. y=dist_to_screen*ye/ze+yshift;
  80.  
  81. // drawing
  82. if((object+i)->action==move)
  83. MoveTo(x,y);
  84. else
  85. LineTo(x,y);
  86. }
  87. }
  88. int main(void)
  89. {
  90. const int n=20; // number of torus' segments
  91. Point3D torus[2*n*(n+1)]; // coordinates for torus' points
  92. float rho=1800,theta=0,phi=3*Pi/4,dist_to_screen=600; // view point
  93. int xshift=300, yshift=250; // picture offset
  94. float delta=2.0*Pi/n, r=75, R=300; // torus' parameters
  95. float alpha,cosa,sina,beta,x; // auxulary variables
  96. // initializing graphics mode
  97. init_gr();
  98. // generating torus
  99. for (int i=0;i<n;i++)
  100. {
  101. alpha=i*delta;
  102. cosa=cos(alpha);
  103. sina=sin(alpha);
  104. for (int j=0;j<n+1;j++)
  105. {
  106. beta=j*delta;
  107. x=R+r*cos(beta);
  108. torus[i*(n+1)+j].x=cosa*x;
  109. torus[i*(n+1)+j].y=sina*x;
  110. torus[i*(n+1)+j].z=r*sin(beta);
  111. torus[i*(n+1)+j].action=((i==0 && j==0)?move:draw);
  112. }
  113. }
  114. int c=n*n+n;
  115. for (i=0;i<n;i++)
  116. {
  117. beta=i*delta;
  118. x=R+r*cos(beta);
  119. for (int j=0;j<n+1;j++)
  120. {
  121. alpha=j*delta;
  122. cosa=cos(alpha);
  123. sina=sin(alpha);
  124. torus[c+i*(n+1)+j].x=cosa*x;
  125. torus[c+i*(n+1)+j].y=sina*x;
  126. torus[c+i*(n+1)+j].z=r*sin(beta);
  127. torus[c+i*(n+1)+j].action=draw;
  128. }
  129. }
  130. // drawing
  131. draw3Dobject(torus,2*n*(n+1),rho,theta,phi,dist_to_screen,xshift,yshift);
  132. /* clean up */
  133. getch();
  134. end_gr();
  135. return 0;
  136. }
//////////////////////////////////////////////////////////////////////////////
//
// Generating and drawing torus
// (c) Johna Smith, 1996
//
// Method description:
// Torus has two main circles, which are described by
// two systems of eqations:
// x=Rcos(a) x=R+rcos(b)
// y=Rsin(a) y=0
// z=0 z=rsin(b)
// By generating this circles (approximating by polygons) we can get torus
//
//////////////////////////////////////////////////////////////////////////////

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