draw graph of the function in C graphics

Dpetrov picture Dpetrov · May 21, 2018 · Viewed 10.5k times · Source

I'm trying to draw a graph of a function. How to do it correctly, i cant find any resources in internet on that.

code:

#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);
}

I need it to be more visible and not that skiny. What would be the aproach. also here are the functions to be implemented: (i started with the last function)

enter image description here

output: enter image description here

EDIT: So for those interested, i did it, the code is here

here output: all 3 graphs

Answer

Dpetrov picture Dpetrov · May 21, 2018

So, since its old library and my university still uses it face palm anyone interested i figure it out.

code:

#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);
}

output: enter image description here