Torus.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. namespace EE{
  4. /******************************************************************************/
  5. Torus& Torus::operator*=( Flt f) {pos*=f; R*=f; r*=f; return T;}
  6. Torus& Torus::operator/=( Flt f) {pos/=f; R/=f; r/=f; return T;}
  7. Torus& Torus::operator*=(C Matrix3 &m)
  8. {
  9. pos*=m;
  10. up *=m; up.normalize(); Flt l=m.maxScale();
  11. R *=l;
  12. r *=l;
  13. return T;
  14. }
  15. Torus& Torus::operator*=(C Matrix &m)
  16. {
  17. pos*=m;
  18. up *=m.orn(); up.normalize(); Flt l=m.maxScale();
  19. R *=l;
  20. r *=l;
  21. return T;
  22. }
  23. /******************************************************************************/
  24. void Torus::draw(C Color &color, Bool fill, VecI2 resolution)C
  25. {
  26. if(resolution.x<0)resolution.x=12;else if(resolution.x<3)resolution.x=3;
  27. if(resolution.y<0)resolution.y=12;else if(resolution.y<3)resolution.y=3;
  28. Matrix3 m; m.setUp(up);
  29. Vec2 cs;
  30. Vec d0=m.x;
  31. VI.color(color);
  32. REP(resolution.x)
  33. {
  34. CosSin(cs.x, cs.y, i*PI2/resolution.x);
  35. Vec d1=cs.x*m.x + cs.y*m.z,
  36. l0=pos + d0*(R+r),
  37. r0=pos + d1*(R+r);
  38. REPD(n2, resolution.y)
  39. {
  40. CosSin(cs.x, cs.y, n2*PI2/resolution.y);
  41. Vec l1=pos + d0*R + (cs.x*d0 + cs.y*m.y)*r,
  42. r1=pos + d1*R + (cs.x*d1 + cs.y*m.y)*r;
  43. if(fill)
  44. {
  45. VI.quad(l0, l1, r1, r0);
  46. }else
  47. {
  48. VI.line(l0, l1);
  49. VI.line(l0, r0);
  50. }
  51. l0=l1;
  52. r0=r1;
  53. }
  54. d0=d1;
  55. }
  56. VI.end();
  57. }
  58. /******************************************************************************/
  59. Flt Dist(C Vec &point, C Torus &torus)
  60. {
  61. Vec delta=point-torus.pos;
  62. Flt dist_point_plane=DistPointPlane(delta, torus.up);
  63. Vec nearest=delta-torus.up*dist_point_plane; // PointOnPlane(delta, torus.up);
  64. Flt dist;
  65. if(nearest.setLength(torus.R))dist=Dist(delta, nearest);
  66. else dist=Dist(torus.R, dist_point_plane); // point is located exactly on the axis of rotation (pos=torus.pos, dir=torus.up)
  67. return Max(dist-torus.r, 0);
  68. }
  69. /******************************************************************************/
  70. }
  71. /******************************************************************************/