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

简介:

代码如下:

复制代码
<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
     <title>函数2*x+Math.sqrt(5-x*x)及其共轭函数2*x-Math.sqrt(5-x*x)曲线勾画</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("2*x+Math.sqrt(5-x*x) 红色曲线",x,y);
        
        ctx.fillText("2*x-Math.sqrt(5-x*x) 绿色曲线",x,y+20);

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

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

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

        var x,y;
        for(x=-13;x<=13;x+=0.01){
            if(5-x*x>0){            
                y=2*x+Math.sqrt(5-x*x);// 函数式在此
                var arr={"x":x,"y":y};
                cds.push(arr);    

                y=2*x-Math.sqrt(5-x*x);// 与上面函数互为共轭函数
                var arr={"x":x,"y":y};
                cds1.push(arr);
            }
        }

        // 2*x+Math.sqrt(5-x*x)
        ctx.strokeStyle = "red";
        ctx.beginPath();
        
        for(var i=0; i<cds.length; i++){  
            ctx.lineTo(cds[i].x*SU,cds[i].y*SU);
        } 
        
        ctx.stroke();
        ctx.closePath();

        // 2*x-Math.sqrt(5-x*x);
        ctx.strokeStyle = "green";
        ctx.beginPath();
        
        for(var i=0; i<cds1.length; i++){  
            ctx.lineTo(cds1[i].x*SU,cds1[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/8135244.html,如需转载请自行联系原作者

相关文章
|
10月前
|
NumPy 差分、最小公倍数、最大公约数、三角函数详解
NumPy 差分 离散差分意味着相邻元素之间的减法。 例如,对于 [1, 2, 3, 4],离散差分将是 [2-1, 3-2, 4-3] = [1, 1, 1] 要找到离散差分,使用 diff() 函数。
对角矩阵(Diagonal Matrix)
对角矩阵(Diagonal Matrix)是一种特殊的矩阵,其元素仅位于主对角线上。对角矩阵通常用于线性代数和微积分等数学领域,它有以下几个特点:
958 7
输出三角形面积和周长
输出三角形面积和周长 (15 分)
146 0
(二维vector)(绝对值求和等式的处理)B. Playing in a Casino
(二维vector)(绝对值求和等式的处理)B. Playing in a Casino
110 0
Unity Mathf【Deg & Rad】- 关于数学运算中的度与弧度
Unity Mathf【Deg & Rad】- 关于数学运算中的度与弧度
357 1
三角函数中的正弦、余弦、正切、余切、正割、余割函数性质及常用公式
三角函数中的正弦、余弦、正切、余切、正割、余割函数性质及常用公式
895 0
三角函数中的正弦、余弦、正切、余切、正割、余割函数性质及常用公式
三角函数
三角函数
228 0
三角函数
【欧拉计划第 6 题】和的平方与平方的和差值 Sum square difference
【欧拉计划第 6 题】和的平方与平方的和差值 Sum square difference
181 0
常见距离公式 numpy 实现
在使用 keras 或者使用 tf 做深度学习时,通常有些内容需要计算距离来作为判定相似程度的依据,如下列举一些常见的距离公式: def minkowski_distance(vec1, vec2, p=3): """ 闵氏距离 ...
1250 0