calculate a perpendicular offset from a diagonal line

前端 未结 3 857
小蘑菇
小蘑菇 2020-12-09 06:10

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.

3条回答
  •  执念已碎
    2020-12-09 06:21

    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));
    }
    

提交回复
热议问题