dynamische Variablennamen
Verfasst: Sonntag 29. Oktober 2023, 14:03
Ich hatte dieses Problem schon mal gepostet, bin mit meinem Code so nicht zufrieden.
Ich erzeuge Koordinaten für Winkel in svg. Mal brauche ich die Schenkel, mal die Winkelbögen, meistens beides. Ich erzeuge diese mit der Funktion:
Die Liste mit den Koordinaten für einen 30° Winkel mit Schenkeln sieht dann z.B. so aus:
Hier sind es 17 Einträge. Ich brauche aber oft 2, 3 oder auch vier Winkel und muss die Werte unterscheiden. Bei vier Winkeln sind das jetzt schon 68 Einträge. Ich möchte diese Gruppen jeweils mit einer ID versehen. Also z.B. "center_x_1", "center_x_2"... Diese dynamische Benennung von Variablen scheint in Python nicht vorgesehen zu sein. Ich habe mir, nach dem letzten Posting, mit "if id == 1 ... elif id ==2 ... geholfen und die Koordinaten einzeln benannt. Das sieht blöd aus. Habt ihr da keine Idee?
Ich erzeuge Koordinaten für Winkel in svg. Mal brauche ich die Schenkel, mal die Winkelbögen, meistens beides. Ich erzeuge diese mit der Funktion:
Code: Alles auswählen
def winkel_koo(center_x, center_y, radius, winkel, startwinkel, color = "None", symbol = "", schenkel = 0, scheitel = False, id = 0):
rad_start = math.radians(startwinkel)
rad = math.radians(winkel)
koordinaten = dict(center_x = center_x, center_y = center_y, )
# das sind die Schenkel:
if schenkel > 0:
x1 = center_x - schenkel * math.cos(rad_start)
y1 = center_y - schenkel * math.sin(rad_start)
x2 = center_x - schenkel * math.cos(rad+rad_start)
y2 = center_y - schenkel * math.sin(rad+rad_start)
if scheitel == True:
x3 = center_x + schenkel * math.cos(rad_start)
y3 = center_y + schenkel * math.sin(rad_start)
x4 = center_x + schenkel * math.cos(rad+rad_start)
y4 = center_y + schenkel * math.sin(rad+rad_start)
schenkel_koo = dict(schenkel_1_x = x3, schenkel_1_y = y3, schenkel_2_x = x4, schenkel_2_y = y4)
else:
schenkel_koo = dict(schenkel_1_x = x1, schenkel_1_y = y1, schenkel_2_x = x2, schenkel_2_y = y2)
koordinaten.update(schenkel_koo)
# das ist der Bogen mit Text:
if color:
start_x = center_x - radius * math.cos(rad_start)
start_y = center_y - radius * math.sin(rad_start)
end_x = center_x - radius * math.cos(rad+rad_start)
end_y = center_y - radius * math.sin(rad+rad_start)
if winkel <=180:
largeArcFlag = 0
else:
largeArcFlag = 1
text_x = center_x - radius*3/4 * math.cos(rad/2+rad_start)
text_y = center_y - radius/2 * math.sin(rad/2+rad_start)
bogen_koo = dict(bogen_radius = radius, sweep_flag = 1, color = color, symbol = symbol,
start_bogen_x = start_x, start_bogen_y = start_y, end_bogen_x = end_x, end_bogen_y = end_y, largeArcFlag = largeArcFlag,
text_x = text_x, text_y = text_y, )
koordinaten.update(bogen_koo)
return koordinaten
Code: Alles auswählen
{'center_x': 200, 'center_y': 100, 'schenkel_1_x': 248.2962913144534, 'schenkel_1_y': 87.05904774487395, 'schenkel_2_x': 248.2962913144534, 'schenkel_2_y': 112.94095225512602, 'bogen_radius': 40, 'sweep_flag': 1, 'color': 'lightskyblue', 'symbol': 'β', 'start_bogen_x': 238.63703305156272, 'start_bogen_y': 89.64723819589916, 'end_bogen_x': 238.63703305156275, 'end_bogen_y': 110.35276180410081, 'largeArcFlag': 0, 'text_x': 230.0, 'text_y': 100.0}