将笛卡尔坐标转换为极坐标 - Matlab

Converting Cartesian Coordinates To Polar Coordinates - Matlab

我在将笛卡尔坐标转换为极坐标,然后返回笛卡尔坐标以确认我的初始转换是否成功的整个转换过程中遇到了问题。

但是,由于某种原因,当我从第三象限将极坐标转换回笛卡尔坐标时,我的 xy 值是错误的。

以我这部分代码为例:

x = -2.075548439;
y = -2.481775416;

if x < 0 && y < 0 % QUAD 3

    radius = sqrt( (x^2) + (y^2) );
    theta = atand( (y*-1)/(x*-1) );
    theta = (270 - theta);
end

x = radius * cosd(theta); 
y = radius * sind(theta);

% answer: x = -2.481775416, and y = -2.075548439

其他三个象限内的所有其他 xy 转化将 xy 放回正确的顺序。

然而,这部分代码确实将 xy 放回正确的顺序:

x = 3.130287009;
y = -0.50613326;

if x > 0 && y < 0 % QUAD 4

    radius = sqrt( (x^2) + (y^2) );
    theta = atand( (y*-1)/(x) );
    theta = (360 - theta);
end

x = radius * cosd(theta); 
y = radius * sind(theta);

% answer: x = 3.130287009, and y = -0.50613326

您不需要检查特殊情况。以下代码可将任何象限中的点从笛卡尔坐标正确地转换为极坐标坐标。

x = -2.075548439;
y = -2.481775416;

radius = sqrt( (x^2) + (y^2) );
theta = atan2d( y, x );

x = radius * cosd(theta); 
y = radius * sind(theta);