Advertisement
here2share

# for_pixel_generative_art.py

Dec 19th, 2022
1,004
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 22.27 KB | None | 0 0
  1. # for_pixel_generative_art.py
  2.  
  3. Mandelbrot Set: This algorithm generates a Mandelbrot set by iterating over a complex plane and using a color map to determine the color of each point based on the number of iterations it takes to escape the set.
  4. import math
  5. def mandelbrot():
  6.     for x in range(-width, width):
  7.         for y in range(-height, height):
  8.             z = complex(x * 4 / width, y * 4 / height)
  9.             c = z
  10.             for i in range(255):
  11.                 if abs(z) > 2:
  12.                     break
  13.                 z = z * z + c
  14.             cv.create_line(x, y, x, y, fill=color_map[i])
  15.         cv.update()
  16.        
  17. Conway's Game of Life: This algorithm simulates the evolution of a cellular automaton according to the rules of Conway's Game of Life.
  18. def game_of_life():
  19.     grid = [[random.choice([0, 1]) for _ in range(width)] for _ in range(height)]
  20.     while True:
  21.         for i in range(1, width - 1):
  22.             for j in range(1, height - 1):
  23.                 neighbors = sum([grid[i + ii][j + jj] for ii in [-1, 0, 1] for jj in [-1, 0, 1] if ii != 0 or jj != 0])
  24.                 if grid[i][j] == 1:
  25.                     if neighbors < 2 or neighbors > 3:
  26.                         grid[i][j] = 0
  27.                 else:
  28.                     if neighbors == 3:
  29.                         grid[i][j] = 1
  30.         for i in range(1, width - 1):
  31.             for j in range(1, height - 1):
  32.                 if grid[i][j] == 1:
  33.                     cv.create_line(i, j, i + 1, j + 1, fill='black')
  34.                 else:
  35.                     cv.create_line(i, j, i + 1, j + 1, fill='white')
  36.         cv.update()
  37.        
  38. Fractal Tree: This algorithm generates a fractal tree by recursively drawing lines with decreasing length and alternating colors.
  39. def fractal_tree(cv, x1, y1, angle, depth):
  40.     if depth:
  41.         x2 = x1 + int(math.cos(angle) * depth * 10)
  42.         y2 = y1 + int(math.sin(angle) * depth * 10)
  43.         cv.create_line(x1, y1, x2, y2, fill=random.choice(colors))
  44.         fractal_tree(cv, x2, y2, angle - 20, depth - 1)
  45.         fractal_tree(cv, x2, y2, angle + 20, depth - 1)
  46. def draw_fractal_tree():
  47.     fractal_tree(cv, width / 2, height, -90, 9)
  48.     cv.update()
  49.    
  50. Cellular Automaton: This algorithm simulates a cellular automaton by using a rule set to determine the color of the line based on the colors of neighboring cells.
  51. import random
  52. def cellular_automaton():
  53.     grid = [[random.choice(colors) for _ in range(width)] for _ in range(height)]
  54.     while True:
  55.         for i in range(1, width - 1):
  56.             for j in range(1, height - 1):
  57.                 neighbors = sum([grid[i + ii][j + jj] for ii in [-1, 0, 1] for jj in [-1, 0, 1] if ii != 0 or jj != 0])
  58.                 if grid[i][j] == 1:
  59.                     if neighbors < 2 or neighbors > 3:
  60.                         grid[i][j] = 0
  61.                 else:
  62.                     if neighbors == 3:
  63.                         grid[i][j] = 1
  64.         for i in range(1, width - 1):
  65.             for j in range(1, height - 1):
  66.                 if grid[i][j] == 1:
  67.                     cv.create_line(i, j, i + 1, j + 1, fill='black')
  68.                 else:
  69.                     cv.create_line(i, j, i + 1, j + 1, fill='white')
  70.         cv.update()
  71.        
  72. Particle System: This algorithm simulates a particle system by randomly generating particles with initial velocities and updating their positions over time according to basic physics principles.
  73. def particle_system():
  74.     particles = [Particle(random.uniform(-5, 5), random.uniform(-5, 5)) for _ in range(100)]
  75.     while True:
  76.         for particle in particles:
  77.             particle.update()
  78.             cv.create_line(particle.x, particle.y, particle.x + particle.vx, particle.y + particle.vy, fill=random.choice(colors))
  79.         cv.update()
  80.  
  81. Sine Wave: This algorithm generates a sine wave by using a sine function to determine the y coordinate of the line.
  82. import math
  83. def sine_wave():
  84.     x = 0
  85.     while True:
  86.         y = math.sin(x / 10) * 50 + 50
  87.         cv.create_line(x, y, x, y, fill=random.choice(colors))
  88.         cv.update()
  89.         x += 1
  90.  
  91. Rainbow Gradient: This algorithm generates a rainbow gradient by using the hue value of a color to determine the color of the line.
  92. import colorsys
  93. def rainbow_gradient():
  94.     x = 0
  95.     while True:
  96.         hue = x / width
  97.         color = colorsys.hsv_to_rgb(hue, 1.0, 1.0)
  98.         color = '#%02x%02x%02x' % (int(color[0] * 255), int(color[1] * 255), int(color[2] * 255))
  99.         cv.create_line(x, 0, x, height, fill=color)
  100.         cv.update()
  101.         x += 1
  102.        
  103. Circular Pattern: This algorithm generates a circular pattern by using polar coordinates to determine the x and y coordinates of the line.
  104. import math
  105. def circular_pattern():
  106.     t = 0
  107.     while True:
  108.         r = t / 10
  109.         x = r * math.cos(t) + width / 2
  110.         y = r * math.sin(t) + height / 2
  111.         cv.create_line(x, y, x, y, fill=random.choice(colors))
  112.         cv.update()
  113.         t += 0.1
  114.        
  115. Plasma Effect: This algorithm generates a plasma effect by using sine and cosine functions to determine the color of each point on the canvas.
  116. import colorsys
  117. def plasma():
  118.     t = 0
  119.     while True:
  120.         for x in range(width):
  121.             for y in range(height):
  122.                 hue = (math.sin(x / 10 + t) + math.cos(y / 10 + t)) / 2
  123.                 color = colorsys.hsv_to_rgb(hue, 1.0, 1.0)
  124.                 color = '#%02x%02x%02x' % (int(color[0] * 255), int(color[1] * 255), int(color[2] * 255))
  125.                 cv.create_line(x, y, x, y, fill=color)
  126.         cv.update()
  127.         t += 0.1
  128.        
  129. Spirograph: This algorithm generates a spirograph by drawing circles with different radii and using the parametric equations for a circle to determine the x and y coordinates of the line.
  130. def spirograph():
  131. r1 = random.randint(50, 150)
  132. r2 = random.randint(50, 150)
  133. p = random.randint(50, 150)
  134. t = 0
  135. while t < 360:
  136. x = (r1 - r2) * math.cos(t) + p * math.cos((r1 - r2) * t / r2)
  137. y = (r1 - r2) * math.sin(t) - p * math.sin((r1 - r2) * t / r2)
  138. cv.create_line(x, y, x + 1, y + 1, fill=random.choice(colors))
  139. t += 0.1
  140. cv.update()
  141.  
  142. L-System: This algorithm generates a fractal pattern using an L-system (Lindenmayer system) by iteratively replacing symbols in a string according to a set of rules.
  143. def l_system():
  144. start = "F"
  145. rules = {"F": "F[+F]F[-F]F"}
  146. for i in range(5):
  147. next_str = ""
  148. for char in start:
  149. if char in rules:
  150. next_str += rules[char]
  151. else:
  152. next_str += char
  153. start = next_str
  154. for char in start:
  155. if char == "F":
  156. cv.create_line(x, y, x + 5, y + 5, fill=random.choice(colors))
  157. x += 5
  158. y += 5
  159. elif char == "+":
  160. x += 5
  161. elif char == "-":
  162. y += 5
  163. cv.update()
  164.  
  165. Perlin Noise: This algorithm generates a random, organic-looking pattern using Perlin noise, which is based on interpolating between random gradient vectors.
  166. def perlin_noise():
  167. x_offset = random.random()
  168. y_offset = random.random()
  169. for x in range(width):
  170. for y in range(height):
  171. nx = x / width - 0.5
  172. ny = y / height - 0.5
  173. noise = perlin(nx + x_offset, ny + y_offset)
  174. cv.create_line(x, y, x + 1, y + 1, fill=(noise * 255, noise * 255, noise * 255))
  175. cv.update()
  176.  
  177. Bouncing Ball: This algorithm simulates a bouncing ball by updating the x and y coordinates of the ball based on its velocity and checking for collisions with the edges of the canvas.
  178. def bouncing_ball():
  179. x = random.randint(0, width)
  180. y = random.randint(0, height)
  181. vx = random.uniform(-5, 5)
  182. vy = random.uniform(-5, 5)
  183. while True:
  184. x += vx
  185. y += vy
  186. if x < 0 or x > width:
  187. vx = -vx
  188. if y < 0 or y > height:
  189. vy = -vy
  190. cv
  191. .create_line(x, y, x + 1, y + 1, fill=random.choice(colors))
  192. cv.update()
  193. time.sleep(0.01)
  194.  
  195. Mandelbulb: This algorithm generates a 3D fractal known as a Mandelbulb by iterating over a 3D space and using a color map to determine the color of each point based on the number of iterations it takes to escape the set.
  196. def mandelbulb():
  197. for x in range(-width, width):
  198. for y in range(-height, height):
  199. for z in range(-depth, depth):
  200. c = complex(x * 4 / width, y * 4 / height, z * 4 / depth)
  201. z = c
  202. for i in range(255):
  203. if abs(z) > 2:
  204. break
  205. z = z * z + c
  206. cv.create_line(x, y, x, y, fill=color_map[i])
  207. cv.update()
  208.  
  209. Particle System: This algorithm simulates a particle system by updating the position and velocity of each particle based on forces such as gravity and air resistance.
  210. def particle_system():
  211. particles = []
  212. for i in range(50):
  213. particles.append({
  214. "x": random.uniform(0, width),
  215. "y": random.uniform(0, height),
  216. "vx": random.uniform(-5, 5),
  217. "vy": random.uniform(-5, 5),
  218. "mass": random.uniform(0.1, 1)
  219. })
  220. while True:
  221. for particle in particles:
  222. particle["x"] += particle["vx"]
  223. particle["y"] += particle["vy"]
  224. particle["vy"] += 0.1 * particle["mass"]
  225. if particle["x"] < 0 or particle["x"] > width:
  226. particle["vx"] = -particle["vx"]
  227. if particle["y"] < 0 or particle["y"] > height:
  228. particle["vy"] = -particle["vy"]
  229. cv.create_line(particle["x"], particle["y"], particle["x"] + 1, particle["y"] + 1, fill=random.choice(colors))
  230. cv.update()
  231. time.sleep(0.01)
  232. Firework: This algorithm simulates a firework by creating a burst of particles at the starting point and updating their position and velocity based on forces such as gravity and air resistance.
  233. def firework():
  234. particles = []
  235. for i in range(50):
  236. particles.append({
  237. "x": random.uniform(0, width),
  238. "y": random.uniform(0, height),
  239. "vx": random.uniform(-5, 5),
  240. "vy": random.uniform(-5, 5),
  241. "mass": random.uniform(0.1, 1)
  242. })
  243. while True:
  244. for particle in particles:
  245. particle["x"] += particle["vx"]
  246. particle["y"] += particle["vy"]
  247. particle["vy"] += 0.1 * particle["mass"]
  248. if particle["x"] < 0 or particle["x"] > width:
  249. particle["vx"] = -particle["vx"]
  250. if particle["y"] < 0 or particle["y"] > height:
  251. particle["vy"] = -particle["vy"]
  252. cv.create_line(particle["x"], particle["y"], particle["x"] + 1, particle["y"] + 1, fill=random.choice(colors))
  253. cv.update()
  254. time.sleep(0.01)
  255.  
  256. Galaxy: This algorithm simulates a galaxy by updating the position and velocity of each star based on the gravitational force of the other stars.
  257. def galaxy():
  258. stars = []
  259. for i in range(50):
  260. stars.append({
  261. "x": random.uniform(0, width),
  262. "y": random.uniform(0, height),
  263. "vx": random.uniform(-0.5, 0.5),
  264. "vy": random.uniform(-0.5, 0.5),
  265. "mass": random.uniform(0.1, 1)
  266. })
  267. while True:
  268. for i, star in enumerate(stars):
  269. for j, other in enumerate(stars):
  270. if i == j:
  271. continue
  272. dx = other["x"] - star["x"]
  273. dy = other["y"] - star["y"]
  274. distance = math.sqrt(dx2 + dy2)
  275. if distance < 10:
  276. distance = 10
  277. force = other["mass"] / (distance**2)
  278. star["vx"] += dx / distance * force
  279. star["vy"] += dy / distance * force
  280. star["x"] += star["vx"]
  281. star["y"] += star["vy"]
  282. cv.create_line(star["x"], star["y"], star["x"] + 1, star["y"] + 1, fill=random.choice(colors))
  283. cv.update()
  284. time.sleep(0.01)
  285. Shapes: This algorithm generates random shapes by randomly selecting points on the canvas and connecting them with lines.
  286. def shapes():
  287. points = []
  288. for i in range(10):
  289. points.append((random.randint(0, width), random.randint(0, height)))
  290. for i, point in enumerate(points):
  291. for j, other in enumerate(points):
  292. if i == j:
  293. continue
  294. cv.create_line(point[0], point[1], other[0], other[1], fill=random.choice(colors))
  295. cv.update()
  296.  
  297. Kaleidoscope: This algorithm generates a kaleidoscope effect by drawing a pattern and rotating it around the center of the canvas.
  298. def kaleidoscope():
  299. angle = 0
  300. while True:
  301. for x in range(-width, width):
  302. for y in range(-height, height):
  303. nx = x * math.cos(angle) - y * math.sin(angle)
  304. ny = x * math.sin(angle) + y * math.cos(angle)
  305. cv.create_line(nx, ny, nx + 1, ny + 1, fill=random.choice(colors))
  306. angle += 0.01
  307. cv.update()
  308. time.sleep(0.01)
  309. Ray Tracing: This algorithm generates a 3D image by tracing the path of light rays through a 3D scene and determining the color of each pixel based on the objects and lights in the scene.
  310. def ray_tracing():
  311. for x in range(width):
  312. for y in range(height):
  313. ray_origin = (0, 0, 0)
  314. ray_direction = (x / width - 0.5, y / height - 0.5, 1)
  315. intersect, color = trace_ray(ray_origin, ray_direction, objects, lights)
  316. if intersect:
  317. cv.create_line(x, y, x + 1, y + 1, fill=color)
  318. cv.update()
  319.  
  320. Perlin Worm: This algorithm generates a curving, organic-looking pattern using Perlin noise, which is based on interpolating between random gradient vectors.
  321. def perlin_worm():
  322. x = 0
  323. y = 0
  324. t = 0
  325. while True:
  326. nx = x / width - 0.5
  327. ny = y / height - 0.5
  328. noise = perlin(nx, ny)
  329. x += math.cos(noise * math.pi * 2)
  330. y += math.sin(noise * math.pi * 2)
  331. t += 0.01
  332. cv.create_line(x, y, x + 1, y + 1, fill=(noise * 255, noise * 255, noise * 255))
  333. cv.update()
  334. time.sleep(0.01)
  335.  
  336. Flocking: This algorithm simulates the flocking behavior of birds by updating the position and velocity of each bird based on the positions and velocities of its neighbors.
  337. def flocking():
  338. birds = []
  339. for i in range(50):
  340. birds.append({
  341. "x": random.uniform(0, width),
  342. "y": random.uniform(0, height),
  343. "vx": random.uniform(-5, 5),
  344. "vy": random.uniform(-5, 5)
  345. })
  346. while True:
  347. for i, bird in enumerate(birds):
  348. vx = bird["vx"]
  349. vy = bird["vy"]
  350. for j, other in enumerate(birds):
  351. if i == j:
  352. continue
  353. dx = other["x"] - bird["x"]
  354. dy = other["y"] - bird["y"]
  355. distance = math.sqrt(dx2 + dy2)
  356. if distance < 10:
  357. vx += dx / distance
  358. vy += dy / distance
  359. bird["vx"] = vx
  360. bird["vy"] = vy
  361. bird["x"] += bird["vx"]
  362. bird["y"] += bird["vy"]
  363. cv.create_line(bird["x"], bird["y"], bird["x"] + 1, bird["y"] + 1, fill=random.choice(colors))
  364. cv.update()
  365. time.sleep(0.01)
  366. Audio Visualization: This algorithm generates a visual representation of an audio signal by using the amplitude of the signal at each point in time to determine the color and height of the line.
  367. def audio_visualization(audio_data):
  368. for i, sample in enumerate(audio_data):
  369. cv.create_line(i, height / 2, i, height / 2 + sample * height / 2, fill=random.choice(colors))
  370. cv.update()
  371. Rainbow Lines: This algorithm generates a rainbow effect by gradually changing the color of the lines as they are drawn.
  372. def rainbow_lines():
  373. hue = 0
  374. while True:
  375. for x in range(width):
  376. color = colorsys.hsv_to_rgb(hue, 1, 1)
  377. cv.create_line(x, 0, x, height, fill=color)
  378. hue += 0.01
  379. cv.update()
  380. time.sleep(0.01)
  381.  
  382. Plasma: This algorithm generates a plasma effect by using a set of sine waves with different frequencies and amplitudes to determine the color of each pixel.
  383. def plasma():
  384. t = 0
  385. while True:
  386. for x in range(width):
  387. for y in range(height):
  388. color = (
  389. math.sin(x / 10 + t) + math.sin(y / 10 + t),
  390. math.sin(x / 20 + t) + math.sin(y / 20 + t),
  391. math.sin(x / 30 + t) + math.sin(y / 30 + t)
  392. )
  393. cv.create_line(x, y, x + 1, y + 1, fill=color)
  394. t += 0.01
  395. cv.update()
  396. time.sleep(0.01)
  397. Sierpinski Triangle: This algorithm generates a Sierpinski triangle by recursively drawing smaller triangles with alternating colors.
  398. def sierpinski_triangle(cv, x1, y1, x2, y2, x3, y3, depth):
  399. if depth:
  400. cv.create_line(x1, y1, x2, y2, fill=random.choice(colors))
  401. cv.create_line(x2, y2, x3, y3, fill=random.choice(colors))
  402. cv.create_line(x3, y3, x1, y1, fill=random.choice(colors))
  403. sierpinski_triangle(cv, x1, y1, (x1 + x2) / 2, (y1 + y2) / 2, (x1 + x3) / 2, (y1 + y3) / 2, depth - 1)
  404. sierpinski_triangle(cv, (x1 + x2) / 2, (y1 + y2) / 2, x2, y2, (x2 + x3) / 2, (y2 + y3) / 2, depth - 1)
  405. sierpinski_triangle(cv, (x1 + x3) / 2, (y1 + y3) / 2, (x2 + x3) / 2, (y2 + y3) / 2, x3, y3, depth - 1)
  406. def draw_sierpinski_triangle():
  407. sierpinski_triangle(cv, 0, 0, width, 0, width / 2, height, 5)
  408. cv.update()
  409.  
  410. Perlin Noise: This algorithm generates a random, flowing pattern by interpolating between a set of randomly generated control points.
  411. def perlin_noise():
  412. control_points = [[random.randint(0, width), random.randint(0, height)] for _ in range(5)]
  413. for x in range(width):
  414. for y in range(height):
  415. color = 0
  416. for i, (cx, cy) in enumerate(control_points):
  417. dist = math.sqrt((x - cx) ** 2 + (y - cy) ** 2)
  418. color += 255 / (i + dist)
  419. cv.create_line(x, y, x, y, fill=color)
  420. cv.update()
  421.  
  422. Spirograph: This algorithm generates a spirograph by drawing a line that traces the path of a point rotating around a fixed point.
  423. def spirograph(cv, x, y, r, R, O):
  424. theta = 0
  425. while True:
  426. x1 = (R - r) * math.cos(theta) + O * math.cos((R - r) / r * theta) + x
  427. y1 = (R - r) * math.sin(theta) - O * math.sin((R - r) / r * theta) + y
  428. cv.create_line(x, y, x1, y1, fill=random.choice(colors))
  429. x, y = x1, y1
  430. theta += 0.1
  431. cv.update()
  432.  
  433. Koch Snowflake: This algorithm generates a Koch snowflake by recursively drawing lines and forming an equilateral triangle.
  434. def koch_snowflake(cv, x1, y1, x2, y2, depth):
  435. if depth:
  436. x3 = (x1 + x2) / 2 + (y2 - y1) / math.sqrt(3)
  437. y3 = (y1 + y2) / 2 - (x2 - x1) / math.sqrt(3)
  438. x4 = (x1 + x3) / 2 + (y3 - y1) / 2
  439. y4 = (y1 + y3) / 2 - (x3 - x1) / 2
  440. koch_snowflake(cv, x1, y1, x4, y4, depth - 1)
  441. koch_snowflake(cv, x4, y4, x3, y3, depth - 1)
  442. koch_snowflake(cv, x3, y3, x2, y2, depth - 1)
  443. def draw_koch_snowflake():
  444. cv.create_line(0, height / 2, width / 2, 0, fill='white')
  445. cv.create_line(width / 2, 0, width, height / 2, fill='white')
  446. cv.create_line(width, height / 2, width / 2, height, fill='white')
  447. cv.create_line(width / 2, height, 0, height / 2, fill='white')
  448. koch_snowflake(cv, 0, height / 2, width / 2, 0, 5)
  449. koch_snowflake(cv, width / 2, 0, width, height / 2, 5)
  450. koch_snowflake(cv, width, height / 2, width / 2, height, 5)
  451. koch_snowflake(cv, width / 2, height, 0, height / 2, 5)
  452. cv.update()
  453.  
  454. Sierpinski Triangle: This algorithm generates a Sierpinski triangle by recursively dividing a triangle into smaller triangles and coloring them according to a predetermined pattern.
  455. def sierpinski_triangle(cv, x1, y1, x2, y2, x3, y3, depth):
  456. if depth:
  457. x4 = (x1 + x2) / 2
  458. y4 = (y1 + y2) / 2
  459. x5 = (x2 + x3) / 2
  460. y5 = (y2 + y3) / 2
  461. x6 = (x1 + x3) / 2
  462. y6 = (y1 + y3) / 2
  463. sierpinski_triangle(cv, x1, y1, x4, y4, x6, y6, depth - 1)
  464. sierpinski_triangle(cv, x4, y4, x2, y2, x5, y5, depth - 1)
  465. sierpinski_triangle(cv, x6, y6, x5, y5, x3, y3, depth - 1)
  466. else:
  467. cv.create_line(x1, y1, x2, y2, fill='white')
  468. cv.create_line(x2, y2, x
  469.  
  470. Dragon Curve: This algorithm generates a dragon curve by alternating between turning left and right and drawing a line.
  471. def dragon_curve(cv, x1, y1, x2, y2, depth):
  472. if depth:
  473. x3 = x1 + (x2 - x1) / math.sqrt(2)
  474. y3 = y1 + (y2 - y1) / math.sqrt(2)
  475. x4 = x1 + (x2 - x1) / 2 - (y2 - y1) / 2
  476. y4 = y1 + (y2 - y1) / 2 + (x2 - x1) / 2
  477. dragon_curve(cv, x3, y3, x4, y4, depth - 1)
  478. dragon_curve(cv, x4, y4, x2, y2, depth - 1)
  479. else:
  480. cv.create_line(x1, y1, x2, y2, fill='white')
  481. def draw_dragon_curve():
  482. dragon_curve(cv, 0, height / 2, width, height / 2, 10)
  483. cv.update()
  484.  
  485. Julia Set: This algorithm generates a Julia set by iterating over a complex plane and using a color map to determine the color of each point based on the number of iterations it takes to escape the set.
  486. def julia():
  487. c = complex(-0.4, 0.6)
  488. for x in range(-width, width):
  489. for y in range(-height, height):
  490. z = complex(x * 4 / width, y * 4 / height)
  491. for i in range(255):
  492. if abs(z) > 2:
  493. break
  494. z = z * z + c
  495. cv.create_line(x, y, x, y, fill=color_map[i])
  496. cv.update()
  497.  
  498. Barnsley Fern: This algorithm generates a Barnsley fern by applying a set of rules to a starting point to determine the next point.
  499. def barnsley_fern():
  500. x = 0
  501. y = 0
  502. for i in range(10000):
  503. r = random.random()
  504. if r < 0.01:
  505. x = 0
  506. y = 0.16 * y
  507. elif r < 0.86:
  508. x = 0.85 * x + 0.04 * y
  509. y = -0.04 * x + 0.85 * y + 1.6
  510. elif r < 0.93:
  511. x = 0.2 * x - 0.26 * y
  512. y = 0.23 * x + 0.22 * y + 1.6
  513. else:
  514. x = -0.15 * x + 0.28 * y
  515. y = 0.26 * x + 0.24 * y + 0.44
  516. cv.create_line(x * 10 + width / 2, -y * 10 + height / 2, x * 10 + width / 2, -y * 10 + height / 2, fill='green')
  517. cv.update()
  518.  
  519. Spirograph: This algorithm generates a spirograph by drawing a line that rotates around a fixed point.
  520. def spirograph(cv, x1, y1, r1, r2, p):
  521. theta = 0
  522. while True:
  523. x2 = (r1 + r2) * math.cos(theta) - p * math.cos((r1 + r2) * theta / r2) + x1
  524. y2 = (r1 + r2) * math.sin(theta) - p * math.sin((r1 + r2) * theta / r2) + y1
  525. cv.create_line(x1, y1, x2, y2, fill='red')
  526. theta += 0.01
  527. x1 = x2
  528. y1 = y2
  529. cv.update()
  530. def draw_spirograph():
  531. spirograph(cv, width / 2, height / 2, 100, 50, 50)
  532.  
  533. Random Walk: This algorithm generates a random walk by moving a point in a random direction and drawing a line to the new position.
  534. def random_walk():
  535. x = width / 2
  536. y = height / 2
  537. while True:
  538. x += random.choice([-1, 0, 1])
  539. y += random.choice([-1, 0, 1])
  540. cv.create_line(x, y, x + 1, y + 1, fill='blue')
  541. cv.update()
  542.  
  543. L-System: This algorithm generates a pattern using an L-system by applying a set of rules to a starting string and iteratively replacing characters according to the rules.
  544. def l_system():
  545. axiom = 'F'
  546. rules = {'F': 'F+F-F-F+F'}
  547. iterations = 5
  548. for i in range(iterations):
  549. next_string = ''
  550. for char in axiom:
  551. if char in rules:
  552. next_string += rules[char]
  553. else:
  554. next_string += char
  555. axiom = next_string
  556. for char in axiom:
  557. if char == 'F':
  558. cv.create_line(x, y, x + 1, y, fill='white')
  559. x += 1
  560. elif char == '+':
  561. x, y = y, -x
  562. elif char == '-':
  563. x, y = -y, x
  564. cv.update()
  565.  
  566. Percolation: This algorithm simulates percolation by randomly coloring cells and checking if they are connected to a previously colored cell. If they are, they are colored the same color as the connected cell.
  567. def percolation():
  568. grid = [[random.choice(colors) for _ in range(width)] for _ in range(height)]
  569. while True:
  570. for i in range(1, width - 1):
  571. for j in range(1, height - 1):
  572. if grid[i][j] == grid[i-1][j]:
  573. grid[i][j] = grid[i-1][j]
  574. elif grid[i][j] == grid[i][j-1]:
  575. grid[i][j] = grid[i][j-1]
  576. for i in range(1, width - 1):
  577. for j in range(1, height - 1):
  578. cv.create_line(i, j, i + 1, j + 1, fill=grid[i][j])
  579. cv.update()
  580.  
  581. Fireworks Display: This algorithm generates a fireworks display by creating a "rocket" that explodes into a circle of sparks when it reaches the top of the screen.
  582. import random
  583. def fireworks():
  584. x = random.randint(0, width)
  585. y = height
  586. vx = random.uniform(-2, 2)
  587. vy = random.uniform(-10, -5)
  588. while y > 0:
  589. cv.create_line(x, y, x, y, fill='red')
  590. x += vx
  591. y += vy
  592. vy += 0.5
  593. cv.update()
  594. time.sleep(0.01)
  595. for i in range(360):
  596. cv.create_line(x, y, x + math.cos(i) * 20, y + math.sin(i) * 20, fill=random.choice(colors))
  597. cv.update()
  598. time.sleep(0.01)
  599.  
  600. Spirograph: This algorithm generates a spirograph by drawing a series of circles with decreasing radii and alternating colors.
  601. import math
  602. def spirograph():
  603. for i in range(360):
  604. cv.create_line(width / 2, height / 2, width / 2 + math.cos(i) * radius, height / 2 + math.sin(i) * radius, fill=random.choice(colors))
  605. radius -= 1
  606. cv.update()
  607. time.sleep(0.01)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement