In order to practice my programming skills, I am trying to implement a Button class, to use in pygame. I know there are libraries like PGU, I do this to improve in programming.
I spent a bit of time creating a function to find a good font size for the text inside the button, depending on the length of the text and the dimensions of the button.
I came to this result, it's close enough to what I want. However, I am looking for advice to improve this function that looks wobbly:
def balanced_text_size_button(length, width, height): # sqrt(length) / length -> The higher the length is, the smaller this value become, # it decrease slower and slower as length increase (never reach 0). # pow(....., 1.6) -> The resulting value is multiplied, # the impact is more important on high value # (I don't think it makes much sense I found this empirically). length_factor = math.pow((math.sqrt(length) / length), 1.6) # If there is a lot of horizontal space for the text, the font will be big, # but if there is a lot of char to write it will be smaller. font_size = width * length_factor # This way the font is not too high if font_size > height: font_size = height * 0.9 return int(font_size)