I'm trying to program a musical stave and have come against the problem of where to place a note, given its index.
The index of a note is defined from a model created from the table in the following: Piano Note Frequencies
For example, in the model, C4 has an index of 40 and D#5 an index of 55.
The text in red shows the indices the stave has available to put notes (disregard the horizontal position). Only a few indices are shown, however they extend much further. For example, placing a note on the stave at index 0, it appears at A4 on the stave.
I'm having difficulty translating from an index in the note model, to an index on the musical stave.
At first, I've done a (knowingly) naive implementation. Index 0 in the stave corresponds to A4, or index 49 in the note model. Given an index from the model, calculate how many half steps away the index is and use this as the index in the stave. For example, C5 is index 52 and gives a stave index of 3. Looking at the above image, it's clear this is wrong for two reasons.
Firstly, we didn't account for a note and its sharp laying on the same index. Secondly, we didn't account that B4 and C5 have one half step between them, but are in different places on the stave.
I've been scratching my head over this for a while. What are the rules to follow for translating from a note index to a position on the stave?
EDIT: Each note in the note model knows whether it is sharp or not, its pitch (e.g. C, D, E, etc), and octave.