0

I'm writing an algorithm for plotting mathematical functions. I'm new to C++ and programming in general and thus do not know how to implement this properly.

In order to do this, I naively plot the points satisfying the mathematical condition into a bitmap(ppm file) but when I do this and plot a parabola(y*y =4*a*x), only a few points are plotted in the graph. the coordinate axes and straight lines(y=mx+c) are rendered fine.

Here's my code: I draw to a ppm file

int main()
{

    int width = 512;
    int height = 512;
    std::ofstream ofs("plot.ppm", std::ios::out);
    ofs << "P3\n" << width << " " << height << "\n255\n";
    for (int h = 0;h< height;h++) {
        for (int w = 0; w < width; w++) {
            vec3f col;
            float x = ((float)w - 256);
            float y = -((float)h - 256);
            if (h == 256||w==256)col = vec3f(0, 0, 0);//axes
            else col = vec3f(1,1,1);//background
            if (y==x)col = vec3f(0, 0, 0);//straight line
            if ((y*y) == (4*16*x))col=vec3f(1, 0, 0);//parabola
            int r = int(255.99 * col.x);
            int g = int(255.99 * col.y);
            int b = int(255.99 * col.z);

            ofs << r << " " << g << " " << b << "\n";
        }
    }
    ofs.close();
}

This program plots only a few points which, upon checking the debugger, I found out to be : (0,0),(225,120),(196,112),(169,104),(144,96) and so on [these are the coordinates after origin is translated to (256,256), the center of the image]

I want to know why this is happening and how to fix this.

here's the output image plot.ppm :

the output

Shriram
  • 167
  • 1
  • 13
  • If that's the case,how do I fix it? thanks. – Shriram Jun 06 '19 at 15:24
  • 2
    See this related question : [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) Instead of comparing with equality, check if the difference between the values is very small. – François Andrieux Jun 06 '19 at 15:25
  • @FrançoisAndrieux oh ok thanks I'll try that out – Shriram Jun 06 '19 at 15:28
  • 2
    You're only checking at integer values of `x` and `y`, so the only points you'll see on your parabola are the ones where integer values of `x` and `y` solve the equation. The points you're missing fall in between (e.g. when `y == 25`, `x == 9.765625`). – 1201ProgramAlarm Jun 06 '19 at 15:35
  • @FrançoisAndrieux is incorrect, 1201ProgramAlarm is correct. The integers you're dealing with in this problem are perfectly representable as floats, as they're all below 16,777,217. See: https://stackoverflow.com/questions/3793838/which-is-the-first-integer-that-an-ieee-754-float-is-incapable-of-representing-e (However, you should still never compare floats for equality, so it's worth keeping in mind what has been said by François.) – druckermanly Jun 06 '19 at 15:42
  • @druckermanly Yep, that makes sense. I'll remove my first comment, but I'll leave the second linking to the question about floating point math precision. – François Andrieux Jun 06 '19 at 15:43
  • @1201ProgramAlarm oh yikes. Silly me ,fixed it .thanks a lot – Shriram Jun 06 '19 at 16:10

0 Answers0