Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. from __future__ import division
  2. import math
  3. import random
  4. from matplotlib import pyplot as plt
  5. import numpy as np
  6.  
  7. def intersectLC((x1,y1),(x2,y2), (o1,o2),r):
  8.  
  9. #translate everything to (0,0)
  10. x1 = x1-o1
  11. y1 = y1-o2
  12. x2 = x2-o1
  13. y2 = y2-o2
  14.  
  15. d_x = x2-x1
  16. d_y = y2-y1
  17.  
  18. d_r = math.sqrt(math.pow(d_x,2)+math.pow(d_y,2))
  19.  
  20. D = x1*y2 - x2*y1
  21.  
  22. delta = math.pow(r,2)*math.pow(d_r,2) - math.pow(D,2)
  23.  
  24. if delta<=0:
  25. print "FALSE"
  26. return False, []
  27. else:
  28. p1_x = (D*d_y + sgn(d_y)*d_x*math.sqrt(math.pow(r,2)*math.pow(d_r,2)-math.pow(D,2)))/math.pow(d_r,2)+o1
  29. p1_y = (-D*d_x + abs(d_y)*math.sqrt(math.pow(r,2)*math.pow(d_r,2)-math.pow(D,2)))/math.pow(d_r,2)+o2
  30. p2_x = (D*d_y - sgn(d_y)*d_x*math.sqrt(math.pow(r,2)*math.pow(d_r,2)-math.pow(D,2)))/math.pow(d_r,2)+o1
  31. p2_y = (-D*d_x - abs(d_y)*math.sqrt(math.pow(r,2)*math.pow(d_r,2)-math.pow(D,2)))/math.pow(d_r,2)+o2
  32. x1 = x1+o1
  33. y1 = y1+o2
  34. x2 = x2+o1
  35. y2 = y2+o2
  36. plt.subplot(121)
  37. plt.scatter([p1_x,p2_x],[p1_y,p2_y])
  38.  
  39. point = [(p1_x,p1_y),(p2_x,p2_y)]
  40.  
  41. # if random.random()<=0.5:
  42. # point = [(p2_x,p2_y),(p1_x,p1_y)]
  43.  
  44. print point
  45.  
  46. return True, point
  47.  
  48. def getAngle((x,y), (o1,o2)):
  49.  
  50. x = x - o1
  51. y = y - o2
  52.  
  53. theta = math.atan(abs(y/x))
  54.  
  55. if x>=0 and y>=0:
  56. print theta, math.degrees(theta)
  57. return theta
  58. #return math.degrees(theta)
  59. if x<=0 and y>=0:
  60. print math.pi-theta, math.degrees(math.pi-theta)
  61. return math.pi-theta
  62. #return math.degrees(math.pi-theta)
  63. if x<=0 and y<=0:
  64. print math.pi+theta, math.degrees(math.pi+theta)
  65. return math.pi+theta
  66. #return math.degrees(math.pi+theta)
  67. if x>=0 and y<=0:
  68. print 2*math.pi-theta, math.degrees(2*math.pi-theta)
  69. return 2*math.pi-theta
  70. #return math.degrees(2*math.pi-theta)
  71.  
  72. def sgn(x):
  73.  
  74. if x<0: return -1
  75. else: return 1
  76.  
  77. def chooseSegmentsRandom(n, N):
  78.  
  79. segments = []
  80.  
  81. plt.subplot(121)
  82. plt.axis('equal')
  83.  
  84. an = np.linspace(0, 2*np.pi, 100)
  85. plt.plot(15*np.cos(an), 15*np.sin(an))
  86.  
  87. for i in xrange(0,n):
  88. segment = [(random.random()*2*N-N,random.random()*2*N-N),(random.random()*2*N-N,random.random()*2*N-N)]
  89. segments.append(segment)
  90. plt.plot([segment[0][0], segment[1][0]], [segment[0][1], segment[1][1]])
  91.  
  92. return segments
  93.  
  94. def chooseSegmentsClusters(n, N):
  95.  
  96. segments = []
  97.  
  98. plt.subplot(121)
  99. plt.axis('equal')
  100.  
  101. an = np.linspace(0, 2*np.pi, 100)
  102. plt.plot(15*np.cos(an), 15*np.sin(an))
  103.  
  104. for i in xrange(0,int(n/4)):
  105. segment = [(random.random()*2+5,random.random()*2-6),(random.random()*2+5,random.random()*2+6)]
  106. segments.append(segment)
  107. plt.plot([segment[0][0], segment[1][0]], [segment[0][1], segment[1][1]], color = 'r')
  108.  
  109. for i in xrange(0,int(n/4)):
  110. segment = [(random.random()*4,random.random()*2-3),(random.random()*4-2,random.random()*2+3)]
  111. segments.append(segment)
  112. plt.plot([segment[0][0], segment[1][0]], [segment[0][1], segment[1][1]], color = 'b')
  113.  
  114.  
  115. for i in xrange(0,int(n/4)):
  116. segment = [(random.random()*2-6,random.random()*1),(random.random()*2+6, random.random()*1)]
  117. segments.append(segment)
  118. plt.plot([segment[0][0], segment[1][0]], [segment[0][1], segment[1][1]], color = 'g')
  119.  
  120. for i in xrange(0,int(n/4)):
  121. segment = [(random.random()*2-6,random.random()*1-7),(random.random()*2+6, random.random()*1-5)]
  122. segments.append(segment)
  123. plt.plot([segment[0][0], segment[1][0]], [segment[0][1], segment[1][1]], color = 'y')
  124.  
  125. plt.show()
  126.  
  127. return segments
  128.  
  129. def chooseSegmentsIntersection(n, N, a,b):
  130.  
  131. segments = []
  132.  
  133. plt.subplot(121)
  134. plt.axis('equal')
  135.  
  136. an = np.linspace(0, 2*np.pi, 100)
  137. plt.plot(15*np.cos(an), 15*np.sin(an))
  138.  
  139. for i in xrange(0,n):
  140. x1 = 1
  141. x2 = 1
  142. while math.pow(x1,2) + math.pow(x2,2)>=1:
  143. x1 = random.random()*2 - 1
  144. x2 = random.random()*2 - 1
  145. x = 5*(math.pow(x1,2)-math.pow(x2,2))/(math.pow(x1,2)+math.pow(x2,2))
  146. y = 5*(2*x1*x2)/(math.pow(x1,2)+math.pow(x2,2))
  147. segment = [(a,b),((x+a),(y+b))]
  148. segments.append(segment)
  149. plt.plot([segment[0][0], segment[1][0]], [segment[0][1], segment[1][1]])
  150.  
  151. return segments
  152.  
  153. def findAngles(segments, o, r):
  154.  
  155. plt.axis('equal')
  156.  
  157. i = 0
  158.  
  159. angles = []
  160.  
  161. for segment in segments:
  162. print segment
  163. b, p = intersectLC(segment[0], segment[1], o, r)
  164. if b:
  165. plt.subplot(122)
  166. a1 = getAngle(p[0],o)
  167. a2 = getAngle(p[1],o)
  168. plt.scatter(a1,a2, color = 'r')
  169. angles.append([a1,a2])
  170. # if i<int(100/4):
  171. # plt.scatter(getAngle(p[0],o), getAngle(p[1],o), color = 'r')
  172. # else:
  173. # if i<2*int(100/4):
  174. # plt.scatter(getAngle(p[0],o), getAngle(p[1],o), color = 'b')
  175. # else:
  176. # if i<3*int(100/4):
  177. # plt.scatter(getAngle(p[0],o), getAngle(p[1],o), color = 'g')
  178. # else:
  179. # plt.scatter(getAngle(p[0],o), getAngle(p[1],o), color = 'y')
  180.  
  181. i+=1
  182.  
  183. plt.show()
  184. return angles
  185.  
  186. def torus_distance((x1,y1),(x2,y2)):
  187.  
  188. return math.sqrt(math.pow(min(abs(x1-x2), 2*math.pi - abs(x1-x2)),2)+math.pow(min(abs(y1-y2), 2*math.pi - abs(y1-y2)),2))
  189.  
  190.  
  191. def kMeans(X, K, maxIters = 10, plot_progress = None):
  192.  
  193. centroids = X[np.random.choice(np.arange(len(X)), K), :]
  194. for i in range(maxIters):
  195. # Cluster Assignment step
  196. C = np.array([np.argmin([np.dot(x_i-y_k, x_i-y_k) for y_k in centroids]) for x_i in X])
  197. # Move centroids step
  198. centroids = [X[C == k].mean(axis = 0) for k in range(K)]
  199. if plot_progress != None: plot_progress(X, C, np.array(centroids))
  200. return np.array(centroids) , C
  201.  
  202. #centroids, C = kMeans(np.array([(0,-1),(0,0),(0,1),(0,2)]),3)
  203. #print centroids
  204. #print C
  205.  
  206. # segments = []
  207. # segments.extend(chooseSegmentsIntersection(100,2,0,0))
  208. # segments.extend(chooseSegmentsIntersection(100,2,-7.5,0))
  209. # segments.extend(chooseSegmentsIntersection(100,2,5,0))
  210. # segments.extend(chooseSegmentsIntersection(100,2,-5,0))
  211. # segments.extend(chooseSegmentsIntersection(100,2,7.5,0))
  212. # segments.extend(chooseSegmentsIntersection(100,2,-7.5,0))
  213. # segments.extend(chooseSegmentsIntersection(100,2,10,0))
  214. # segments.extend(chooseSegmentsIntersection(100,2,-10,0))
  215.  
  216. # findAngles(segments, (0,0), 15)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement