Guest User

Untitled

a guest
Oct 16th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. documentclass[crop,tikz,margin=10pt]{standalone}
  2. usepackage{tikz}
  3. usetikzlibrary{intersections}
  4. usetikzlibrary{spy}
  5. usepackage{pgfplots}
  6. begin{document}
  7. begin{tikzpicture}[
  8. spy using outlines = {circle,size=3cm,magnification=5,connect spies},
  9. ]
  10. begin{axis}
  11. addplot+[domain = 0:2*pi] expression {sin(deg(x))};
  12. coordinate (spy point) at (axis cs: 0, 0);
  13. coordinate (magnifying glass) at (rel axis cs: -0.4, 0.2);
  14. end{axis}
  15.  
  16. spy on (spy point) in node at (magnifying glass);
  17. end{tikzpicture}
  18. begin{tikzpicture}
  19. defradiusa{0.3}
  20. defradiusb{3}
  21.  
  22. defxa{5}
  23. defya{3}
  24.  
  25. defxb{0}
  26. defyb{0}
  27.  
  28. coordinate (magnifying glass) at (xa, ya);
  29. coordinate (spy point) at (xb, yb);
  30.  
  31. pgfmathsetmacroxp{(xb * radiusa - xa * radiusb) / (radiusa - radiusb)}
  32. pgfmathsetmacroyp{(yb * radiusa - ya * radiusb) / (radiusa - radiusb)}
  33. pgfmathsetmacrodistancea{sqrt((xp - xa) * (xp - xa) + (yp - ya) * (yp - ya) - radiusa * radiusa))}
  34. pgfmathsetmacrodistanceb{sqrt((xp - xb) * (xp - xb) + (yp - yb) * (yp - yb) - radiusb * radiusb))}
  35. pgfmathsetmacrodenoma{(xp - xa)*(xp - xa) + (yp - ya)*(yp - ya)}
  36. pgfmathsetmacrodenomb{(xp - xb)*(xp - xb) + (yp - yb)*(yp - yb)}
  37.  
  38. pgfmathsetmacroxc{(radiusa * radiusa * (xp - xa) + radiusa * (yp - ya) * distancea) / denoma + xa}
  39. pgfmathsetmacroyc{(radiusa * radiusa * (yp - ya) - radiusa * (xp - xa) * distancea) / denoma + ya}
  40.  
  41. pgfmathsetmacroxe{(radiusa * radiusa * (xp - xa) - radiusa * (yp - ya) * distancea) / denoma + xa}
  42. pgfmathsetmacroye{(radiusa * radiusa * (yp - ya) + radiusa * (xp - xa) * distancea) / denoma + ya}
  43.  
  44. pgfmathsetmacroxd{(radiusb * radiusb * (xp - xb) + radiusb * (yp - yb) * distanceb) / denomb + xb}
  45. pgfmathsetmacroyd{(radiusb * radiusb * (yp - yb) - radiusb * (xp - xb) * distanceb) / denomb + yb}
  46.  
  47. pgfmathsetmacroxf{(radiusb * radiusb * (xp - xb) - radiusb * (yp - yb) * distanceb) / denomb + xb}
  48. pgfmathsetmacroyf{(radiusb * radiusb * (yp - yb) + radiusb * (xp - xb) * distanceb) / denomb + yb}
  49.  
  50.  
  51. draw (magnifying glass) circle(radiusa);
  52. draw (spy point) circle(radiusb);
  53.  
  54. % draw (xa, ya) node[scale=3, green] {.};
  55. % draw (xb, yb) node[scale=3, green] {.};
  56. % draw (xp, yp) node[scale=3, blue] {.};
  57. % draw (xc, yc) node[scale=3, red] {.};
  58. % draw (xd, yd) node[scale=3, red] {.};
  59. % draw (xe, ye) node[scale=3, red] {.};
  60. % draw (xf, yf) node[scale=3, red] {.};
  61.  
  62. % draw (xa, ya) -- (xp, yp);
  63. % draw (xb, yb) -- (xp, yp);
  64.  
  65. draw (xc, yc) -- (xd, yd);
  66. draw (xe, ye) -- (xf, yf);
  67. end{tikzpicture}
  68. end{document}
  69.  
  70. documentclass[crop,tikz,margin=10pt]{standalone}
  71. usepackage{tikz}
  72. usetikzlibrary{intersections}
  73. usetikzlibrary{spy}
  74. usepackage{pgfplots}
  75. makeatletter
  76. newcommandxcoord[2][center]{{%
  77. pgfpointanchor{#2}{#1}%
  78. pgfmathparse{pgf@x/pgf@xx}%
  79. pgfmathprintnumber{pgfmathresult}%
  80. }}
  81. newcommandycoord[2][center]{{%
  82. pgfpointanchor{#2}{#1}%
  83. pgfmathparse{pgf@y/pgf@yy}%
  84. pgfmathprintnumber{pgfmathresult}%
  85. }}
  86. makeatother
  87. begin{document}
  88. begin{tikzpicture}
  89. begin{scope}[
  90. spy using outlines = {circle,size=3cm,magnification=5},
  91. ]
  92. begin{axis}
  93. addplot+[domain = 0:2*pi] expression {sin(deg(x))};
  94. coordinate (spy point) at (axis cs: 0, 0);
  95. coordinate (magnifying glass) at (rel axis cs: -0.4, 0.2);
  96. coordinate (a) at (rel axis cs: 0.5, 1.1);
  97. end{axis}
  98. node at (a) {spy point: xcoord{spy point}, ycoord{spy point}, glass: xcoord{magnifying glass}, ycoord{magnifying glass}};
  99.  
  100. spy on (spy point) in node at (magnifying glass);
  101. end{scope}
  102. coordinate (c) at (-1.6589690159337693, 0.10010961563788023);
  103. coordinate (d) at (0.7862061968132461, 2.6420219231275763);
  104. coordinate (e) at (-2.962541913303562, 2.6233998438799935);
  105. coordinate (f) at (0.5254916173392875, 3.1466799687759988);
  106. draw (c) -- (d);
  107. draw (e) -- (f);
  108. end{tikzpicture}
  109. end{document}
  110.  
  111. from math import sqrt
  112. import matplotlib.pyplot as plt
  113.  
  114.  
  115. def main():
  116. radiusa = 1.5
  117. radiusb = 1.5 / 5
  118. xa = -2.74
  119. ya = 1.14
  120. xb = 0.57
  121. yb = 2.85
  122.  
  123. figure, ax = plt.subplots()
  124. circlea = plt.Circle((xa, ya), radiusa, color='C0')
  125. circleb = plt.Circle((xb, yb), radiusb, color='C0')
  126. ax.add_artist(circlea)
  127. ax.add_artist(circleb)
  128.  
  129. (xc, yc), (xd, yd), (xe, ye), (xf, yf) = compute(xa, ya, radiusa, xb, yb, radiusb)
  130.  
  131. ax.plot([xc, xd], [yc, yd], color='C0')
  132. ax.plot([xe, xf], [ye, yf], color='C0')
  133.  
  134. ax.set_xlim(
  135. min(xa - radiusa, xb - radiusb),
  136. max(xa + radiusa, xb + radiusb),
  137. )
  138. ax.set_ylim(
  139. min(ya - radiusa, yb - radiusb),
  140. max(ya + radiusa, yb + radiusb),
  141. )
  142.  
  143. print("\coordinate (c) at ({}, {});".format(xc, yc))
  144. print("\coordinate (d) at ({}, {});".format(xd, yd))
  145. print("\coordinate (e) at ({}, {});".format(xe, ye))
  146. print("\coordinate (f) at ({}, {});".format(xf, yf))
  147.  
  148. plt.show()
  149.  
  150.  
  151. def compute(xa, ya, radiusa, xb, yb, radiusb):
  152. xp = (xb * radiusa - xa * radiusb) / (radiusa - radiusb)
  153. yp = (yb * radiusa - ya * radiusb) / (radiusa - radiusb)
  154. distancea = sqrt((xp - xa) * (xp - xa) + (yp - ya) * (yp - ya) - radiusa * radiusa)
  155. distanceb = sqrt((xp - xb) * (xp - xb) + (yp - yb) * (yp - yb) - radiusb * radiusb)
  156. denoma = (xp - xa)*(xp - xa) + (yp - ya)*(yp - ya)
  157. denomb = (xp - xb)*(xp - xb) + (yp - yb)*(yp - yb)
  158.  
  159. xc = (radiusa * radiusa * (xp - xa) + radiusa * (yp - ya) * distancea) / denoma + xa
  160. yc = (radiusa * radiusa * (yp - ya) - radiusa * (xp - xa) * distancea) / denoma + ya
  161.  
  162. xe = (radiusa * radiusa * (xp - xa) - radiusa * (yp - ya) * distancea) / denoma + xa
  163. ye = (radiusa * radiusa * (yp - ya) + radiusa * (xp - xa) * distancea) / denoma + ya
  164.  
  165. xd = (radiusb * radiusb * (xp - xb) + radiusb * (yp - yb) * distanceb) / denomb + xb
  166. yd = (radiusb * radiusb * (yp - yb) - radiusb * (xp - xb) * distanceb) / denomb + yb
  167.  
  168. xf = (radiusb * radiusb * (xp - xb) - radiusb * (yp - yb) * distanceb) / denomb + xb
  169. yf = (radiusb * radiusb * (yp - yb) + radiusb * (xp - xb) * distanceb) / denomb + yb
  170.  
  171. return (xc, yc), (xd, yd), (xe, ye), (xf, yf)
  172.  
  173.  
  174. if __name__ == '__main__':
  175. main()
Add Comment
Please, Sign In to add comment