I am writing a music display program and need to draw a 'slur' between two notes. A slur is a curved line linking two notes - just to be clear.
I know the note positions and calculate where the start and end points of the curve should be - Start point A and End point B.
I now need to obtain the offset C, given the distance required, for use within a quadratic curve. This is where my, very, limited knowledge and understanding of maths formulae comes in.
I have indeed looked here in SO for my answer, but the solutions proposed either do not work or I am too limited to code them correctly.
Can someone help me with the calculation, in a NON mathematical form ?
The offset curve is drawn by evaluating the tangent at the end of each of these straight line segments, dividing by the length of the tangent, rotating 90 degrees to get the unit normal, then multiplying by the appropriate offset.
I took legends2k excellent answer and converted to Java on Android. This might help someone save some time.
private PointF getPerpendicularPoint(int startX, int startY, int stopX, int stopY, float distance)
{
PointF M = new PointF((startX + stopX) / 2, (startY + stopY) / 2);
PointF p = new PointF(startX - stopX, startY - stopY);
PointF n = new PointF(-p.y, p.x);
int norm_length = (int) Math.sqrt((n.x * n.x) + (n.y * n.y));
n.x /= norm_length;
n.y /= norm_length;
return new PointF(M.x + (distance * n.x), M.y + (distance * n.y));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With