<< | к задаче | главная | печатать | обсудить(0 сообщений) >>
Задача: Рисование куба
Исходник: Пример кода рисования куба, рисование куба [C++, code #127, hits: 5777, рейтинг: 3/5,4.82(2400)] +
автор: - [добавлен: 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. // drawing
  81. if((object+i)->action==move)
  82. MoveTo(x,y);
  83. else
  84. LineTo(x,y);
  85. }
  86. }
  87. int main(void)
  88. {
  89. const int n=4; // number of cubes segments +1
  90. Point3D cube[15*n]; // coordinates for cubes points
  91. float rho=1900,theta=Pi/3,phi=2*Pi/3,dist_to_screen=600; // view point
  92. int xshift=300, yshift=140; // picture offset
  93. float side=300; // cubes parameters
  94. float delta;// auxulary variable
  95. // initializing graphics mode
  96. init_gr();
  97. // generating cube
  98. delta=side/(n-1);
  99. for (int i=0;i<n;i++)
  100. {
  101. for(int j=i*5;j<i*5+5;j++)
  102. {
  103. cube[j].x=i*delta;
  104. cube[j].action=draw;
  105. }
  106. cube[i*5].y=0;
  107. cube[i*5].z=0;
  108. cube[i*5].action=move;
  109. cube[i*5+1].y=side;
  110. cube[i*5+1].z=0;
  111. cube[i*5+2].y=side;
  112. cube[i*5+2].z=side;
  113. cube[i*5+3].y=0;
  114. cube[i*5+3].z=side;
  115. cube[i*5+4].y=0;
  116. cube[i*5+4].z=0;
  117. }
  118. int c=5*n;
  119. for (i=0;i<n;i++)
  120. {
  121. for(int j=i*5;j<i*5+5;j++)
  122. {
  123. cube[c+j].y=i*delta;
  124. cube[c+j].action=draw;
  125. }
  126. cube[c+i*5].x=0;
  127. cube[c+i*5].z=0;
  128. cube[c+i*5].action=move;
  129. cube[c+i*5+1].x=side;
  130. cube[c+i*5+1].z=0;
  131. cube[c+i*5+2].x=side;
  132. cube[c+i*5+2].z=side;
  133. cube[c+i*5+3].x=0;
  134. cube[c+i*5+3].z=side;
  135. cube[c+i*5+4].x=0;
  136. cube[c+i*5+4].z=0;
  137. }
  138. c=10*n;
  139. for (i=0;i<n;i++)
  140. {
  141. for(int j=i*5;j<i*5+5;j++)
  142. {
  143. cube[c+j].z=i*delta;
  144. cube[c+j].action=draw;
  145. }
  146. cube[c+i*5].y=0;
  147. cube[c+i*5].x=0;
  148. cube[c+i*5].action=move;
  149. cube[c+i*5+1].y=side;
  150. cube[c+i*5+1].x=0;
  151. cube[c+i*5+2].y=side;
  152. cube[c+i*5+2].x=side;
  153. cube[c+i*5+3].y=0;
  154. cube[c+i*5+3].x=side;
  155. cube[c+i*5+4].y=0;
  156. cube[c+i*5+4].x=0;
  157. }
  158. // drawing
  159. draw3Dobject(cube,15*n,rho,theta,phi,dist_to_screen,xshift,yshift);
  160. /* clean up */
  161. getch();
  162. end_gr();
  163. return 0;
  164. }
//////////////////////////////////////////////////////////////////////////////
//
// Generating and drawing cube
// (c) Johna Smith, 1996
//
// Method description:
// Cube is one of simplest figures in stereometry.
// We just need to generate 'n' squares parallel to X,Y and Z axes
//
//////////////////////////////////////////////////////////////////////////////

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