Pyramid.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. namespace EE{
  4. /******************************************************************************/
  5. Flt Pyramid::area()C
  6. {
  7. Flt a=side();
  8. return a*(a + 2*Dist(a*0.5f, h));
  9. }
  10. Flt Pyramid::volume()C
  11. {
  12. return Sqr(side())*h/3;
  13. }
  14. Pyramid& Pyramid::operator*=(Flt f)
  15. {
  16. pos*=f;
  17. h *=f;
  18. return T;
  19. }
  20. Pyramid& Pyramid::operator/=(Flt f)
  21. {
  22. pos/=f;
  23. h /=f;
  24. return T;
  25. }
  26. Pyramid& Pyramid::operator*=(C Matrix3 &m)
  27. {
  28. OrientP &ornp=T;
  29. Flt f=m.maxScale();
  30. ornp*=m; ornp.fix();
  31. h *=f;
  32. return T;
  33. }
  34. Pyramid& Pyramid::operator*=(C Matrix &m)
  35. {
  36. OrientP &ornp=T;
  37. Flt f=m.maxScale();
  38. ornp*=m; ornp.fix();
  39. h *=f;
  40. return T;
  41. }
  42. /******************************************************************************/
  43. void Pyramid::toCorners(Vec (&v)[5])C
  44. {
  45. Vec x =cross()*(scale*h),
  46. y =perp *(scale*h),
  47. z =dir * h,
  48. center=pos + z;
  49. v[0]=pos;
  50. v[1]=center-x+y;
  51. v[2]=center+x+y;
  52. v[3]=center+x-y;
  53. v[4]=center-x-y;
  54. }
  55. /******************************************************************************/
  56. void Pyramid::draw(Color color, Bool fill)C
  57. {
  58. Vec v[5]; toCorners(v);
  59. VI.color(color);
  60. if(fill)
  61. {
  62. VI.tri(v[0], v[1], v[2]);
  63. VI.tri(v[0], v[2], v[3]);
  64. VI.tri(v[0], v[3], v[4]);
  65. VI.tri(v[0], v[4], v[1]);
  66. VI.tri(v[1], v[2], v[4]);
  67. VI.tri(v[4], v[2], v[3]);
  68. }else
  69. {
  70. VI.line(v[0], v[1]);
  71. VI.line(v[0], v[2]);
  72. VI.line(v[0], v[3]);
  73. VI.line(v[0], v[4]);
  74. VI.line(v[1], v[2]);
  75. VI.line(v[2], v[3]);
  76. VI.line(v[3], v[4]);
  77. VI.line(v[4], v[1]);
  78. }
  79. VI.end();
  80. }
  81. /******************************************************************************/
  82. Bool Cuts(C Vec &point, C Pyramid &pyramid)
  83. {
  84. Vec d=point-pyramid.pos;
  85. Flt z=DistPointPlane(d, pyramid.dir);
  86. if( z>=0 && z<=pyramid.h)
  87. {
  88. z*=pyramid.scale;
  89. if(Abs(DistPointPlane(d, pyramid.perp ))<=z // check 'perp' first because unlike 'cross', it doesn't require calculations
  90. && Abs(DistPointPlane(d, pyramid.cross()))<=z)return true;
  91. }
  92. return false;
  93. }
  94. Bool Cuts(C VecD &point, C Pyramid &pyramid)
  95. {
  96. Vec d=point-pyramid.pos; // no need for 'VecD'
  97. Flt z=DistPointPlane(d, pyramid.dir);
  98. if( z>=0 && z<=pyramid.h)
  99. {
  100. z*=pyramid.scale;
  101. if(Abs(DistPointPlane(d, pyramid.perp ))<=z // check 'perp' first because unlike 'cross', it doesn't require calculations
  102. && Abs(DistPointPlane(d, pyramid.cross()))<=z)return true;
  103. }
  104. return false;
  105. }
  106. Bool Cuts(C VecD &point, C PyramidM &pyramid)
  107. {
  108. Vec d=point-pyramid.pos; // no need for 'VecD'
  109. Flt z=DistPointPlane(d, pyramid.dir);
  110. if( z>=0 && z<=pyramid.h)
  111. {
  112. z*=pyramid.scale;
  113. if(Abs(DistPointPlane(d, pyramid.perp ))<=z // check 'perp' first because unlike 'cross', it doesn't require calculations
  114. && Abs(DistPointPlane(d, pyramid.cross()))<=z)return true;
  115. }
  116. return false;
  117. }
  118. /******************************************************************************/
  119. }
  120. /******************************************************************************/