Задача: Рисование полусферы
Исходник: Рисование полусферы, язык: C++ [code #129, hits: 7970]
автор: - [добавлен: 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=10; // number of hemispheres segments
  90. Point3D hemisphere[8*n*n+n]; // coordinates for hemispheres points
  91. float rho=1800,theta=Pi,phi=3*Pi/4,dist_to_screen=600; // view point
  92. int xshift=300, yshift=250; // picture offset
  93. float delta=Pi/(2*n), R=300; // hemisphere parameters
  94. float alpha,cosa,sina,beta,cosb,sinb; // auxulary variables
  95. // initializing graphics mode
  96. init_gr();
  97. // generating semisphere
  98. for (int i=0;i<4*n;i++)
  99. {
  100. alpha=i*delta;
  101. cosa=cos(alpha);
  102. sina=sin(alpha);
  103. for (int j=0;j<n;j++)
  104. {
  105. beta=j*delta;
  106. sinb=sin(beta);
  107. cosb=cos(beta);
  108. hemisphere[i*n+j].x=R*cosa*sinb;
  109. hemisphere[i*n+j].y=R*sina*sinb;
  110. hemisphere[i*n+j].z=-R*cosb;
  111. hemisphere[i*n+j].action=(j==0?move:draw);
  112. }
  113. }
  114. int c=4*n*n;
  115. for (i=0;i<n;i++)
  116. {
  117. beta=i*delta;
  118. sinb=sin(beta);
  119. cosb=cos(beta);
  120. for (int j=0;j<4*n+1;j++)
  121. {
  122. alpha=j*delta;
  123. cosa=cos(alpha);
  124. sina=sin(alpha);
  125. hemisphere[c+i*(4*n+1)+j].x=R*cosa*sinb;
  126. hemisphere[c+i*(4*n+1)+j].y=R*sina*sinb;
  127. hemisphere[c+i*(4*n+1)+j].z=-R*cosb;
  128. hemisphere[c+i*(4*n+1)+j].action=(j==0?move:draw);
  129. }
  130. }
  131. // drawing
  132. draw3Dobject(hemisphere,8*n*n+n,rho,theta,phi,dist_to_screen,xshift,yshift);
  133. /* clean up */
  134. getch();
  135. end_gr();
  136. return 0;
  137. }
//////////////////////////////////////////////////////////////////////////////
//
// Generating and drawing semisphere
// (c) Johna Smith, 1996
//
// Method description:
// Hemisphere is described by following eqation:
// 2 2 2
// x + y + z = 1, -1<=z<=0
//
// By generating this points with step 'delta' we can approximate hemisphere
//
//////////////////////////////////////////////////////////////////////////////

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