Original Post
I''ve been trying to implement an algorithm that will convert screen coordinates (in pixels) into a 2D cartesian system where the origin is at the center of the screen and the x and y can be set to a specific size. As an example, I''m trying to map a system that has x from -4 to 4 and y from 3 to -3 in order to plot a Mandelbrot fractal. The section of code that plots the fractal looks like this:
The code works but the lines that convert the coordinates seem very ugly to me. I did this more or less through experimentation to find out what works but what I need to do is come up with a generic method for performing this kind of operation so that I can use it in other programs without having to ''hardwire'' it. If anyone could give me a push in the right direction I would appreciate it. I know this is an elementary concept in graphics programming, but I''m still a beginner. BTW: This is not a school project, programming is simply a hobby
Phillip
for (y=0; y<SCREEN_HEIGHT; y++)
{
for (x=0; x<SCREEN_WIDTH; x++)
{
// convert pixel coordiates into graph coords
comp_x = (4*(x-SCREEN_WIDTH/2))/SCREEN_WIDTH;
comp_y = -((3*(y-SCREEN_HEIGHT/2))/SCREEN_HEIGHT);
Complex z;
Complex c(comp_x, comp_y);
for (iter=0; iter<maxIter; iter++)
{
z = z*z + c;
dist = z.distance();
if (dist>2)
{
if(iter!=0)
color = ColorIndex[iter];
break;
}
}
if (dist<2)
color=RGB(0, 0, 0);
SetPixel(hdc, x, y, color);
}
}