代码:

<!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="240px" 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=240;

        var context=canvas.getContext("2d");

        context.fillStyle = "white";
        context.fillRect(0, 0, canvasWidth, canvasHeight);

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

        //context.save();

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

        context.save();
        context.translate(0+offsetX,canvasHeight-offsetY);
        context.rotate(getRad(180));
        context.scale(-1,1);        

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

        context.restore();

        context.fillText("x^2-2x+1",40,200);
        context.fillText("-----------",40,210);
        context.fillText("x^2+x+2",40,220);
        context.fillText("y=",25,210);
        context.fillText("曲线,^2是平方的意思。作者 逆火狂飙",95,210);
    }

    function drawCurve(ctx){
        var cds=[{}];// 初始化cds为空json

        var x,y;
        for(x=-12;x<=12;x+=0.1){
            y=(x*x-2*x+1)/(x*x+x+2);
            var arr={"x":x,"y":y};
            cds.push(arr);
        }

        // 将数组里面的点一段段连线
        var ymax=-4,ymin=4,xmax,xmin;

        ctx.strokeStyle = "red";
        ctx.beginPath();

        for(var i=0; i<cds.length; i++)
        {
            //console.log("x="+cds[i].x*50+" y="+cds[i].y*50);
            ctx.lineTo(cds[i].x*50,cds[i].y*50);

            // 求y最大值
            if(cds[i].y>ymax){
                ymax=cds[i].y;
                xmax=cds[i].x;
            }

            // 求y最小值
            if(cds[i].y<ymin){
                ymin=cds[i].y;
                xmin=cds[i].x;
            }
        } 

        ctx.stroke();
        ctx.closePath();

        // 极大值
        ctx.beginPath();
        ctx.moveTo(xmax*50,ymax*50-5);
        ctx.lineTo(xmax*50,ymax*50+5);

        ctx.save();
        ctx.scale(1,-1);
        ctx.fillText("ymax="+cutShort(ymax.toString(),8),xmax*50,-ymax*50);
        ctx.restore();

        ctx.stroke();
        ctx.closePath();

        // 极小值
        ctx.beginPath();
        ctx.moveTo(xmin*50,ymin*50-5);
        ctx.lineTo(xmin*50,ymin*50+5);

        ctx.save();
        ctx.scale(1,-1);
        ctx.fillText("ymin="+ymin,xmin*50,-ymin*50);
        ctx.restore();

        ctx.stroke();
        ctx.closePath();
    }

    function drawAxisX(ctx){
        ctx.save();

        ctx.lineWidth=0.5;
        ctx.strokeStyle='navy';
        ctx.fillStyle='navy';

        var start=-600;
        var end=600;

        // 画轴
        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();
        }

        // 写文字
        var i=0;
        for(x=start;x<end;x+=50){
            ctx.save();
            ctx.scale(1,-1);
            ctx.fillText(x/50,x,y+10);
            ctx.restore();
        }

        ctx.restore();
    }

    function drawAxisY(ctx){
        ctx.save();

        ctx.lineWidth=0.5;
        ctx.strokeStyle='navy';
        ctx.fillStyle='navy';

        var start=-100;
        var end=120;

        // 画轴
        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();
        }

        // 写文字
        x=-19;
        for(y=start;y<end;y+=50){
            ctx.save();

            ctx.scale(1,-1);

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

            ctx.restore();
        }

        ctx.restore();
    }

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

    function cutShort(str,length){
        if(str.length>length){
            str=str.substr(0,length)+"...";
        }

        return str;
    }
//-->
</script>

绘制函数y=(x^2-2x+1)/(x^2+x+2)的曲线的更多相关文章

  1. 绘制函数 y=x^2-2x-3/2x^2+2x+1 的曲线

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

  2. 求函数 y=x^2-2x-3/2x^2+2x+1 的极值

    解:展开函数式得到2yx2+2xy+y=x2-2x-3 继而得到(2y-1)x2+(2y+2)x+(y+3)=0 将上式看作x的二次方程,y组成了方程的系数. 只有Δ>=0,x才有实值. Δ=( ...

  3. MATLAB绘制函数图

    序言 Matlab可以根据用户给出的数据绘制相应的函数图.对于单个2D函数图,需要给出一个行向量x作为函数图上离散点集的横坐标,以及一个与x列数一样的横坐标y作为函数图上点集的纵坐标. 向量x和y的取 ...

  4. 2017年全国卷3的21题与2018年全国卷3的21题命题背景是同一个函数$y=\frac{2x}{\ln(x+1)}$(再次瞎谈)

    2017年四川高考数学(全国卷3)理科21题第1问 已知函数\(f(x)=x-1-a\ln x\) (1)若\(f(x)\geqslant 0\),求\(a\)的值\(.\) 该不等式等价于$a\ln ...

  5. 学习windows编程 day3 之窗口绘画二:边框绘制函数

    #include <windows.h> LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM l ...

  6. python之绘制函数pyplot初探

    我们想将我们手里的数据通过图形的方式展示出来,这样我们更直观的,更可以发现数据带给我们的信息.今天给大家介绍要给python中pyplot绘制函数.一般我们想将手里的数据绘制成图形,分为四大步:1.准 ...

  7. 使用神经网络来拟合函数y = x^3 +b

    我们使用一个三层的小网络来,模拟函数y = x^3+b函数 import tensorflow as tf import numpy as np import matplotlib.pyplot as ...

  8. 函数 y=x^x的分析

    关于函数 y=xx的分析: 由图像得,y在负无穷大到0图像处处不连续,故y的定义域为(0,正无穷大): 故该函数不就是y=e^(lnxx)吗? 1.定义域:我们变形一下,y=e^(xlnx),显然是0 ...

  9. 2、函数y=f(x)

    /* Note:Your choice is C IDE */ #include "stdio.h" /* 3.函数y=f(x)可表示为: */ void main() { int ...

随机推荐

  1. MySQL的数据引擎讲解

    一.MySQL的数据引擎讲解 在MySQL数据库中,常用的引擎主要就是2个:Innodb和MyIASM. 1.简单介绍这两种引擎,以及该如何去选择. a.Innodb引擎,Innodb引擎提供了对数据 ...

  2. hdu 4135 Co-prime (素数打表+容斥原理)

    题目链接 题意:问从A到B中与N互素的个数. 题解: 利用容斥原理:先求出与n互为素数的个数. 可以先将 n 进行素因子分解,然后用区间 x 除以 素因子,就得到了与 n 的 约数是那个素因子的个数, ...

  3. css文本、字母、数字过长 自动换行处理

    ---恢复内容开始--- white-space: normal|pre|nowrap|pre-wrap|pre-line|inherit;white-space 属性设置如何处理元素内的空白norm ...

  4. Python 实现腾讯新闻抓取

    原文地址:http://www.cnblogs.com/rails3/archive/2012/08/14/2636780.htm 思路: 1.抓取腾讯新闻列表页面: http://news.qq.c ...

  5. BZOJ2120/洛谷P1903 [国家集训队] 数颜色 [带修改莫队]

    BZOJ传送门:洛谷传送门 数颜色 题目描述 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会向你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R ...

  6. 通过邮箱验证注册——.net代码

    在写一些面向用户的网站类的程序时,必不可少的一个就是注册,通常情况下,我们会选择邮箱验证后注册,或者手机发送验证码注册.上篇文章中已经简单的描述了手机验证注册,这篇主要介绍一下邮箱验证. 邮箱验证的步 ...

  7. 【POJ 2409】Let it Bead

    http://poj.org/problem?id=2409 Burnside引理:设\(G\)是\(X\)的置换群,而\(\mathcal{C}\)是\(X\)中一个满足下面条件的着色集合:对于\( ...

  8. 【Trie模板】HDU1251-统计难题

    [题意] n统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). [思路] 裸题,不过G++好像会超内存,C++就不会. #include<iostream> #include& ...

  9. Problem C: 输入10个数,根据提示进行从小到大输出或从大到小输出

    #include<stdio.h> int main() { char ch; ]; while(scanf("%c",&ch)!=EOF) { int i,j ...

  10. 推荐系统中的SVD

    本文主要参考:Factorization Meets the Neighborhood: a Multifaceted Collaborative Filtering Model 在用户对自己需求相对 ...