Cone.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. namespace EE{
  4. /******************************************************************************/
  5. Flt Cone::area()C
  6. {
  7. Flt min, max; MinMax(r_low, r_high, min, max);
  8. if(min== 0)return PI *max*(max + Dist(max, h));
  9. if(min==max)return PI2*max*(max + h);
  10. Flt h0=h*min/(max-min);
  11. return PI*(max*(max + Dist(max, h0+h))
  12. +min*(min - Dist(min, h0 )));
  13. }
  14. Flt Cone::volume()C
  15. {
  16. Flt min, max; MinMax(r_low, r_high, min, max);
  17. if(min== 0)return PI_3*max*max*h;
  18. if(min==max)return PI *max*max*h;
  19. Flt h0=h*min/(max-min);
  20. return PI_3*(max*max*(h0+h) - min*min*h0);
  21. }
  22. Cone& Cone::operator*=(Flt f)
  23. {
  24. r_low *=f;
  25. r_high*=f;
  26. h *=f;
  27. pos *=f;
  28. return T;
  29. }
  30. Cone& Cone::operator/=(Flt f)
  31. {
  32. r_low /=f;
  33. r_high/=f;
  34. h /=f;
  35. pos /=f;
  36. return T;
  37. }
  38. Cone& Cone::operator*=(C Matrix3 &m)
  39. {
  40. Flt s=m.maxScale();
  41. pos *=m;
  42. up *=m;
  43. h *=up.normalize();
  44. r_low *=s;
  45. r_high*=s;
  46. return T;
  47. }
  48. Cone& Cone::operator*=(C Matrix &m)
  49. {
  50. Flt s=m.maxScale();
  51. pos *=m;
  52. up *=m.orn();
  53. h *=up.normalize();
  54. r_low *=s;
  55. r_high*=s;
  56. return T;
  57. }
  58. /******************************************************************************/
  59. void Cone::draw(C Color &color, Bool fill, Int resolution)C
  60. {
  61. if(resolution<0)resolution=24;else if(resolution<3)resolution=3;
  62. Matrix3 matrix; matrix.setUp(up); matrix.y*=h;
  63. Vec prev0=pos+ r_low *matrix.x,
  64. prev1=pos+matrix.y+r_high*matrix.x;
  65. VI.color(color);
  66. REP(resolution)
  67. {
  68. Flt c, s; CosSin(c, s, (PI2*i)/resolution);
  69. Vec next0=pos+ (c*r_low )*matrix.x+(s*r_low )*matrix.z,
  70. next1=pos+matrix.y+(c*r_high)*matrix.x+(s*r_high)*matrix.z;
  71. if(fill)VI.quad(next1, prev1, prev0, next0);else
  72. {
  73. VI.line(next1, prev1);
  74. VI.line(next0, next1);
  75. VI.line(next0, prev0);
  76. }
  77. prev0=next0;
  78. prev1=next1;
  79. }
  80. VI.end();
  81. }
  82. /******************************************************************************/
  83. Bool Cuts(C Vec &point, C Cone &cone)
  84. {
  85. if(cone.h>0)
  86. {
  87. Flt z=DistPointPlane(point, cone.pos, cone.up);
  88. if( z>=0 && z<=cone.h)
  89. {
  90. Vec on_line=cone.pos+cone.up*z;
  91. Flt dist2 =Dist2(point, on_line),
  92. r2 =Sqr (Lerp(cone.r_low, cone.r_high, z/cone.h));
  93. return dist2<=r2;
  94. }
  95. }
  96. return false;
  97. }
  98. /******************************************************************************/
  99. }
  100. /******************************************************************************/