Search code examples
c#wpfalgorithmautomatic-ref-countingrounded-corners

Algorithm for rounding a corner between line and arc


I need an algorithm that can round a corner between a line and an arc. The start information that I have is P0-start point, P-corner point, P2-end point, R2-radius of the arc between P and P2 and R-radius of the rounded corner(on the second picture).

Corner between line and arc

Output or wanted points are cross sections C0 and C2 and center point of the rounding circle-O

Rounded corner between line and arc


Solution

  • In my sketch BF is part of given segment (F is not known yet), C is center of given arc, B is point of rough conjugation. c is line, parallel to BF, |GF|=|GH| = r - radius of small arc.

    To make smooth conjugation, tangent to small arc in point F should be collinear with BF direction, so GF is perpendicular to BF, and tangents to both arcs in point H should coincide - so radius-vectors CH and GH lie on the same line. enter image description here

    Let unit direction vector of BF segment is ud=(dx,dy), so unit normal is un=(-dy, dx). (Negate normal for arcs at another side of BF)

    Center of small arc G has coordinates (where t is unknown parameter - length of BF)

    G = B + ud * t + un * r
    

    and distance GC is difference of arcs radii, so

     |G - C| = |R - r| 
       or in coordinates:
    (B.x + dx * t - dy * r - C.x)^2 + (B.y + dy * t + dx * r - C.y)^2 = (R - r)^2
    

    Open parentheses, solve quadratic equation for unknown t. If solutions exist, choose right root, and you'll get coordinates of center of conjugation arc G and its ends

    quick check1: Line Y=5, big arc with R=5, we want small arc with r=2

     B=(5,5)
     ud=(-1,0)
     un=(0,-1)
     (5-t)^2 + (5-2-5)^2 = (5-2)^2
     solution gives 
     t = 5 +/- Sqrt(5), the second root is valid
     E = (5 - (5 - Sqrt(5)), 3) = (2.23, 3)
    

    Resulting smooth arc is c-f

    enter image description here

    quick check2: Line Y=5, big arc with R=5, we want small arc with r=2

     B=(5,5)
     big arc center (H here) = (1,2)  
     ud=(-1,0)
     un=(0,-1)
     (4-t)^2 + (5-2-2)^2 = (5-2)^2
     solution gives 
     t = 4 +/- Sqrt(8), the second root is valid
     E = (5 - (4 - Sqrt(8)), 3) = (3.83, 3)
    

    Resulting smooth arc is F-G

    (In both cases larger root corresponds to conjugation with complementary part of big arc)

    enter image description here