Example: http://www.zhaojz.com.cn/demo/draw9.html
The code copy is as follows:
//Draw tangent
//point
//dot center
//r Radius
function drawCircleTangent(point, dot, r){
//Draw auxiliary lines-start
var color = 'DarkRed'; //The color of the tangent
var color2 = "#ccc"; //The colors of other auxiliary lines
drawLine(dot, [dot[0]+9*r,dot[1]], {color: color2}); // Extend the horizontal line where the center of the circle is
drawLine(dot, [dot[0],dot[1]-4*r], {color: color2}); // Draw the vertical line where the center of the circle is
drawPoint({
pw:2,ph:2,color:'DarkRed',point: [dot[0]+9*r,dot[1],'x']
});
drawPoint({
pw:2,ph:2,color:'DarkRed',point: [dot[0],dot[1]-4*r,'y']
});
drawLine(point, [point[0],dot[1]], {color: color2}); // Draw the vertical line from point to the x-axis
drawLine(point, dot, {color: color2}); // Connect point and dot
drawLine([point[0]-2*r, point[1]], [point[0]+2*r, point[1]], {color: color2}); //Draw the horizontal line where the point is
//Draw auxiliary lines-end
//point.push('point');
drawPoint({
pw:2,ph:2,color:'DarkRed',point: point
});
//dot.push('centre');
var r_square = Math.pow(r,2); // square of r
var point_v = point[1]-dot[1]; //The square of the distance from point to the x-axis
var point_h = point[0]-dot[0]; //The square of the distance from point to the y-axis
var c_square = Math.pow(point_v,2) + Math.pow(point_h,2); //The square of the distance from point to the center of the circle
var c = Math.sqrt(c_square); //Distance from point to center
var sinA = Math.abs(point_v)/c; //sinA
var cosA = Math.abs(point_h)/c; //cosA
var b_square = c_square-r_square; //The square of the distance from point to tangent
var b = Math.sqrt(b_square); //The distance from point to tangent
var sinB = b/c; //sinB
var cosB = r/c; //cosB
//Determine the quadrant where the point is located with the center of the circle as the coordinate dot
var quadrant = 1; //Default value
var pm_h = point_h == 0? point_h: point_h/Math.abs(point_h); //Horizontal direction
var pm_v = point_v == 0? point_v: point_v/Math.abs(point_v); //Vertical direction
var hv = pm_h*pm_v; //Multiply, when -1, point is in the first and third quadrants, +1, point is in the second and fourth quadrants, and when 0, point is on the axis
switch(hv){
case 1:
if((pm_h+pm_v)==-2){
quadrant = 2; //The second quadrant
}else{
quadrant = 4; //The fourth quadrant
}
break;
case -1:
if((pm_h-pm_v)==-2){
quadrant = 3; //The third quadrant
}
break;
case 0:
if((pm_h+pm_v)==-1){ // When point is on the negative half axis of the x-axis or the positive half axis of the y-axis, it is determined that point is in the second quadrant
quadrant = 2;
}
if((pm_h+pm_v)==1){ // When point is on the positive half axis of the x-axis or the negative half axis of the y-axis, it is determined that point is in the fourth quadrant
quadrant = 4;
}
break;
default:
}
var sinC = 0;
var conC = 0;
var sinD = 0;
var conD = 0;
switch(quadrant){
case 1:
sinC = cosB*cosA+sinB*sinA; //sinC = sin(90+(BA)) = cos(BA) = cosB*cosA+sinB*sinA
conC = -(sinB*cosA-cosB*sinA); //cosC = cos(90+(BA)) = -sin(BA) = -(sinB*cosA-cosB*sinA)
sinD = -(cosA*cosB-sinA*sinB); //sinD = sin(270-(A+B))
conD = -(sinA*cosB+cosA*sinB); //conD = cos(270-(A+B))
break;
case 2:
sinC = -(cosB*cosA-sinB*sinA); //sinC = sin(-90+(A+B))
conC = sinA*cosB+cosA*sinB; //conC = cos(-90+(A+B))
sinD = cosA*cosB+sinA*sinB; //sinD = sin(90+(AB))
conD = -(sinA*cosB-cosA*sinB); //conD = cos(90+(AB))
break;
case 3:
sinC = -(cosA*cosB+sinA*sinB); //sinC = sin(-90+(AB))
conC = -(sinA*cosB-cosA*sinB); //conC = cos(-90+(AB))
sinD = (cosA*cosB-sinA*sinB);
conD = sinA*cosB+cosA*sinB;
break;
case 4:
sinC = cosA*cosB-sinA*sinB;
conC = -(sinA*cosB+cosA*sinB)
sinD = -(cosA*cosB+sinA*sinB); //sinD = sin(270+(AB))
conD = (sinA*cosB-cosA*sinB); //conD = cos(270+(AB))
break;
default:
}
var tangentPointA = [point[0]+b*conC, point[1]+b*sinC]; // Position of tangent point A
drawLine(point, tangentPointA, {color: color}); //Draw the tangent line
drawLine(dot, tangentPointA, {color: color2}); //Connect dots and tangent points
//drawArc(point, 17, (quadrant ==1 ||quadrant==4?180:0)-(quadrant ==2 ||quadrant==3?(-1):1)*Math.asin(sinC)*180/Math.PI, 0);
var tangentPointB = [point[0]+b*conD, point[1]+b*sinD]; // Position of the tangent point B
drawLine(point, tangentPointB, {color: color}); //Draw the tangent line
drawLine(dot, tangentPointB, {color: color2}); //Connect dots and tangent points
//drawArc(point, 27, (quadrant ==1 ||quadrant==4?180:0)-(quadrant ==2 ||quadrant==3?(-1):1)*Math.asin(sinD)*180/Math.PI, 0);
drawPoint({ //Trag the point
pw:3,ph:3,color:'DarkRed',point: tangentPointA
});
drawPoint({ //Trag the point
pw:3,ph:3,color:'DarkRed',point: tangentPointB
});
//Draw auxiliary arc
//(quadrant ==1 ||quadrant==4?360:0)
drawArc(point, b, 60, (quadrant ==1 ||quadrant==4?180:0)-(quadrant ==2 ||quadrant==3?(-1):1)*Math.asin(sinC)*180/Math.PI-5);
}