4 ms·
This is nice. I think it has appeared before on HN. A nitpick is that the piano keys are drawn incorrectly; on a real piano the black keys are not centered betw
by bambax 4y ago
This is nice. I think it has appeared before on HN. A nitpick is that the piano keys are drawn incorrectly; on a real piano the black keys are not centered between the white keys (most important), and they are also wider and longer.
- keymasta 4y agoHere are two ways to get the keys' measurements, which I made/use. The tricky part from a naive approach is that white keys are on a grid of seven whereas black keys are on a grid of twelve. I copypasta'ed from my code so I hope it's still correct. This is Python but it should be trivial to use any language. evenlySpacedWhites = True octaveWidthProportion = 2 #Piano keys longer/shorter blackKeyProportion = 0.61832 #Black keys longer/shorter height = 256 #This can be any number and controls total size if evenlySpacedWhites: CtoEWhites = 35 FtoBWhites = 35 CtoEBlacks = 21 FtoBBlacks = 20 else: CtoEWhites = 20 FtoBWhites = 21 CtoEBlacks = 12 FtoBBlacks = 12 #before normalisation octaveNumberWidth = 3 * CtoEWhites + 4 * FtoBWhites #using the proportion makes the output image have the same height, regardless if whether evenlySpacedWhites is True/False octaveWidth = round(height * octaveWidthProportion) proportion = octaveWidth / octaveNumberWidth CtoEWhites = round(CtoEWhites * proportion) FtoBWhites = round(FtoBWhites * proportion) CtoEBlacks = round(CtoEBlacks * proportion) FtoBBlacks = round(FtoBBlacks * proportion) blackKeyLength = round(height * blackKeyProportion)