from math import pi, cos, sin, exp import pygame, colorsys # === graph data === # vertices as screen positions viewsize = 600 shrink = 0.9 vertices = [(viewsize*(1 + shrink*x)/2, viewsize*(1 + shrink*y)/2) for x, y in [(0, 0)] + [(cos(-n*pi/5), sin(-n*pi/5)) for n in range(10)] + [(0, 0)] ] # edges as vertex pairs. FireStar will rest on vertices 6 (A), 5 (B), 7 (C) edges = [ (0, 1), (0, 3), (0, 5), (0, 7), (0, 9), (2, 8), (2, 6), (6, 10), (10, 4), (4, 8), (2, 9), (2, 11), (2, 5), (4, 1), (4, 9), (4, 7), (6, 3), (6, 11), (6, 9), (8, 5), (8, 11), (8, 1), (10, 7), (10, 11), (10, 3), (9, 3), (9, 5), (1, 5), (1, 7), (3, 7) ] # cyclic edge adjacencies cyc_edge_adj = [ (0, 1), (1, 2), (2, 3), (3, 4), (4, 0), (5, 10), (10, 11), (11, 12), (12, 6), (6, 5), (6, 16), (16, 17), (17, 18), (18, 7), (7, 6), (7, 22), (22, 23), (23, 24), (24, 8), (8, 7), (8, 13), (13, 14), (14, 15), (15, 9), (9, 8), (5, 9), (9, 19), (19, 20), (20, 21), (21, 5), (4, 10), (10, 25), (25, 26), (26, 18), (18, 4), (3, 15), (15, 29), (29, 28), (28, 22), (22, 3), (2, 12), (12, 27), (27, 26), (26, 19), (19, 2), (1, 24), (24, 25), (25, 29), (29, 16), (16, 1), (0, 21), (21, 28), (28, 27), (27, 13), (13, 0), (14, 11), (11, 23), (23, 20), (20, 17), (17, 14) ] # === interaction parameters === frame_rate = 24 inductance = 0.001 drain = False drain_rate = 0.1 # === phase space === charge = 12*[0] current = 30*[0] # === main loop === if __name__ == '__main__': # set up display and clock pygame.init() screen = pygame.display.set_mode((viewsize, viewsize + 100)) pygame.display.set_caption('Preview') font = pygame.font.Font(None, 25) clock = pygame.time.Clock() # set up background background = pygame.Surface(screen.get_size()).convert() background.fill((32, 32, 32)) to_light = 0 while True: # handle events for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_j: charge[0] += 10 charge[11] -= 10 elif event.key == pygame.K_k: charge[0] += 100 charge[11] -= 100 elif event.key == pygame.K_SPACE: drain = True pygame.draw.aaline(screen, (255, 255, 255), (0, 0), (viewsize, viewsize)) elif event.type == pygame.KEYUP: if event.key == pygame.K_SPACE: drain = False elif event.type == pygame.QUIT: quit() # print data energy = 0.5*(sum(q*q for q in charge) + inductance*sum(i*i for i in current)) screen.blit(background, (0, 0)) text = font.render('energy', True, (255, 255, 255)) screen.blit(text, (20, viewsize)) text = font.render(str(energy), True, (255, 255, 255)) screen.blit(text, (120, viewsize)) text = font.render('charge[0]', True, (255, 255, 255)) screen.blit(text, (20, 30 + viewsize)) text = font.render(str(charge[0]), True, (255, 255, 255)) screen.blit(text, (120, 30 + viewsize)) # show state for e in range(30): litness = 1 - exp(-2*current[e]*current[e]) color = colorsys.hsv_to_rgb(0.45 + litness*0.25, 1 - 0.5*litness*litness, litness) ##color = colorsys.hsv_to_rgb(litness*0.167, 1 - 0.5*litness*litness, litness) pygame.draw.aaline( screen, tuple(255*c for c in color), vertices[edges[e][0]], vertices[edges[e][1]], ) # evolve state. use verlet integration, first updating the currents and then # using the new currents to update the charges. for e in range(30): u, v = edges[e] ##current[e] *= 1 - resistance current[e] += inductance*(charge[u] - charge[v]) for e in range(30): u, v = edges[e] charge[u] -= current[e] charge[v] += current[e] if drain: for v in range(12): charge[v] *= 1 - drain_rate # step pygame.display.flip() clock.tick(frame_rate)