Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calculate a perpendicular offset from a diagonal line

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.

enter image description here

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 ?

like image 685
Simon Avatar asked Jun 19 '13 15:06

Simon


People also ask

How do you find the offset of a curve?

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.


1 Answers

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));
}
like image 61
David Boyd Avatar answered Oct 31 '22 18:10

David Boyd