from kandinsky import * from ion import * import random import time # This is a SO code at this adress: # https://stackoverflow.com/questions/24852345/hsv-to-rgb-color-conversion def tuple2rgb(*t): return "#" + "".join(["{:02x}".format(int(i)) for i in t]) def hsv_to_rgb(h, s, v): if s == 0.0: v *= 255 return tuple2rgb(v, v, v) i = int(h * 6.) f = (h * 6.) - i p, q, t = int(255*(v*(1.-s))),\ int(255*(v*(1.-s*f))),\ int(255*(v*(1.-s*(1-f)))) v *= 255 i %= 6 if i == 0: return tuple2rgb(v, t, p) if i == 1: return tuple2rgb(q, v, p) if i == 2: return tuple2rgb(p, v, t) if i == 3: return tuple2rgb(p, q, v) if i == 4: return tuple2rgb(t, p, v) if i == 5: return tuple2rgb(v, p, q) def fy(tab): n = len(tab) for i in range(n - 1): j = random.randint(i, n - 1) tab[i], tab[j] = tab[j], tab[i] def tri_insertion(tab, f=lambda x: None): n = len(tab) for i in range(1, n): c = tab[i] p = i - 1 while p >= 0 and tab[p] > c: tab[p + 1] = tab[p] p -= 1 f(tab) tab[p + 1] = c f(tab) def tri_selection(tab, f=lambda x: None): n = len(tab) for i in range(n - 1): ipp = i for j in range(i + 1, n): if tab[j] < tab[ipp]: ipp = j if i != ipp: tab[i], tab[ipp] = tab[ipp], tab[i] f(tab) def tri_bulles(tab, f=lambda x: None): n = len(tab) for i in range(n - 1): ipg = 0 for j in range(1, n - i): if tab[j] > tab[ipg]: ipg = j tab[ipg], tab[n - i - 1] = tab[n - i - 1], tab[ipg] f(tab) def trace_etat(tab): xoffset = yoffset = 11 fill_rect(xoffset, yoffset, 200, 200, (255, 255, 255)) largeur = 200 // len(tab) mx = max(tab) w = round(largeur * .8) for i in range(len(tab)): x = xoffset + i * largeur y = yoffset trace_barre(x, y, tab[i], w, mx) def trace_barre(x, y, n, w, nmax): p = n / nmax h = round(p * 200) c = hsv_to_rgb(p, .75, 1) fill_rect(x, 222 - (y + h), w, h, c) def animation(tab): global DELAI if keydown(KEY_PLUS): DELAI -=.02 elif keydown(KEY_MINUS): DELAI += .02 trace_etat(tab) time.sleep(DELAI) def liste_choix(): for i in range(len(TRIS)): x = 200 + 2*11 y = 11 + 20*i if i == CHOIX: draw_string(TRIS[i], x, y, (255, 255, 255), (255, 0, 0)) else: draw_string(TRIS[i], x, y, (255, 0, 0)) draw_string("+/- vit.", x, 222 - 11 - 90, hsv_to_rgb(1,.5,1)) draw_string("mélange", x, 222 - 11 - 60, hsv_to_rgb(1,.5,1)) draw_string("avec", x, 222 - 11 - 40, hsv_to_rgb(1,.5,1)) draw_string("OK", x, 222 - 11 - 20, hsv_to_rgb(1,.5,1)) def change_choix(): fill_rect(200 + 2*11, 0, 320 - 200 - 2*11, 222, (255, 255, 255)) liste_choix() tab = list(range(1, 30 + 1)) CHOIX = 0 DELAI = .3 TRIS = ["insertion", "sélection", "bulles"] liste_choix() fy(tab) trace_etat(tab) while True: if keydown(KEY_EXE): if CHOIX == 0: tri_insertion(tab, animation) elif CHOIX == 1: tri_selection(tab, animation) elif CHOIX == 2: tri_bulles(tab, animation) if keydown(KEY_OK): fy(tab) trace_etat(tab) if keydown(KEY_DOWN): CHOIX += 1 CHOIX %= len(TRIS) change_choix() if keydown(KEY_UP): CHOIX -= 1 CHOIX %= len(TRIS) change_choix() time.sleep(.05)