Guest User

Untitled

a guest
Jun 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. settings.render = 4;
  2. settings.outformat = "eps";
  3.  
  4. // import modules --------------------------------------------------------------
  5. import graph;
  6. import three;
  7. import solids;
  8.  
  9. // overall settings ------------------------------------------------------------
  10. currentprojection = orthographic(0,0,6);
  11. viewportmargin = (10,10);
  12. size(10cm);
  13. currentlight = ((3,3,0));
  14.  
  15. // -----------------------------------------------------------------------------
  16. struct circle {
  17. pair center;
  18. real radius;
  19. }
  20.  
  21. // radius of inner Soddy circle ------------------------------------------------
  22. real soddyRadius(real r1, real r2, real r3){
  23. return 1/(1/r1+1/r2+1/r3+2*sqrt(1/r1/r2+1/r2/r3+1/r3/r1));
  24. }
  25.  
  26. // center of inner Soddy circle ------------------------------------------------
  27. pair soddyCenter(pair A, pair B, pair C){
  28. pair AB = B-A;
  29. pair AC = C-A;
  30. real a = length(C-B); real b = length(AC); real c = length(AB);
  31. // area of triangle ABC:
  32. real Delta = 0.5*abs(AB.x*AC.y-AB.y*AC.x);
  33. // triangular coordinates:
  34. real tc1 = 1 + 2*Delta/(a*(b+c-a));
  35. real tc2 = 1 + 2*Delta/(b*(c+a-b));
  36. real tc3 = 1 + 2*Delta/(c*(a+b-c));
  37. //
  38. real den = a*tc1 + b*tc2 + c*tc3;
  39. real k1 = a*tc1/den; real k2 = b*tc2/den; real k3 = c*tc3/den;
  40. return k1*A + k2*B + k3*C;
  41. }
  42.  
  43. // inner Soddy circle ----------------------------------------------------------
  44. circle soddyCircle1(circle C1, circle C2, circle C3){
  45. circle out;
  46. out.radius = soddyRadius(C1.radius, C2.radius, C3.radius);
  47. out.center = soddyCenter(C1.center, C2.center, C3.center);
  48. return out;
  49. }
  50.  
  51. // case of an exterior center C3 -----------------------------------------------
  52. circle soddyCircle2(circle C1, circle C2, circle C3){
  53. real r1 = C1.radius; real r2 = C2.radius; real r3 = -C3.radius;
  54. real r = soddyRadius(r1,r2,r3);
  55. pair z1 = C1.center; pair z2 = C2.center; pair z3 = C3.center;
  56. pair term1 = 1/r1*z1 + 1/r2*z2 + 1/r3*z3;
  57. pair term2 = 2*sqrt(1/r1/r2*z1*z2 + 1/r2/r3*z2*z3 + 1/r1/r3*z1*z3);
  58. pair center1 = r*(term1-term2);
  59. pair center2 = r*(term1+term2);
  60. real d1 = (center1.x-z3.x)^2 + (center1.y-z3.y)^2;
  61. real d2 = (center2.x-z3.x)^2 + (center2.y-z3.y)^2;
  62. circle out;
  63. out.radius = r;
  64. if(d1>d2){
  65. out.center = center1;
  66. }else{
  67. out.center = center2;
  68. }
  69. return out;
  70. }
  71.  
  72. // starting circles ------------------------------------------------------------
  73. circle C1; C1.center = (1,-1/sqrt(3)); C1.radius = 1;
  74. circle C2; C2.center = (-1,-1/sqrt(3)); C2.radius = 1;
  75. circle C3; C3.center = (0,sqrt(3)-1/sqrt(3)); C3.radius = 1;
  76. circle C4; C4.center = (0,0); C4.radius = 2+soddyRadius(1,1,1);
  77.  
  78. // main function ---------------------------------------------------------------
  79. void Apollony(circle C1, circle C2, circle C3, int n, bool exterior){
  80. circle Cnew;
  81. if(exterior){
  82. Cnew = soddyCircle2(C1,C2,C3);
  83. }else{
  84. Cnew = soddyCircle1(C1,C2,C3);
  85. }
  86. draw(surface(sphere((Cnew.center.x,Cnew.center.y,0), Cnew.radius)), red);
  87. if(n>1){
  88. Apollony(Cnew, C2, C3, n-1, exterior);
  89. Apollony(C1, Cnew, C3, n-1, exterior);
  90. Apollony(C1, C2, Cnew, n-1, false);
  91. }
  92. }
  93.  
  94. // plot ------------------------------------------------------------------------
  95. int n = 6;
  96. Apollony(C1, C2, C3, n, false);
  97. Apollony(C1, C2, C4, n, true);
  98. Apollony(C1, C3, C4, n, true);
  99. Apollony(C2, C3, C4, n, true);
Add Comment
Please, Sign In to add comment