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();
    }

任何人都可以看到该代码有任何直接问题吗?变量 trtb 始终评估为 0。

如果需要的话,我很乐意提供更多信息。

有帮助吗?

解决方案

您还没有给出类型的其他变量 - 特别是,什么是类型camera.fov_xcamera.fov_y的?如果它们都是整数,然后初始化angxangy线将使用整数运算进行评价。

这可以通过铸造一个操作数是固定的:

double angy = ((double) camera.fov_y / h) * iy;

fovyfovx变量已经加倍虽然,这是没问题的。

可不可以给的这个完整的例子,我们可以编译和测试自己呢?

编辑:Koistya纳文的编辑是矫枉过正。你只需要一个表达的一个操作数是双对整件事采用双算术进行计算。 (它需要合适的表情,虽然 - 如果你做(a/b) * c和投c双,乘法将与双运算来完成,但A / B可能仍然要做为整数)

下面是无处不在使用适当改变,以确保双算术上市应该是:

// Changed loops to declare the variable, for stylistic purposes
for (int iy = 0; iy < h; iy++)
{
    // Changed here - cast camera.fov_y
    double angy = ((double) camera.fov_y / h) * iy;
    for (int ix = 0; ix < w; ix++)
    {
        // Changed here - cast camera.fov_x
        double angx = ((double) 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();
}

其他提示

它知道这不是您原来问题的一部分,但使用 SetPixel(..) 效率不是很高,如果您计划在光线跟踪引擎中使用它,可能会出现问题。

您可能想使用 LockBits() 方法,请参阅此 回答这个 更多细节。另一种方法是使用“不安全”C# 代码访问数据,该代码允许您使用指向数据的指针。看 这个问题 有关更多信息,我通过使用“不安全”代码获得了〜x2的加速。

不要忘了投你的整数双打。例如:

for (iy = 0; iy < h; iy++)
{
    double angy = ((double) camera.fov_y / h) * iy;
    for (ix = 0; ix < w; ix++)
    {
        double angx = ((double) camera.fov_x / (double) w) * (double) 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();
} 

快速参考:

 int * double = double
 int / double = double
 double * int = double
 double / int = double

 int * int = int
 int / int = int // be carefull here!

 1 / 10 = 0 (not 0.1D)
 10 / 11 = 0 (not 1)
 1D / 10 = 0.1D
 1 / 10D = 0.1D
 1D / 10D = 0.1D
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top