import pygame import numpy as np import random maxx = 800 maxy = 600 maxx = 200 maxy = 150 # Mögliche Gittertypen: # 1 : hexagonales Gitter # 2 : trigonales Gitter # 3 : Rautengitter # 4 : Quadratgitter gittertyp = 1 einheit = 60 radius_punkt = 8 dicke_kante = 4 schriftgroesse = 20 epsilon = 0.4*einheit rand = radius_punkt weiter_per_tastendruck = False pausendauer = 200 pygame.init() leinwand = pygame.display.set_mode((maxx+2*rand, maxy+2*rand)) pygame.display.set_caption('Planarer Graph') leinwand.fill('black') pygame.display.update() def schreibe(x, y, text): # schrift = pygame.font.Font('freesansbold.ttf', round(schriftgroesse)) # Mit pygame.font.SysFont kann man wohl jede der Fonts aus der Liste # pygame.font.get_fonts() # verwenden. schrift = pygame.font.SysFont('freemono', round(schriftgroesse)) # print(text, type(text)) formatierter_text = schrift.render(text, True, 'white', 'black') # formatierter_text.set_alpha(127) rechteck = formatierter_text.get_rect() # rechteck.center = leinwandpunkt(x, y) rechteck.center = (x, y) # print(dir(rechteck)) leinwand.blit(formatierter_text, rechteck) if gittertyp in {1, 2, 3}: v = einheit * np.array([1, 0]) w = einheit * np.array([0.5, 3**0.5/2]) elif gittertyp == 4: v = einheit * np.array([1, 0]) w = einheit * np.array([0, 1]) offset = np.array([rand+epsilon, rand+epsilon]) def koordinaten(x, y): return x*v+y*w + offset punkte_vw_koordinaten = [] y = 0 while koordinaten(0, y)[1] < maxy+2*rand: x = -y while koordinaten(x, y)[0] < maxx+2*rand: if rand+epsilon <= koordinaten(x, y)[0] < maxx+2*rand-epsilon and rand+epsilon <= koordinaten(x, y)[1] < maxy+2*rand-epsilon: if gittertyp == 1: if (x-y) % 3 != 0: punkte_vw_koordinaten.append([x, y]) else: punkte_vw_koordinaten.append([x, y]) x += 1 y += 1 # print(punkte_vw_koordinaten) kanten = [] e = np.array([1, 0]) f = np.array([0, 1]) if gittertyp in {1, 2, 3}: nachbarvektoren = [e, -e, f, -f, -e+f, e-f] elif gittertyp == 4: nachbarvektoren = [e, -e, f, -f] for nr, p in enumerate(punkte_vw_koordinaten): x, y = p if (gittertyp in {1, 3} and (x-y) % 3 == 1) or gittertyp in {2, 4}: for n in nachbarvektoren: q = p + n for j, pp in enumerate(punkte_vw_koordinaten): if q[0] == pp[0] and q[1]==pp[1]: kante_vorhanden = False for k in kanten: if {k[0], k[1]} == {nr, j}: kante_vorhanden = True break if not kante_vorhanden: kanten.append([nr, j]) break # try: # # print("Hallo") # # print(p, q) # print(punkte.count(q)) # j = punkte.index(q) # print(j) # kanten.append([nr, j]) # # print(kanten) # except ValueError: # pass leinwandpunkte = [] for p in punkte_vw_koordinaten: stoerung = np.array([round(epsilon*(1-2*random.random())), round(epsilon*(1-2*random.random()))]) leinwandpunkte.append(koordinaten(*p)+stoerung) # print(kanten) kantenlaengen = [] for nr, k in enumerate(kanten): i, j = k pygame.draw.line(leinwand, 'blue', leinwandpunkte[i], leinwandpunkte[j], width=dicke_kante) laenge = round(np.linalg.norm(leinwandpunkte[i]-leinwandpunkte[j])) kantenlaengen.append(laenge) x, y = (leinwandpunkte[i]+leinwandpunkte[j])/2 # schreibe(x, y, f'{laenge:.0f}') schreibe(x, y, f'{laenge}') for nr, p in enumerate(punkte_vw_koordinaten): x, y = p if (x-y) % 3 == 1: pygame.draw.circle(leinwand, 'red', leinwandpunkte[nr], radius_punkt, width=0) elif (x-y) % 3 == 2: pygame.draw.circle(leinwand, 'yellow', leinwandpunkte[nr], radius_punkt, width=0) else: pygame.draw.circle(leinwand, 'orange', leinwandpunkte[nr], radius_punkt, width=0) def male_punkt(i, farbe='red', radius=radius_punkt): pygame.draw.circle(leinwand, farbe, leinwandpunkte[i], radius_punkt) def male_kante(i, j, farbe='blue', dicke=dicke_kante): pygame.draw.line(leinwand, farbe, leinwandpunkte[i], leinwandpunkte[j], width=dicke) # if adjacent[i][j] > 0: # pygame.draw.line(leinwand, farbe, leinwandpunkte[i], leinwandpunkte[j], width=dicke) # else: # print('Fehler') # Anzahl der Knoten n = len(punkte_vw_koordinaten) adjacent = [[0 for j in range(n)] for i in range(n)] for nr, k in enumerate(kanten): i, j = k adjacent[i][j] = adjacent[j][i] = kantenlaengen[nr] print(adjacent) UNENDLICH = 1000000 minimum = UNENDLICH mini = UNENDLICH minj = UNENDLICH for i in range(n): for j in range(n): if adjacent[i][j] != 0 and adjacent[i][j] < minimum: minimum = adjacent[i][j] mini = i minj = j print("Hallo") baumknoten = [mini, minj] male_kante(mini, minj, 'cyan', 2*dicke_kante) pygame.display.update() pygame.time.wait(pausendauer) print("Ciao") while len(baumknoten) < n: ################### # Hier Code ergänzen. ################### pygame.display.update() if not weiter_per_tastendruck: pygame.time.wait(pausendauer) else: tastendruck = False while not tastendruck: for ereignis in pygame.event.get(): if ereignis.type == pygame.KEYDOWN: tastendruck = True beenden = False while not beenden: pygame.display.update() for ereignis in pygame.event.get(): if ereignis.type == pygame.QUIT: beenden = True elif ereignis.type == pygame.KEYDOWN: if ereignis.key in {pygame.K_ESCAPE, pygame.K_q}: beenden = True elif ereignis.type == pygame.MOUSEBUTTONDOWN: beenden = True pygame.quit()