PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/S3DRect.cpp

https://github.com/AVGP/Simple3D
C++ | 155 lines | 129 code | 19 blank | 7 comment | 16 complexity | ecbc31e68ec5415795f4b15dbf9cdcf1 MD5 | raw file
  1. #include "S3DRect.h"
  2. S3DRect::S3DRect(S3DPoint points[4])
  3. {
  4. isFilled = false;
  5. color = RGB(255,255,255);
  6. for(int i=0;i<4;i++) vertices[i] = points[i];
  7. }
  8. S3DRect::S3DRect(S3DPoint a, S3DPoint b,S3DPoint c,S3DPoint d)
  9. {
  10. isFilled = false;
  11. color = RGB(255,255,255);
  12. vertices[0] = a;
  13. vertices[1] = b;
  14. vertices[2] = c;
  15. vertices[3] = d;
  16. }
  17. void S3DRect::move(double dx,double dy,double dz)
  18. {
  19. for(int i=0;i<4;i++)
  20. {
  21. vertices[i].x += dx;
  22. vertices[i].y += dy;
  23. vertices[i].z += dz;
  24. }
  25. }
  26. void S3DRect::rotate(double rx,double ry,double rz,S3DPoint *anchor)
  27. {
  28. //Um den Ursprung positionieren
  29. double dx = 0,dy = 0,dz = 0;
  30. if(anchor != NULL)
  31. {
  32. //Um den Ankerpunkt positionieren
  33. dx = -1.0 * anchor->x;
  34. dy = -1.0 * anchor->y;
  35. dz = -1.0 * anchor->z;
  36. }
  37. else
  38. {
  39. dx = -1.0 * (vertices[0].x + vertices[1].x + vertices[2].x + vertices[3].x)/4.0;
  40. dy = -1.0 * (vertices[0].y + vertices[1].y + vertices[2].y + vertices[3].y)/4.0;
  41. dz = -1.0 * (vertices[0].z + vertices[1].z + vertices[2].z + vertices[3].z)/4.0;
  42. }
  43. for(int i=0;i<4;i++)
  44. {
  45. vertices[i].x += dx;
  46. vertices[i].y += dy;
  47. vertices[i].z += dz;
  48. }
  49. //Drehen
  50. //Um Z-Achse
  51. double angle_z = rz * PI/180.0;
  52. for(int i=0;i<4;i++)
  53. {
  54. double o_x = vertices[i].x;
  55. double o_y = vertices[i].y;
  56. vertices[i].x = cos(angle_z) * o_x - sin(angle_z) * o_y;
  57. vertices[i].y = sin(angle_z) * o_x + cos(angle_z) * o_y;
  58. }
  59. //Um Y-Achse
  60. double angle_y = ry * PI/180.0;
  61. for(int i=0;i<4;i++)
  62. {
  63. double o_x = vertices[i].x;
  64. double o_z = vertices[i].z;
  65. vertices[i].z = cos(angle_y) * o_z - sin(angle_y) * o_x;
  66. vertices[i].x = sin(angle_y) * o_z + cos(angle_y) * o_x;
  67. }
  68. //Um X-Achse
  69. double angle_x = rx * PI/180.0;
  70. for(int i=0;i<4;i++)
  71. {
  72. double o_y = vertices[i].y;
  73. double o_z = vertices[i].z;
  74. vertices[i].z = cos(angle_x) * o_z - sin(angle_x) * o_y;
  75. vertices[i].y = sin(angle_x) * o_z + cos(angle_x) * o_y;
  76. }
  77. //Zurückverschieben
  78. for(int i=0;i<4;i++)
  79. {
  80. vertices[i].x -= dx;
  81. vertices[i].y -= dy;
  82. vertices[i].z -= dz;
  83. }
  84. }
  85. void S3DRect::draw(S3DDevice *d,S3DSurface w,S3DContext g)
  86. {
  87. XSetForeground(d,g,color);
  88. for(int i=0;i<4;i++)
  89. {
  90. XDrawLine(d,w,g,
  91. PLANAR_DISTANCE*(vertices[i].x/vertices[i].z),
  92. PLANAR_DISTANCE*(vertices[i].y/vertices[i].z),
  93. PLANAR_DISTANCE*(vertices[(i+1)%4].x/vertices[(i+1)%4].z),
  94. PLANAR_DISTANCE*(vertices[(i+1)%4].y/vertices[(i+1)%4].z));
  95. }
  96. if(isFilled)
  97. {
  98. XPoint p[4];
  99. for(int i=0;i<4;i++)
  100. {
  101. p[i].x = PLANAR_DISTANCE*(vertices[i].x/vertices[i].z);
  102. p[i].y = PLANAR_DISTANCE*(vertices[i].y/vertices[i].z);
  103. }
  104. XFillPolygon(d,w,g,p,4,Convex,CoordModeOrigin);
  105. }
  106. }
  107. void S3DRect::setColor(unsigned long c)
  108. {
  109. color = c;
  110. }
  111. unsigned long S3DRect::getColor()
  112. {
  113. return color;
  114. }
  115. S3DPoint *S3DRect::getPoints()
  116. {
  117. return vertices;
  118. }
  119. double S3DRect::getZ()
  120. {
  121. double minZ,maxZ;
  122. minZ = vertices[0].z;
  123. maxZ = vertices[0].z;
  124. for(int i=1;i<4;i++)
  125. {
  126. if(vertices[i].z < minZ)
  127. {
  128. minZ = vertices[i].z;
  129. }
  130. else if(vertices[i].z > maxZ)
  131. {
  132. maxZ = vertices[i].z;
  133. }
  134. }
  135. return (maxZ-minZ/2);
  136. }