here2share

# tk_vector_field.py

Jan 6th, 2026
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.76 KB | None | 0 0
  1. # tk_vector_field.py
  2.  
  3. import math, random, tkinter as tk
  4.  
  5. WIDTH, HEIGHT = 1200, 600
  6. MARGIN = 40
  7. NUM_LON = WIDTH // MARGIN
  8. NUM_LAT = HEIGHT // MARGIN
  9. DELAY = 10
  10. LENGTH = MARGIN * 0.7
  11.  
  12. focus = []
  13. for _ in range(20):
  14.     theta = random.uniform(0, 2*math.pi)
  15.     speed = random.uniform(0.5, 3.0)
  16.     vx = math.cos(theta) * speed
  17.     vy = math.sin(theta) * speed
  18.     focus.append({
  19.         "x": random.randint(0, WIDTH),
  20.         "y": random.randint(0, HEIGHT),
  21.         "vx": vx,
  22.         "vy": vy,
  23.         "theta": theta
  24.     })
  25.  
  26. MAX_SPEED = 10
  27. ACCEL = 0.5
  28.  
  29. def lon_to_x(lon): return MARGIN + (lon + 180) * (WIDTH - 2*MARGIN) / 360
  30. def lat_to_y(lat): return HEIGHT - MARGIN - (lat + 90) * (HEIGHT - 2*MARGIN) / 180
  31.  
  32. def magnitude_color(mag):
  33.     r = int(255 * mag); b = int(255 * (1-mag)); g = 40
  34.     return f"#{r:02x}{g:02x}{b:02x}"
  35.  
  36. def update_focus():
  37.     for f in focus:
  38.         f["theta"] += random.uniform(-0.1, 0.1)
  39.         ax = math.cos(f["theta"]) * ACCEL + random.uniform(-0.1, 0.1)
  40.         ay = math.sin(f["theta"]) * ACCEL + random.uniform(-0.1, 0.1)
  41.         f["vx"] += ax; f["vy"] += ay
  42.         speed = math.sqrt(f["vx"]**2 + f["vy"]**2)
  43.         if speed > MAX_SPEED:
  44.             f["vx"] = f["vx"] / speed * MAX_SPEED
  45.             f["vy"] = f["vy"] / speed * MAX_SPEED
  46.         f["x"] += f["vx"]; f["y"] += f["vy"]
  47.  
  48.         if f["x"] < 0: f["x"] += WIDTH
  49.         if f["x"] >= WIDTH: f["x"] -= WIDTH
  50.         if f["y"] < 0: f["y"] += HEIGHT
  51.         if f["y"] >= HEIGHT: f["y"] -= HEIGHT
  52.  
  53. def animate_field():
  54.     update_focus()
  55.     for j in range(NUM_LAT):
  56.         lat = -90 + j*(180/(NUM_LAT-1))
  57.         py = lat_to_y(lat)
  58.         for i in range(NUM_LON):
  59.             lon = -180 + i*(360/(NUM_LON-1))
  60.             px = lon_to_x(lon)
  61.             u = v = 0
  62.             for f in focus:
  63.                 dx = (f["x"] - px + WIDTH/2) % WIDTH - WIDTH/2
  64.                 dy = (f["y"] - py + HEIGHT/2) % HEIGHT - HEIGHT/2
  65.                 dist = math.sqrt(dx*dx + dy*dy) + 1e-6
  66.                 u += dx / (dist**2)
  67.                 v += dy / (dist**2)
  68.             mag = math.sqrt(u*u + v*v)
  69.             if mag > 0:
  70.                 u /= mag
  71.                 v /= mag
  72.                 dx = LENGTH * u
  73.                 dy = -LENGTH * v
  74.                 x2, y2 = px + dx, py + dy
  75.                 color = magnitude_color(min(1.0, mag/5.0))
  76.                 c.coords(arrows[j][i], x2, y2, px, py)
  77.                 c.itemconfig(arrows[j][i], fill=color)
  78.     c.after(DELAY, animate_field)
  79.  
  80. root = tk.Tk()
  81. root.title("# tk_vector_field.py")
  82. root.geometry('+0+0')
  83. c = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg="white")
  84. c.pack()
  85. arrows = [[c.create_line(0,0,0,0,arrow=tk.LAST,width=2) for i in range(NUM_LON)] for j in range(NUM_LAT)]
  86. animate_field()
  87. root.mainloop()
  88.  
Advertisement
Add Comment
Please, Sign In to add comment