After two weeks of summer school in South Tyrol I am back at home and can continue working on Zong!. Today I finished the first version of an automatic layout engine for slurs. I thought that this would be an easy task, but instead of plain if-then-else-code it turned out that I would need some math to solve this task. There are some requirements which have to be fulfilled:
- The slur may not touch note heads
- The slur must have a certain amount of curvature
- The slur should cling to the notes as close as possible
- And of course the slur should look nice
After some discussion, also with mathematicians and physicist (thanks to Uli, Christian, Phil, Daniel), I finally found a first solution that looks fine for most cases. Here is the result (simplified display):
Play around with it too see how it works. These are the basic ideas:
- Compute a polygon over the notes and convert it to a convex one
- Find a quadratic curve that minimizes the area between the curve and the polygon (using linear programming and solving some SLEs)
- Approximate this curve by a cubic bezier curve. If it is too flat, increase its curvature.
The result must be a cubic bezier curve to ensure compatibility to other notation programs and MusicXML.