for (iy = 0; iy < h; iy++)
{
double angy = (camera.fov_y / h) * iy;
for (ix = 0; ix < w; ix++)
{
double angx = (camera.fov_x / w) * ix;
//output[ix,iy].r = (int)Math.Round(255 * (angy / camera.fov_y);
//output[ix,iy].b = (int)Math.Round(255 * (angy / camera.fov_y);
double tr = (angx / camera.fov_x) * 255D;
double tb = (angy / camera.fov_y) * 255D;
Console.Write("({0},{1})",Math.Round(tr),Math.Round(tb));
output.SetPixel(ix, iy, Color.FromArgb(Convert.ToInt32(tr), 0, Convert.ToInt32(tb)) );
Console.Write(".");
}
Console.WriteLine();
}
Can anyone see any immediate problems with that code?
The variables tr and tb always evaluate to 0.
I’m happy to provide more information if it is needed.
You haven’t given the types for the other variables – in particular, what are the types of
camera.fov_xandcamera.fov_y? If they’re both integers, then the lines initializingangxandangywill be evaluated using integer arithmetic.This can be fixed by casting one of the operands:
The
fovyandfovxvariables are already doubles though, this isn’t the problem.Could you give a complete example of this, which we can compile and test ourselves?
EDIT: Koistya Navin’s edit is overkill. You only need one operand of an expression to be a double for the whole thing to be computed using double arithmetic. (It needs to be the right expression though – if you do
(a/b) * cand castca double, the multiplication will be done with double arithmetic but a/b might still be done as integers.)Here’s the listing changed appropriately to make sure double arithmetic is used everywhere it should be: