Find a tangent point on circle?

后端 未结 9 715
别跟我提以往
别跟我提以往 2020-12-09 04:33

Given a line with first end point P(x1,y1) another end point is unknown, intersect with a circle that located at origin with radius R at only one point(tangent) T(x2,y2). An

相关标签:
9条回答
  • 2020-12-09 05:29

    All you need is in dmckee's answer, but if you care for some code check this implementation using Javascript and HTML canvas.

    Full example: http://jsfiddle.net/zxqCw/1/

    // find tangents
    dx = cx - px;
    dy = cy - py;
    dd = Math.sqrt(dx * dx + dy * dy);
    a = Math.asin(radius / dd);
    b = Math.atan2(dy, dx);
    
    t = b - a
    ta = { x:radius * Math.sin(t), y:radius * -Math.cos(t) };
    
    t = b + a
    tb = { x:radius * -Math.sin(t), y:radius * Math.cos(t) };
    
    0 讨论(0)
  • 2020-12-09 05:29

    Use the x,y coordinates of the intersecting equations (the one of the circle and the one of the line). That's the point.

    If you have only one end point from which to draw the line you'll get two different points, as there will be two different tangent lines, one up and one down.

    0 讨论(0)
  • 2020-12-09 05:35
    1. You can find direction of vector DX if you rotate vector DO by angle alpha (angle alpha is found as asin(len(OX) / len(DO)), which is simply arcsinus of radius over hypotenuse)

    2. You can find the length of vector DX trivially as following: sqrt(len(DO)*len(DO) - len(OX)*len(OX))

    3. Given the direction and length of vector DX, you can find the value of point X. One approach would be to normalize DX and multiply it by the length of it.

    auto dist = D.Distance(O);
    auto side = sqrt(dist*dist - rad*rad)
    auto line = Vector2D(D, O);
    line.Rotate(asin(rad / dist)); //get the direction
    line.Normalize();              //set length to 1
    line*=side;                    //we have the direction, now get length
    Point2D X = D + line;
    

    P.S. Note that there is also a second tangent, which is found by rotating DO by minus alpha

    Image demonstrating the algo

    0 讨论(0)
提交回复
热议问题