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
: