tikz.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 getPositionFromOffset(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 = getPositionFromOffset(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);" % (px, py, px+cosAlpha*length*0.80, py+sinAlpha*length*0.80))
  54. print("\\node[circle,inner sep=1pt,fill] at (%.2f,%.2f) {};" % (0, 0))
  55. print("\\foreach \point in {" + pointCoords[:-1] + "}{")
  56. print("\\node[dot] at \point {};")
  57. print("}")
  58. ################################################################################
  59. random.seed(17)
  60. xSum = 0
  61. ySum = 0
  62. coordinates = [(0,0)]
  63. for i in range(n-1):
  64. radius = uniform(0,20)
  65. angle = uniform(0, 360)
  66. px, py = coordinates[-1]
  67. x, y = getPositionFromOffset(px, py, angle, radius)
  68. xSum += x
  69. ySum += y
  70. coordinates.append((x,y))
  71. center = (float(xSum) / n, float(ySum) / n)
  72. cx, cy = center
  73. coordinates.append((cx-15,cy))
  74. xOffset = 40
  75. cTmp = []
  76. for p in coordinates:
  77. px, py = p
  78. cTmp.append((px-cx+xOffset,py-cy))
  79. coordinates = cTmp
  80. cx, cy = xOffset, 0
  81. pointCoords = ""
  82. for p in coordinates:
  83. px, py = p
  84. newP = "(%.2f,%.2f)," % (px, py)
  85. pointCoords = newP + pointCoords
  86. deltaY = -py
  87. deltaX = xOffset-px
  88. length = (deltaY**2+deltaX**2)**0.5
  89. sinAlpha = deltaY/length
  90. cosAlpha = deltaX/length
  91. print("\draw[->] (%.2f,%.2f) -- (%.2f,%.2f);" % (px, py, px+cosAlpha*length*0.80, py+sinAlpha*length*0.80))
  92. print("\\node[circle,inner sep=1pt,fill] at (%.2f,%.2f) {};" % (xOffset, 0))
  93. print("\\foreach \point in {" + pointCoords[:-1] + "}{")
  94. print("\\node[dot] at \point {};")
  95. print("}")
  96. ##################################
  97. print("\\node[circle,inner sep=1pt,fill] at (%.2f,%.2f) {};" % (xOffset, 0))
  98. print("\\foreach \point in {(15,0), (35,0)}{")
  99. print("\\node[dot] at \point {};")
  100. print("}")
  101. print """
  102. \end{tikzpicture}
  103. \end{preview}
  104. \end{document}
  105. """