极性到矩形和背面

Ric*_*h95 5 c graphics polar-coordinates

我有点卡住了。我正在尝试实现基本的极坐标到矩形的转换,以匹配Photoshop的转换,但是我没有得到相同的结果。

从矩形转换为极性匹配与Photoshop匹配,但从极性转换为矩形却不匹配。

您可以在这张图中看到Photoshop和我的之间的区别: 极性转换

float a, b, ang, dist;
int px, py;
const PI=3.141592653589793;

// Convert from cartesian to polar
for (y=y_start; y<y_end; ++y)
{
    for (x=x_start; x<x_end; ++x)
    {
        a = (float)(x-X/2);
        b = (float)(y-Y/2);

        dist = (sqr(a*a + b*b)*2.0);

        ang = atan2(b,-a)*(58);
        ang = fmod(ang + 450.0,360.0);

        px = (int)(ang*X/360.0);
        py = (int)(dist);

        pset(x, y, 0, src(px,py,0));
        pset(x, y, 1, src(px,py,1));
        pset(x, y, 2, src(px,py,2));
    }
}

// Convert back to cartesian
for (y=y_start; y<y_end; ++y)
{
    for (x=x_start; x<x_end; ++x)
    {

        ang = ((float)x/X)*PI*2.0;

        dist = (float)y*0.5;

        px = (int)(cos(ang)*dist)+X/2;
        py = (int)(sin(ang)*dist)+Y/2;

        pset(x, y, 0, pget(px,py,0));
        pset(x, y, 1, pget(px,py,1));
        pset(x, y, 2, pget(px,py,2));
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的代码。我敢肯定,我在笛卡尔直角上已经搞砸了。该语言基于C。

我究竟做错了什么?有什么建议么?

Rol*_*d W 2

极坐标到笛卡尔变换存在两个问题:

  • 用于定义角度的坐标系的轴指向右侧 (x) 和向下 (y),而您使用具有向上 (x) 和向左 (y) 轴的坐标系进行笛卡尔到极坐标变换。将角度转换为笛卡尔坐标的代码应该是(我添加了一些舍入)

    px = round(-sin(ang)*dist + X/2.)
    py = round(-cos(ang)*dist + Y/2.)
    
    Run Code Online (Sandbox Code Playgroud)

    使用该代码,当增加 x 坐标时,您可以在最终图片中从红色移动到绿色到蓝色,而不是从灰色移动到蓝色到绿色。

  • 假设pgetpset同一个位图进行操作,您将覆盖源图像。循环结构带您沿着源图像中心周围的同心圆向外移动,同时从上到下逐行填充目标。在某个时刻,圆和线开始重叠,并且您开始读取之前修改的数据(发生在抛物线状形状的顶点)。它变得更加复杂,因为在某个时刻您开始读取修改后的数据的变换,以便它再次有效地变换(我猜这会导致右侧出现不规则的三角形区域)。