tikz.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. from random import uniform
  2. import random
  3. import math
  4. print """\documentclass{article}
  5. \usepackage[pdftex,active,tightpage]{preview}
  6. \setlength\PreviewBorder{2mm}
  7. \usepackage{tikz}
  8. \usepackage{tkz-fct}
  9. \usetikzlibrary{shapes.misc}
  10. \usetikzlibrary{shapes, calc, decorations}
  11. \usepackage{amsmath,amssymb}
  12. \\begin{document}
  13. \\begin{preview}
  14. \\begin{tikzpicture}[scale=0.1,dot/.style={ thick,
  15. draw=gray,
  16. cross out,
  17. inner sep=0pt,
  18. minimum width=4pt,
  19. minimum height=4pt}]
  20. """
  21. def get_position_from_offset(px, py, angle, radius):
  22. x = px + radius * math.cos(math.radians(angle))
  23. y = py + radius * math.sin(math.radians(angle))
  24. return (x, y)
  25. n = 5
  26. xSum = 0
  27. ySum = 0
  28. coordinates = [(0, 0)]
  29. random.seed(13)
  30. for i in range(n-1):
  31. radius = uniform(0, 20)
  32. angle = uniform(0, 360)
  33. px, py = coordinates[-1]
  34. x, y = get_position_from_offset(px, py, angle, radius)
  35. xSum += x
  36. ySum += y
  37. coordinates.append((x, y))
  38. center = (float(xSum) / n, float(ySum) / n)
  39. cx, cy = center
  40. coordinates.append((cx+15, cy))
  41. pointCoords = ""
  42. for p in coordinates:
  43. px, py = p
  44. px -= cx
  45. py -= cy
  46. newP = "(%.2f,%.2f)," % (px, py)
  47. pointCoords = newP + pointCoords
  48. deltaY = 0-py
  49. deltaX = 0-px
  50. length = (deltaY**2+deltaX**2)**0.5
  51. sinAlpha = deltaY/length
  52. cosAlpha = deltaX/length
  53. print("\draw[->] (%.2f,%.2f) -- (%.2f,%.2f);" %
  54. (px, py, px+cosAlpha*length*0.80, py+sinAlpha*length*0.80))
  55. print("\\node[circle,inner sep=1pt,fill] at (%.2f,%.2f) {};" % (0, 0))
  56. print("\\foreach \point in {" + pointCoords[:-1] + "}{")
  57. print("\\node[dot] at \point {};")
  58. print("}")
  59. ###############################################################################
  60. random.seed(17)
  61. xSum = 0
  62. ySum = 0
  63. coordinates = [(0, 0)]
  64. for i in range(n-1):
  65. radius = uniform(0, 20)
  66. angle = uniform(0, 360)
  67. px, py = coordinates[-1]
  68. x, y = get_position_from_offset(px, py, angle, radius)
  69. xSum += x
  70. ySum += y
  71. coordinates.append((x, y))
  72. center = (float(xSum) / n, float(ySum) / n)
  73. cx, cy = center
  74. coordinates.append((cx-15, cy))
  75. xOffset = 40
  76. cTmp = []
  77. for p in coordinates:
  78. px, py = p
  79. cTmp.append((px-cx+xOffset, py-cy))
  80. coordinates = cTmp
  81. cx, cy = xOffset, 0
  82. pointCoords = ""
  83. for p in coordinates:
  84. px, py = p
  85. newP = "(%.2f,%.2f)," % (px, py)
  86. pointCoords = newP + pointCoords
  87. deltaY = -py
  88. deltaX = xOffset-px
  89. length = (deltaY**2+deltaX**2)**0.5
  90. sinAlpha = deltaY/length
  91. cosAlpha = deltaX/length
  92. print("\draw[->] (%.2f,%.2f) -- (%.2f,%.2f);" %
  93. (px, py, px+cosAlpha*length*0.80, py+sinAlpha*length*0.80))
  94. print("\\node[circle,inner sep=1pt,fill] at (%.2f,%.2f) {};" % (xOffset, 0))
  95. print("\\foreach \point in {" + pointCoords[:-1] + "}{")
  96. print("\\node[dot] at \point {};")
  97. print("}")
  98. ##################################
  99. print("\\node[circle,inner sep=1pt,fill] at (%.2f,%.2f) {};" % (xOffset, 0))
  100. print("\\foreach \point in {(15,0), (35,0)}{")
  101. print("\\node[dot] at \point {};")
  102. print("}")
  103. print """
  104. \end{tikzpicture}
  105. \end{preview}
  106. \end{document}
  107. """