Search code examples
c#algorithmgeometrycomputational-geometry

Algorithm - Find the midpoint of an arc


In the diagram below, I need to find the midpoint M of the arc from A to B:

midpoint

I want to find M in terms of the following information:

  • A.X and A.Y, the coordinates of A
  • B.X and B.Y, the coordinates of B
  • Radius, the radius of the arc
  • Center.X and Center.Y, the center of the arc

How do I compute the coordinates of M?


Solution

  • Assuming A, B, M and Center are objects of some vector type with the usual operations:

    var a = A-C;
    var b = B-C;
    var m = a+b;
    

    m is a vector that goes from Center towards M. Therefore:

    m = m.Normalize() * Radius;
    M = Center + m;
    

    Please note: This algorithm does not assume an order of A and B, and always interprets the arc as the smaller of the two possible ones. Without adding special cases, it can only handle arcs with an angle smaller than 180°.

    To handle order: First calculate the angle from a to b using atan2:

    var angle = Math.Atan2(b.y, b.x) - Math.Atan2(a.y, a.x);
    if (angle < 0)
    {
        angle = 2*Math.PI + angle;
    }
    

    Then rotate a by half that angle:

    angle /= 2;
    var m = a.Rotate(angle);
    M = Center + m;