Find a tangent point on circle?

后端 未结 9 747
别跟我提以往
别跟我提以往 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) };
    

提交回复
热议问题