zoukankan      html  css  js  c++  java
  • 在笛卡尔坐标系上描绘双曲类曲线

    点评:下图展示了各种双曲类曲线,形状类似,因为无论x的幂级数是多少,都可以把它设为一个新变量,这样又变成了标准双曲线的形式,比如说y=1/(x2),设x2为w,函数又变成y=1/w。这类曲线像是被支点(1,1)顶住的钢丝,两端在不断逼近坐标轴,x的幂级数大的逼近x轴更快,相对逼近y轴就慢些;相反x的幂级数小的逼近x轴慢,相对逼近y轴就快些。

    代码:

    <!DOCTYPE html>
    <html lang="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <head>
         <title>绘制双曲类线</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=1/x 红色",x,y);    
            ctx.fillText("y=1/(x*x) 绿色",x,y+20);    
            ctx.fillText("y=1/√x 紫色",x,y+40);    
            ctx.fillText("y=1/(x*x*x) 栗色",x,y+60);    
    
            ctx.fillText("  作者:逆火狂飙",x+170,y+30);
        }
    
        function drawCurve(ctx){
            var cds=[{}];
            var cds1=[{}];
            var cds2=[{}];
            var cds3=[{}];
            var cds4=[{}];
            var cds5=[{}];
            var cds6=[{}];
    
            var x,y;
            for(x=-13;x<=13;x+=0.01){    
                if(x>0){// 0.01 防止除零溢出
                    y=1/x;// 标准双曲线
                    var arr={"x":x,"y":y};
                    cds.push(arr);    
                }
    
                if(x<-0.01){
                    y=1/x;// 标准双曲线
                    var arr={"x":x,"y":y};
                    cds1.push(arr);    
                }
    
                if(x>0){
                    y=1/(x*x);// 
                    var arr={"x":x,"y":y};
                    cds2.push(arr);    
                }
    
                if(x<-0.01){
                    y=1/(x*x);// 
                    var arr={"x":x,"y":y};
                    cds3.push(arr);    
                }
    
                if(x>0){
                    y=1/Math.sqrt(x);// 
                    var arr={"x":x,"y":y};
                    cds4.push(arr);    
                }
    
                if(x>0){
                    y=1/(x*x*x);// 
                    var arr={"x":x,"y":y};
                    cds5.push(arr);    
                }
    
                if(x<-0.01){
                    y=1/(x*x*x);// 
                    var arr={"x":x,"y":y};
                    cds6.push(arr);    
                }
            }
    
            paintCurve(ctx,"red",cds);
            paintCurve(ctx,"red",cds1);
            paintCurve(ctx,"green",cds2);
            paintCurve(ctx,"green",cds3);
            paintCurve(ctx,"purple",cds4);
            paintCurve(ctx,"maroon",cds5);
            paintCurve(ctx,"maroon",cds6);
        }
    
        function paintCurve(ctx,color,cds){
            var SU=50;// Scale Unit
    
            ctx.strokeStyle = color;
            ctx.beginPath();        
            for(var i=0; i<cds.length; i++){  
                ctx.lineTo(cds[i].x*SU,cds[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>
  • 相关阅读:
    Spring Boot从入门到精通(一)搭建第一个Spring Boot程序
    程序员未来的出路究竟在哪里?一位老码农的心声
    ​IntelliJ IDEA使用技巧—使用EasyCode插件一键生成代码04期
    浅谈Java后端开发工程师腾讯面试经历分享总结
    Java面试技巧—如何自我介绍
    互联网大厂Java面试题集—Spring boot常见面试题(二)
    互联网大厂Java面试题集—Spring boot面试题(一)
    ActiveMQ消息队列从入门到实践(4)—使用Spring JMS收发消息
    ActiveMQ消息队列从入门到实践(1)—JMS的概念和JMS消息模型
    有多少程序员干到35岁,那么其他人去干什么了?
  • 原文地址:https://www.cnblogs.com/heyang78/p/8142896.html
Copyright © 2011-2022 走看看