代码之家  ›  专栏  ›  技术社区  ›  Dpetrov

用c语言作图

  •  2
  • Dpetrov  · 技术社区  · 7 年前

    我想画一个函数的图。如何正确地做,我在网上找不到任何资源。

    代码:

    #include <graphics.h>
    #include <math.h>
    
    #define G GREEN
    
    int main()
    {
        int gd = DETECT, gm, angle = 0;
        double x, y;
            initgraph(&gd, &gm, NULL);
    
        int cx = getmaxx()/2, cy = getmaxy()/2;
        line(20, cy, getmaxx()-20, cy);
        line(cx, 20, cx, getmaxy()-20);
        outtextxy(cx, cy, "O");
        //y=5*x-3*sinx^2(k*x) y=cos(k*x) y=4x^7-3x^3+5
        setcolor(GREEN);
    
        for (x = 0; x < getmaxx(); x+=0.01) {
                /* Calculate y with given x */
                y = 4 * pow(x, 7) - (3 * pow(x, 3) + 5);
                y = cy - y;
    
                /* Coloring pixel at x and y */
                if (y < 0) {
                        break;
                }
                putpixel(x+cx, y, GREEN);
                delay(50);
        }
        for (x = 0; x < getmaxx() && x+cx >= 0; x-=0.01) {
                /* Calculate y with given x */
                y = 4 * pow(x, 7) - 3 * pow(x, 3) + 5;
                y = cy - y;
    
                /* Coloring pixel at x and y */
                if (y > getmaxy())
                        break;
                putpixel(x+cx, y, GREEN);
                delay(50);
        }
    
        getch();
        closegraph();
        return (0);
    }
    

    我需要让它更显眼,而不是那么皮包骨。怎么办? 这里还有要实现的函数:(我从最后一个函数开始)

    enter image description here

    输出: enter image description here

    编辑: 所以对于那些感兴趣的人,我做到了,代码是 here

    此处输出: all 3 graphs

    2 回复  |  直到 7 年前
        1
  •  0
  •   Yunnosch    7 年前

    为了让你的像素更清晰,可以使用半径为2的磁盘(半径为1就足够了)。

    fillellipse(x+cx, y, 2, 2);
    

    你可以在点之间画线来得到一个很好的“图形”印象,
    但请注意不要在定义间隙处错误地表示。
    例如,画一条从(x=-0.1,y=1/-0,1)到(x=0.1,y=1/0,1)的直线是错误的。

    line(x1, y1, x2, y2);
    

    正如评论所指出的,调整两个轴的缩放比例可能会改善印象,但这可能只适用于单个图形。几个四肢不同的人中有一个可能看起来仍然很瘦。

        2
  •  0
  •   Dpetrov    7 年前

    既然它的旧图书馆和我的大学还在使用它 掌心 任何感兴趣的人我都能搞清楚。

    代码:

    #include <graphics.h>
    #include <math.h>
    
    #define G GREEN
    #define X_AXIS  2
    #define Y_AXIS  7
    
    void    draw_last(float direction)
    {
        double x = 0, y, px, py, cx = getmaxx()/2, cy = getmaxy()/2;
    
        while (x <= X_AXIS && x >= -X_AXIS) {
            /* Calculate y with given x */
            y = 4 * pow(x, 7) - 3 * pow(x, 3) + 5;
    
            /* Calculate coordoninates to display */
            px = x * cx / X_AXIS + cx;
            /* -cy because of origin point in window(top left corner) */
            py = y * -cy / Y_AXIS + cy;
            /* in case boundaries are passed */
            if (py < 0 || py > getmaxy())
                break;
    
            if (x == 0) // only for first loop
                moveto(px, py);
            /* Draw segment line */
            lineto(px, py);
            /* update CP */
            moveto(px, py);
    
            x += direction;
            delay(20);
        }
    }
    
    int main()
    {
        int gd = DETECT, gm;
    
        initgraph(&gd, &gm, NULL);
    
        /* Draw the axis */
        int cx = getmaxx()/2, cy = getmaxy()/2;
    
        line(20, cy, getmaxx()-20, cy);
        line(cx, 20, cx, getmaxy()-20);
        outtextxy(cx, cy, "O");
        outtextxy(20, cy, "-2");
        outtextxy(getmaxx()-20, cy, "2");
        outtextxy(cx, 20, "7");
        outtextxy(cx, getmaxy()-20, "-7");
    
        setcolor(GREEN);
        setlinestyle(SOLID_LINE, 0, 2);
    
        /* from x=0 ++ */
        draw_last(0.01);
        /* from x=0 -- */
        draw_last(-0.01);
    
        getch();
        closegraph();
        return (0);
    }
    

    输出: enter image description here

    推荐文章