代码:

<!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>

在笛卡尔坐标系上描绘函数(x*x+1)/(x*x-1)曲线的更多相关文章

  1. 在笛卡尔坐标系上描绘函数2*x+Math.sqrt(5-x*x)及其共轭函数2*x-Math.sqrt(5-x*x)曲线

    代码如下: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Ty ...

  2. 在笛卡尔坐标系上描绘函数 y=4x^2-2/4x-3

    代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type ...

  3. 在笛卡尔坐标系上描绘y=x^2-4/x^2-2x-3曲线

    <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type&quo ...

  4. AcWing:112. 雷达设备(贪心 + 笛卡尔坐标系化区间)

    假设海岸是一条无限长的直线,陆地位于海岸的一侧,海洋位于另外一侧. 每个小岛都位于海洋一侧的某个点上. 雷达装置均位于海岸线上,且雷达的监测范围为d,当小岛与某雷达的距离不超过d时,该小岛可以被雷达覆 ...

  5. 如果是在有master上开启了该参数,记得在slave端也要开启这个参数(salve需要stop后再重新start),否则在master上创建函数会导致replaction中断。

    如果是在有master上开启了该参数,记得在slave端也要开启这个参数(salve需要stop后再重新start),否则在master上创建函数会导致replaction中断.

  6. 自学Linux Shell16.4-在命令行上使用函数

    点击返回 自学Linux命令行与Shell脚本之路 16.4-在命令行上使用函数 脚本函数不仅可以用作shell脚本命令,也可以用作命令行界面的命令.一旦在shell中定义了函数,可以从系统的任意目录 ...

  7. HTML5 Canvas 笛卡尔坐标系转换尝试

    <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type&quo ...

  8. Javascript作业—数组去重(要求:原型链上添加函数)

    数组去重(要求:原型链上添加函数) <script> //数组去重,要求:在原型链上添加函数 //存储不重复的--仅循环一次 if(!Array.prototype.unique1){ A ...

  9. js进阶 12-8 如何知道上一个函数的返回值是什么(如何判断上一个函数是否执行成功)

    js进阶 12-8 如何知道上一个函数的返回值是什么(如何判断上一个函数是否执行成功) 一.总结 一句话总结:event的result属性即可. 1.event的result属性的实际应用场景是什么? ...

随机推荐

  1. Mongodb 3.4 Replication

    官方文档:https://docs.mongodb.com/manual/replication/ 关于ReplSet的文档 https://docs.mongodb.com/manual/refer ...

  2. 插入排序(InsertionSort)

    算法描述 插入排序是在一个已经有序的数据序列,要求在这个已经排好的数据序列中插入一个数,但要求插入后此数据序列仍然有序.插入排序是一种稳定的排序. 基本思想 插入排序是在一个已经有序的小序列的基础上, ...

  3. HDU 4607.Park Visit-树的直径(BFS版)+结论公式(乱推公式)-备忘(加油!)

    Park Visit Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. python的ipython manage.py shell 引发的 No module named _argparse

    环境是:Centos6.6 ,python 2.6 今晚,shell 中输入: # ipython manage.py shell 报错,说找不到命令: 我当时,觉得,我有可能没有安装ipython ...

  5. Thinkphp命名规范

    1.类文件都是以.class.php为后缀(这里是指的ThinkPHP内部使用的类库文件,不代表外部加载的类库文件),使用驼峰法命名,并且首字母大写,例如 DbMysql.class.php: 2.类 ...

  6. 对StackOverflow上投票最高的带javascript标签的问题的汇总

    https://github.com/simongong/js-stackoverflow-highest-votes

  7. Java Eclipse插件

    EasyExplore 快速打开文件所在目录1 http://sourceforge.net/projects/easystruts/ OpenExplorer 快速打开文件所在目录2 https:/ ...

  8. 洛谷——P1724 东风谷早苗

    P1724 东风谷早苗 题目描述 在幻想乡,东风谷早苗是以高达控闻名的高中生宅巫女.某一天,早苗终于入手了最新款的钢达姆模型.作为最新的钢达姆,当然有了与以往不同的功能了,那就是它能够自动行走,厉害吧 ...

  9. run()和start()的区别

    run没有启新的线程,start方法才会调用Thread的native的start0方法,start0会调用run方法,开启新的线程,博主这么做,不是多线程顺序执行,而是把业务阻塞在主线程里.请打印: ...

  10. 【dijkstra】【次短路】【fread】hdu6181 Two Paths

    题意:给你一张简单无向图,问你1到n的次短路.注意,可以不是简单路径. 存个次短路板子,原理还是挺简单,直接看代码吧.然后这份代码还是个fread的示例用法. #include<cstdio&g ...