4

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

genpfault
  • 51,148
  • 11
  • 85
  • 139
Dpetrov
  • 546
  • 2
  • 5
  • 12
  • you need to set your x-axis and y-axis scales correctly – Ahmed Masud May 21 '18 at 07:16
  • Don't only plot single pixel dots. Draw little disks. Consider drawing lines from point to point, possibly with line thicknesses > 1. – Yunnosch May 21 '18 at 07:47
  • Before you actually start plotting, run the loop to calculate the maximum and minimum `y`. Then use the difference of that to decide the range and scale of your graph. That is the only way you will not see these skinny graphs – Ajay Brahmakshatriya May 21 '18 at 07:49
  • 1
    It might be because the library you are using went obsolete before Internet became a thing :) – Lundin May 21 '18 at 08:08

2 Answers2

1

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

Dpetrov
  • 546
  • 2
  • 5
  • 12
  • I would like to see the result for `1/x`. You seem to avoid the definition gap at 0, but in a rather brutal way. – Yunnosch May 21 '18 at 13:38
0

In order to get your pixels more visible, use e.g. disks of radius 2 (radius 1 might suffice).

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

You can draw lines between points to get a very good "graph" impression,
but be careful not to incorrectly represent at definition gaps.
E.g. drawing a line from (X=-0.1,Y=1/-0,1) to (X=0.1,Y=1/0,1) would be wrong.

line(x1, y1, x2, y2);

As the comments have noted, adapting your scaling of both axes might improve the impression, but that probably only works satisfyingly for a single graph. One among several with different extrema might still be looking skinny.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54