Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. nodes[0].setPosition([int(screensize[0]/2),int(screensize[1]/2)]) #fixing central node
  2. damping = 0.2
  3. repulsion = 180
  4. attraction = 0.01
  5. last40 = [0 for x in range(40)] #just for checking when we're done
  6. lastKE = 0
  7. correcting = True
  8. running = True
  9. while running:
  10.     screen.fill(white)
  11.     if correcting:
  12.         #print "correcting"
  13.         totalKE = 0
  14.         for index,node in enumerate(nodes):
  15.             if index == 0:
  16.                 node.setVelocity((0,0))
  17.                 continue
  18.             node.setVelocity((0,0))
  19.             netForce = (0,0)
  20.             for otherNode in nodes:
  21.                 if otherNode == node:
  22.                     pass
  23.                 else:   # repulsion force
  24.                     dsqrd = (node.rect.center[0]-otherNode.rect.center[0])**2+(node.rect.center[1]-otherNode.rect.center[1])**2
  25.                     netForce =  vadd( netForce,(repulsion*(node.rect.center[0]-otherNode.rect.center[0])/float(dsqrd),
  26.                                                 repulsion*(node.rect.center[1]-otherNode.rect.center[1])/float(dsqrd)))
  27.                    
  28.            
  29.             for spring in node.attachedEdges: #spring attraction
  30.                     oN = spring.attachedNodes[0]
  31.                     if oN == node:
  32.                         oN = spring.attachedNodes[1]
  33.                     netForce = vadd(netForce, ((node.weight+oN.weight)*attraction*(oN.rect.center[0]-node.rect.center[0]),
  34.                                                (node.weight+oN.weight)*attraction*(oN.rect.center[1]-node.rect.center[1])))
  35.  
  36.             node.setVelocity(((node.velocity[0]+netForce[0])*damping,
  37.                               (node.velocity[1]+netForce[1])*damping))
  38.            
  39.             totalKE += node.weight*((node.velocity[0]**2+node.velocity[1]**2))
  40.         last40.remove(last40[0])
  41.         last40.append(abs(totalKE-lastKE)) #append delta_KE
  42.         lastKE = totalKE
  43.         averageDE = sum(last40)/40.0
  44.  
  45.         if averageDE < 0.00005:
  46.             print "Nothing changed for 40 iterations, done."
  47.             correcting = False
  48.        
  49.         for index, node in enumerate(nodes):
  50.             if index == 0:
  51.                 node.setVelocity((0,0))
  52.                 node.setPosition((screensize[0]/2,screensize[1]/2))
  53.             else:
  54.                 node.setPosition((int(node.rect.center[0]+node.velocity[0]),
  55.                                   int(node.rect.center[1]+node.velocity[1])))
  56.     for node in nodes:
  57.         for spring in node.attachedEdges:
  58.             spring.update()
  59.     for node in nodes:
  60.         node.update()
  61.                
  62.     pygame.display.flip()  
  63.