python_art_1.py

Created by cent20

Created on November 17, 2021

1.93 KB

De l’art en python avec le module matplotlib.pyplot par Christophe Navaux


# Script porté sur NumWorks par Xavier Andréani.
# Idée originale de Christophe Navaux
# https://twitter.com/tiplanetnews/status/1460996945867116547

import math
from kandinsky import set_pixel
#import matplotlib.pyplot as plt

def draw_line(x1, y1, x2, y2, c):
  m, a1, b1, a2, b2 = 0, int(x1), int(y1), int(x2), int(y2)
  if (x2 - x1) ** 2 < (y2 - y1) ** 2:
    m, a1, a2, b1, b2 = 1, b1, b2, a1, a2
  if min(a1, a2) != a1: a1, b1, a2, b2 = a2, b2, a1, b1
  for a in range(a1, a2 + 1):
    b = int(b1 + (b2 - b1) * (a - a1) / ((a2 - a1) or 1))
    set_pixel((a, b)[m], (b, a)[m], c)

def u(n):
    return (n+0.15)*math.sqrt(n)
    
def coords_2_pixels(x, y):
  k=.55
  return x*k+160, 98-y*k

def plot(n):
  x, y = 0, 0
  px, py = coords_2_pixels(x, y)
  for k in range (1,n+1):
    x_old, y_old, px_old, py_old = x, y, px, py
    x = x + math.cos(2*math.pi*u(k))
    y = y + math.sin(2*math.pi*u(k))
    px, py = coords_2_pixels(x, y)
    draw_line(px_old, py_old, px, py, "#745cf9")
 

#Programme principal
#N représente le nombre de points tracés
N = 100000
plot(N)

# script de Christophe Navaux, @AlgoMaths 
# https://twitter.com/AlgoMaths/status/1459863863466336257
"""
import math   
import matplotlib.pyplot as plt

#fonction qui renvoie le terme de rang n de la suite u
def u(n):

    u = (n+0.15)*math.sqrt(n)
    return u

#fonction qui renvoie les n premiers termes de la suite x    
def x(n):
    x = 0
    L = [0]
    for k in range (1,n+1):
        x = x + math.cos(2*math.pi*u(k))
        L.append(x)
    return L

#fonction qui renvoie les n premiers termes de la suite y 
def y(n):
    y = 0
    L = [0]
    for k in range (1,n+1):
        y = y + math.sin(2*math.pi*u(k))
        L.append(y)
    return L


#Programme principal
#N représente le nombre de points tracés
N=115 # plante si N > 115, N = 100000 requis pour un beau rendu :-()
plt.axis('off') # neutralisation des axes
plt.plot(x(N),y(N),linewidth=1)
plt.show()
"""