JS画几何图形之五【过圆外一点作切线】

样例:http://www.zhaojz.com.cn/demo/draw9.html

//画切线
//point 圆外的一点
//dot 圆心
//r 半径
function drawCircleTangent(point, dot, r){
    //画辅助线-start
    var color = ‘DarkRed‘; //切线的颜色
    var color2 = "#ccc"; //其它辅助线的颜色
    drawLine(dot, [dot[0]+9*r,dot[1]], {color: color2}); // 延长圆心所在的水平线
    drawLine(dot, [dot[0],dot[1]-4*r], {color: color2}); // 画出圆心所在的垂直线
    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}); // 画point到x轴的垂直线
    drawLine(point, dot, {color: color2}); // 连接point与dot
    drawLine([point[0]-2*r, point[1]], [point[0]+2*r, point[1]], {color: color2}); //画point所在的水平线
    //画辅助线-end
    //point.push(‘point‘);
    drawPoint({
        pw:2,ph:2,color:‘DarkRed‘,point: point
    });
    //dot.push(‘centre‘);

    var r_square = Math.pow(r,2); // r的平方
    var point_v = point[1]-dot[1]; //point到x轴的距离的平方
    var point_h = point[0]-dot[0]; //point到y轴的距离的平方
    var c_square = Math.pow(point_v,2) + Math.pow(point_h,2); //point到圆心的距离的平方

    var c = Math.sqrt(c_square); //point到圆心的距离

    var sinA = Math.abs(point_v)/c; //sinA
    var cosA = Math.abs(point_h)/c; //cosA
    var b_square = c_square-r_square; //point到切点的距离的平方
    var b = Math.sqrt(b_square); //point到切点的距离

    var sinB = b/c; //sinB
    var cosB = r/c; //cosB
    //以圆心为坐标圆点,确定point所在的象限
    var quadrant  = 1; //默认值
    var pm_h = point_h == 0? point_h: point_h/Math.abs(point_h); //水平方向
    var pm_v = point_v == 0? point_v: point_v/Math.abs(point_v); //垂直方向
    var hv = pm_h*pm_v; //相乘,-1时point在一三象限,+1时point在二四象限,0时point在轴上
    switch(hv){
        case 1:
            if((pm_h+pm_v)==-2){
                quadrant = 2; //第二象限
            }else{
                quadrant = 4; //第四象限
            }
            break;
        case -1:
            if((pm_h-pm_v)==-2){
                quadrant = 3; //第三象限
            }
            break;
        case 0:
            if((pm_h+pm_v)==-1){ //point在x轴的负半轴或者y轴的正半轴时,断定point在第二象限
                quadrant = 2;
            }
            if((pm_h+pm_v)==1){ //point在x轴的正半轴或者y轴的负半轴时,断定point在第四象限
                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+(B-A)) = cos(B-A) = cosB*cosA+sinB*sinA
            conC = -(sinB*cosA-cosB*sinA); //cosC = cos(90+(B-A)) = -sin(B-A) = -(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+(A-B))
            conD = -(sinA*cosB-cosA*sinB); //conD = cos(90+(A-B))
            break;
        case 3:
            sinC = -(cosA*cosB+sinA*sinB); //sinC = sin(-90+(A-B))
            conC = -(sinA*cosB-cosA*sinB); //conC = cos(-90+(A-B))
            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+(A-B))
            conD = (sinA*cosB-cosA*sinB); //conD = cos(270+(A-B))
            break;
        default:
    }

    var tangentPointA = [point[0]+b*conC, point[1]+b*sinC]; //切点A位置
    drawLine(point, tangentPointA, {color: color}); //画出切线
    drawLine(dot, tangentPointA, {color: color2}); //连接圆点与切点
    //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]; //切点B位置
    drawLine(point, tangentPointB, {color: color}); //画出切线
    drawLine(dot, tangentPointB, {color: color2}); //连接圆点与切点
    //drawArc(point, 27, (quadrant ==1 ||quadrant==4?180:0)-(quadrant ==2 ||quadrant==3?(-1):1)*Math.asin(sinD)*180/Math.PI, 0);

    drawPoint({ //描切点
        pw:3,ph:3,color:‘DarkRed‘,point: tangentPointA
    });
    drawPoint({ //描切点
        pw:3,ph:3,color:‘DarkRed‘,point: tangentPointB
    });
    //画辅助弧
    //(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);
}
时间: 2024-10-01 04:36:31

JS画几何图形之五【过圆外一点作切线】的相关文章

JS画几何图形之六【过直线外一点作垂线】

样例:http://www.zhaojz.com.cn/demo/draw10.html //过直线外一点画垂线 function drawVerticalLine(point, line){ //画辅助线-start var color = 'DarkRed'; //垂线的颜色 var color2 = "#ccc"; //其它辅助线的颜色 drawPoint({ pw:2,ph:2,color:'DarkRed',point: line[0] }); drawPoint({ pw:

JS画几何图形之二【圆】

半径为r的圆上的点p(x,y)与圆心O(x0,y0)的关系: x = x0+rcosA;  y = y0+rsinA ,A为弧度 样例:http://www.zhaojz.com.cn/demo/draw6.html 一.圆 //圆形/椭圆 //dot 圆点 //r 半径 //compressionRatio 垂直压缩比 function drawCircle(dot, r, compressionRatio, data){ var pstart = [dot[0]+r, dot[1]]; //

JS画几何图形之四【饼图】

饼图是将一个圆分割为多个扇形. 样例:http://www.zhaojz.com.cn/demo/draw8.html //饼图 //dot 圆点 //r 半径 //data 数据(一维数组) function drawPie(dot, r, data){ if(data && data.length > 0){ var accumulationAngleOfSlope = new Number(0); //累计偏移角度 var total = new Number(0); var

JS画几何图形之三【正弦曲线】

数学式:y=Asin(ωx+φ)+k 样例:http://www.zhaojz.com.cn/demo/draw7.html JS函数的声明: //画正弦曲线 //dot 原点 //amplitude 振幅 -- A //initialPhase 初相 -- φ //setover 偏距 -- k //palstance 角速度 -- ω //len 周期数 function drawSinusoid(dot, amplitude,initialPhase,palstance,setover,

JS画几何图形之一【直线】

JS画图的想法经过大脑的时候,觉得有点意思,所以就实践了一番.JS画图为系列文章,本是讲点.线和面 先看样例:http://www.zhaojz.com.cn/demo/draw5.html 一.点 这里的点我们使用span标签表示 //描点,参数有点的大小,颜色,点的坐标和标签; 很明显opts参数是一个对象 function drawPoint(opts){ document.write("<span id='"+opts.point[0]+""+opts

poj 1375 Intervals(解析几何 过圆外一点求与圆的切线)

题目大意:给出一个光源,给出一些圆,求投影区间. 如图,先可以求出角a,通过半径与PQ距离,而角b也可以求出.那么就可以求出两条切线与Y轴的夹角,分别为a+b,b-a. 之后利用角度求出各投影线段的左右顶点,排序判断即可. #include<iostream> #include<algorithm> #include<cmath> #include<cstdio> using namespace std; struct Point { double x,y;

MT【45】抛物线外一点作抛物线的切线(尺规作图题)

注1:S为抛物线焦点 注2:由切线的唯一性,以及切线时可以利用MT[42]评得到三角形全等从而得到切线平分$\angle MQS$得到

用JS画斐波那契螺旋线(黄金螺旋线)

偶然看到斐波那契螺旋线(黄金螺旋线)的定义及画图方法,试着用JS画了一下,很漂亮,很好玩 具体定义及画法大家查一下就有了,很简单. 以下是代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>FibonacciSequence</title> </head> <body> <

纯CSS画几何图形

<!doctype html> <html> <head> <meta charset="utf-8"> <title>纯CSS画几何图形</title> </head> <body> <!--多彩空心矩形--> <div style="width:100px; height:100px; border-width:20px; border-style:solid