google-mapsgeometrytrigonometrycomputational-geometrygeofencing

How to calculate X and Y coordinates given an angle and varying side lengths


I am building a Google Maps Javascript API application that allows the user to draw a path of multiple polylines, calculates the angles, then attempts to curve the angles, simulating a vehicle turning radius. In order to do this, I need the point the curve starts and stops at. The problem is that these angles can be in any assortment of directions on a map, and I am struggling to find the appropriate way to calculate the (X,Y) coordinates of these points... I can calculate the magnitude, but due to the varying orientation, am not sure how to get it back to a set of cartesian coordinates. Here is an outline of the geometry:

geomtetry

Ultimately, I have points A, B, and C, angle a, and the radius of the circle. Given that, how can I calculate points P1, P2, and (not labeled) the green reference points for the bezier curve in terms of cartesian coordinates? These points do not have to be specific, just in the general area to guide the curve.


Solution

  • Make vectors BA and

    BA.x = A.x-B.x
    BA.y = A.y-B.y etc
    

    Make unit vectors

    len(BA) = sqrt(BA.x*BA.x+BA.y*BA.y)
    uBA = BA / len(BA)       (uBA.x = BA.x/len(BA) etc)
    uBC = BC / len(BC)
    

    Get cosine of angle between vectors

    cs = cos(fullangle) = dot(uBA, uBC) = uBA.x*uBC.x + uBA.y*ubC.y
    

    Get tangent of half-angle

    tn = sqrt((1-cs)/(1+cs))
    

    Calculate points to provide normal distance R from bisector to lines

    P1 = B + uBA * R / tn   (P1.x = B.x + uBA.x * R / tn   etc)
    P2 = B + uBC * R / tn
    

    Note that smooth conjugation of Bezier curve and lines requires control points on lines, so

    C1 = P1 - uBA * R * coeff
    C2 = P2 - uBC * R * coeff
    

    Where coefficient should be adapted for "smart curve" look, perhaps in range 0.3..1.

    coeff=0.552 
    

    for right-angled lines gives almost perfect circle arc.

    In general

    coeff = 4/3*tg(angle/4) 
    

    is nice approximantion