import tkinter as tk
from math import *

################
##            ##
## Exercice 3 ##
##            ##
################


def affiche_pixel(x,y,couleur):
    Dessin.create_rectangle(x,y,x,y,fill=couleur,outline='')

## Question 3.1

def distance(p,q):
    (x1,y1)=p
    (x2,y2)=q
    return ((x1-x2)**2 + (y1-y2)**2)**.5

## Question 3.2

def dist2(p,q):
    (x1,y1)=p
    (x2,y2)=q
    return (x1-x2)**2 + (y1-y2)**2


## Question 3.3
def disque(x0,y0,r,couleur):
    centre = (x0,y0)
    for x in range(Largeur):
        for y in range(Hauteur):
            p=(x,y)
            if dist2(centre,p)<=r**2:
                affiche_pixel(x,y,couleur)

## Question 3.4
def disque(x0,y0,r,couleur):
    centre = (x0,y0)
    for x in range(x0-r, x0+r+1):
        for y in range(y0-r, y0+r+1):
            p=(x,y)
            if dist2(centre,p)<=r**2:
                affiche_pixel(x,y,couleur)


## Question 3.5
def cercle(x0,y0,r,couleur):
    centre = (x0,y0)
    for x in range(x0-r, x0+r+1):
        for y in range(y0-r, y0+r+1):
            p=(x,y)
            d2=dist2(centre,p)
            if  (r-0.5)**2 <= d2 and d2 <= (r+0.5)**2:
                affiche_pixel(x,y,couleur)

## Question 3.6






################
##            ##
## Exercice 4 ##
##            ##
################

class Cercle:
    def __init__(self,x,y,r,c):
        self.x = x
        self.y = y
        self.rayon = r
        self.couleur = c


    def afficher_cercle(self):
        cercle(self.x,self.y,self.rayon,self.couleur)

    def afficher_disque(self):
        disque(self.x,self.y,self.rayon,self.couleur)





################
##            ##
## Exercice 5 ##
##            ##
################

def couleur_rgb(r,g,b):
    d = '0123456789ABCDEF'
    rr = d[r//16]+d[r%16]
    gg = d[g//16]+d[g%16]
    bb = d[b//16]+d[b%16]
    return '#' + rr + gg + bb





################
##            ##
## Exercice 6 ##
##            ##
################



root = tk.Tk()
root.title("TD 4 —  Exercice 6")

Dessin = tk.Canvas(root,height=500,width=1000,bg="#FFAAAA")
Dessin.pack()

## Question 6.1

def gris(i):
    g = (49-i)*255//49
    return couleur_rgb(g,g,g)

## Question 6.2

def une_nuance():
    for i in range(5):
        for j in range(10):
            g="#000000"
            disque(100*j+50,100*i+50,30,g)


## Question 6.3

def cinquante_nuances():
    n=0
    for i in range(5):
        for j in range(10):
            g=gris(n)
            if i%2==0:
                disque(100*j+50,100*i+50,30,g)
            else:
                disque(100*(10-j-1)+50,100*i+50,30,g)
            n=n+1

cinquante_nuances()

root.mainloop()





################
##            ##
## Exercice 7 ##
##            ##
################

root = tk.Tk()
root.title("TD 4 — Exercice 7")

Dessin = tk.Canvas(root,height=500,width=500,bg='white')
Dessin.pack()


def dessine_montagnes(p0,n,h,l):
    (x,y)=p0
    dx = .5*l/n
    for i in range(1,n+1) :
        p=(x,y)
        (x,y) = (x+dx, y-h)
        Dessin.create_line(p,(x,y))
        p=(x,y)
        (x,y) = (x+dx, y+h)
        Dessin.create_line(p,(x,y))

dessine_montagnes((10,100),2,50,300)
root.mainloop()





################
##            ##
## Exercice 8 ##
##            ##
################

root = tk.Tk()
root.title("TD 4 — Exercice 8")

Dessin = tk.Canvas(root,height=500,width=500,bg='white')
Dessin.pack()


def coordonnées(p):
    (x,y)=p
    return (x+250, -y+250)

def ligne(p,q):
    p1 = coordonnées(p)
    q1 = coordonnées(q)
    Dessin.create_line(p1,q1)

def paramétrique(f,g,tmin,tmax,dt):
    def p(t): return (f(t),g(t))
    t=tmin
    while t<tmax:
        ligne(p(t),p(t+dt))
        t=t+dt

def f(t): return 50*sin(t)
def g(t): return 50*sin(2*t)
paramétrique(f,g,-pi,pi,pi/1000)

root.mainloop()
