Advertisement
houseofmath

Common Tangents

Oct 2nd, 2024 (edited)
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Asymptote 2.28 KB | Source Code | 0 0
  1. import geometry;
  2. import patterns;
  3. size(9cm,0);
  4.  
  5. line[] tan2cir(circle c1, circle c2){ line[] lines;
  6.   if ( c1.C == c2.C && c1.r == c2.r ) { label( "Ups, circles are equal!", c1.C ); }
  7.   if ( c1.C != c2.C ) {
  8.   line[] n = { line(c1.C, c2.C) + unit(c2.C-c1.C) * dir(90) * c1.r, line(c1.C, c2.C) + unit(c1.C-c2.C) * dir(90) * c1.r };
  9.   lines.append( n );
  10.   if ( c1.r != c2.r ) { lines = tangents( circle( c1.C, c1.r ), scale( c2.r / (c2.r - c1.r), c2.C) * c1.C ); }
  11.   lines.append( tangents( circle( c1.C, c1.r ), scale( c2.r / (c2.r + c1.r), c2.C) * c1.C ) ); }
  12.   return lines;
  13. }
  14.  
  15. /*
  16. // Код для проверки работоспособности
  17. real r = 3, R = 8, L = r + R + 4;
  18. // real L = 0; // для совпадения центров окружностей
  19. point O1 = (2,3), O2 = O1 + L * dir( 120 );
  20.  
  21. circle c1 = circle( O1, r );
  22. circle c2 = circle( O2, R );
  23. line[] out = tan2cir( c1, c2 );
  24.  
  25. draw( c1^^c2 );
  26. draw( out );
  27. label( "number of tangents is " + string( out.length ), c1.C, S ); // Размер массива касательных
  28. // Нерабочий код
  29. line[] tan2cir(circle c1, circle c2){ line[] lines;
  30.   if ( c1.C == c2.C && c1.r == c2.r ) { label( "Ups, circles are equal!", c1.C ); }
  31.   else { lines = { line(c1.C, c2.C) + unit(c2.C-c1.C) * dir(90) * c1.r, line(c1.C, c2.C) + unit(c1.C-c2.C) * dir(90) * c1.r };
  32.   if ( c1.r != c2.r ) { lines = tangents( circle( c1.C, c1.r ), scale( c2.r / (c2.r - c1.r), c2.C) * c1.C ); }
  33.   lines.append( tangents( circle( c1.C, c1.r ), scale( c2.r / (c2.r + c1.r), c2.C) * c1.C ) ); }
  34.   return lines;
  35. }
  36.  
  37. // Старый код, для сравнения прогресса
  38. line[] tan2cir(point O1, real r1, point O2, real r2){
  39.   line[] lines;
  40.   circle c1 = circle( O1, r1 );
  41.   if ( r1 == r2 ) {
  42.     line per = parallel(O1, dir(90) * line(O1, O2) );
  43.     lines.push( parallel( intersectionpoints( c1, per )[0], line(O1, O2) ) );
  44.     lines.push( parallel( intersectionpoints( c1, per )[1], line(O1, O2) ) );
  45.   } else {
  46.     point A = (O2 + (O1 - O2) * ( r2 / (r2 - r1) ) );
  47.     lines.push( tangents(c1, A)[0] );
  48.     lines.push( tangents(c1, A)[1] );
  49.   }
  50.   point B = (O2 - O1) * r1 / (r1 + r2);
  51.   line[] tang = tangents( c1, B );
  52.   for ( int i = 0; i < tang.length; ++i ) { lines.push( tang[i] ); }
  53.   return lines;
  54. }
  55. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement