在笛卡尔坐标系上描绘函数(x*x+1)/(x*x-1)曲线

简介:

代码:

复制代码
<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
     <title>描绘函数(x*x+1)/(x*x-1)曲线</title>
    </head>

     <body onload="draw()">
        <canvas id="myCanvus" width="1300px" height="640px" style="border:1px dashed black;">
            出现文字表示你的浏览器不支持HTML5
        </canvas>
     </body>
</html>
<script type="text/javascript">
<!--
    function draw(){
        var canvas=document.getElementById("myCanvus");
        var canvasWidth=1300;
        var canvasHeight=640;

        var context=canvas.getContext("2d");
        
        context.fillStyle = "white";
        context.fillRect(0, 0, canvasWidth, canvasHeight);

        context.strokeStyle = "black";
        context.fillStyle = "black";

        
        // 进行坐标变换:把原点放在左下角,东方为X轴正向,北方为Y轴正向
        var offsetY=320;// Y向偏移值,正值向上偏,用来画坐标轴
        var offsetX=650;// X向偏移值,正值向右偏,用来画坐标轴

        context.save();
        context.translate(0+offsetX,canvasHeight-offsetY);

        drawAxisXText(context);// 文字和线分开画比较好处理
        drawAxisYText(context);
        drawTitleText(context);

        context.rotate(getRad(180));
        context.scale(-1,1);        

        drawAxisX(context);        
        drawAxisY(context);       
        drawCurve(context);       

        context.restore();        
    }

    function drawTitleText(ctx){        
        ctx.lineWidth=0.5;
        ctx.strokeStyle='navy';
        ctx.fillStyle='navy';

        var x=350;
        var y=-250;

        // 写文字
        ctx.fillText("y=(x*x+1)/(x*x-1)",x,y);        

        ctx.fillText("  作者:逆火狂飙",x+170,y+30);
    }

    function drawCurve(ctx){
        var SU=50;// Scale Unit

        var cds=[{}];
        var cds1=[{}];
        var cds2=[{}];

        var x,y;
        for(x=-13;x<=13;x+=0.01){    
            if(x<-1-0.01){// 0.01 防止除零溢出
                y=(x*x+1)/(x*x-1);// 函数式在此
                var arr={"x":x,"y":y};
                cds.push(arr);    
            }

            if(x>1){
                y=(x*x+1)/(x*x-1);// 函数式在此
                var arr={"x":x,"y":y};
                cds1.push(arr);    
            }

            if(x>-1 && x<1){
                y=(x*x+1)/(x*x-1);// 函数式在此
                var arr={"x":x,"y":y};
                cds2.push(arr);    
            }
        }

        ctx.strokeStyle = "red";

        ctx.beginPath();        
        console.log("cds.length="+cds.length);
        for(var i=0; i<cds.length; i++){  
            console.log("12="+cds[i]);
            ctx.lineTo(cds[i].x*SU,cds[i].y*SU);
        } 
        
        ctx.stroke();
        ctx.closePath();

        ctx.beginPath();        
        for(var i=0; i<cds1.length; i++){  
            ctx.lineTo(cds1[i].x*SU,cds1[i].y*SU);
        } 
        
        ctx.stroke();
        ctx.closePath();

        ctx.beginPath();        
        for(var i=0; i<cds2.length; i++){  
            ctx.lineTo(cds2[i].x*SU,cds2[i].y*SU);
        } 
        
        ctx.stroke();
        ctx.closePath();
    }

    function drawAxisX(ctx){
        ctx.save();
        
        ctx.lineWidth=0.5;
        ctx.strokeStyle='navy';
        ctx.fillStyle='navy';

        var start=-650;
        var end=650;

        // 画轴
        ctx.beginPath();
        ctx.moveTo(start, 0);
        ctx.lineTo(end, 0);
        ctx.stroke();
        ctx.closePath();

        // 画箭头
        ctx.beginPath();
        ctx.moveTo(end-Math.cos(getRad(15))*10, Math.sin(getRad(15))*10);
        ctx.lineTo(end, 0);
        ctx.lineTo(end-Math.cos(getRad(15))*10, -Math.sin(getRad(15))*10);
        ctx.stroke();
        ctx.closePath();
        
        // 画刻度
        var x,y;
        y=5;
        for(x=start;x<end;x+=50){
            ctx.beginPath();
            ctx.moveTo(x, 0);
            ctx.lineTo(x, y);
            
            ctx.stroke();
            ctx.closePath();
        }

        ctx.restore();
    }

    function drawAxisXText(ctx){        
        ctx.lineWidth=0.5;
        ctx.strokeStyle='navy';
        ctx.fillStyle='navy';

        var start=-650;
        var end=650;

        // 写文字
        var x,y=5;
        for(x=start;x<end;x+=50){
            ctx.fillText(x/50,x,y+10);
        }
    }

    function drawAxisY(ctx){
        ctx.save();
        
        ctx.lineWidth=0.5;
        ctx.strokeStyle='navy';
        ctx.fillStyle='navy';

        var start=-300;
        var end=300;

        // 画轴
        ctx.beginPath();
        ctx.moveTo(0, start);
        ctx.lineTo(0, end);
        ctx.stroke();
        ctx.closePath();

        // 画箭头
        ctx.beginPath();
        ctx.moveTo(Math.sin(getRad(15))*10, end-Math.cos(getRad(15))*10);
        ctx.lineTo(0, end);
        ctx.lineTo(-Math.sin(getRad(15))*10, end-Math.cos(getRad(15))*10);
        ctx.stroke();
        ctx.closePath();
        
        // 画刻度
        var x,y;
        x=5;
        for(y=start;y<end;y+=50){
            ctx.beginPath();
            ctx.moveTo(x, y);
            ctx.lineTo(0, y);
            
            ctx.stroke();
            ctx.closePath();
        }
    }

    function drawAxisYText(ctx){        
        ctx.lineWidth=0.5;
        ctx.strokeStyle='navy';
        ctx.fillStyle='navy';

        var start=-250;
        var end=350;

        // 写文字
        var x=-19,y=5;
        for(y=start;y<end;y+=50){

            if(y!=0){
                ctx.fillText(-y/50,x,y);
            }
        }
    }

    function getRad(degree){
        return degree/180*Math.PI;
    }

    function cutShort(str,length){
        if(str.length>length){
            str=str.substr(0,length)+"...";
        }
        
        return str;
    }
//-->
</script>
复制代码

 









本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/xiandedanteng/p/8135765.html,如需转载请自行联系原作者

相关文章
|
4月前
|
算法
[Halcon&几何] 矩形顶点和对角连线角度计算
[Halcon&几何] 矩形顶点和对角连线角度计算
38 0
|
4月前
[Halcon&几何] 直线的垂线与延长线的计算
[Halcon&几何] 直线的垂线与延长线的计算
88 1
|
10月前
|
数据可视化 C++
高斯正反算—投影坐标转大地坐标、大地坐标转投影坐标(附有完整代码及测试结果)
高斯正反算—投影坐标转大地坐标、大地坐标转投影坐标(附有完整代码及测试结果)
|
11月前
|
数据可视化 数据处理
分面中添加不同的直线
分面中添加不同的直线
107 0
144.绘制布朗运动曲线
144.绘制布朗运动曲线
82 0
075.绘制余弦曲线和直线的迭加
075.绘制余弦曲线和直线的迭加
49 0