有没有办法扭转atan2的效果?
我有一个关于逆向 atan2
的具体问题,我将用 PHP 编写我的代码示例。
$radial = 1.12*PI();
$transformed = -atan2(cos($radial)*2, sin($radial)*1.5);
$backToRadial = ?
有没有办法在不知道起始径向的情况下将转换后的值反转为起始径向? 代码流程应该是这样的: $radial =>变换($径向)=> Transformback(transform($radial)) =>; $径向
。
我在网上搜索(包括堆栈)但找不到任何正确的代码。还查了一下维基百科,但是实在是太震撼了。我认为我的问题更多的是代数问题;)。
让我知道你的想法!
_
Jason S 的回答:
Answer (by radial range: -PI-PI):
$backToRadial = atan2(2*cos($transformed), -1.5*sin($transformed));
Answer (by radial range: 0-2PI):
$backToRadial = PI() + atan2(-(2*cos($transformed)), -(-1.5*sin($transformed)));
I have a specific question about reversing atan2
, I will write my code example in PHP.
$radial = 1.12*PI();
$transformed = -atan2(cos($radial)*2, sin($radial)*1.5);
$backToRadial = ?
Is there a way to reverse the transformed value to the start radial, without knowing the start radial?
This is how code flow should be: $radial => transform($radial) => transformback(transform($radial)) => $radial
.
I have searched on the web (incl. stack) but I couldn't find any correct code. Also looked on Wikipedia, but it was overwhelming. My question is more a algebra question I think ;).
Let me know what you think!
_
Answered by Jason S:
Answer (by radial range: -PI-PI):
$backToRadial = atan2(2*cos($transformed), -1.5*sin($transformed));
Answer (by radial range: 0-2PI):
$backToRadial = PI() + atan2(-(2*cos($transformed)), -(-1.5*sin($transformed)));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
适用于主角(θ = -π 到 +π 范围内的角度)的简单答案如下:
其中第一个方程是正向变换,第二个方程是许多逆变换之一。
这样做的原因是,您所做的相当于笛卡尔坐标对 (x,y) = (r cos θ, r sin θ) 的反射 + 缩放 + 单位幅度归一化,因为 r =1 atan2(y,x) = θ。
有效的具体转换是(x',y') = (1.5y, -2x)。
θ' = atan2(y',x') = atan2(-2x, 1.5y) = atan2(-2Rcos θ, 1.5Rsin θ) = -atan2(2 cos θ, 1.5 sin θ),
最后一步为真,因为对于任何 k > atan2(ky,kx) = atan2(y,x) 0,并且 -atan2(y,x) = atan2(-y, x)。
这可以通过求解 x 和 y 来逆转,即 y = 1/1.5 * x' 和 x = -1/2 * y':
θ = atan2(y,x) = atan2(1/1.5 * x', - 1/2 * y')
,我们选择将 (x,y) 乘以 k = 3/R 以保持角度不变:
θ = atan2(2x'/R, -1.5y'/R) = atan2(2 cos θ', -1.5 sin θ')
QED
编辑:Jason 正确地指出,您的示例角度 1.12π 不在主角范围 -π 到 +π 内。您需要定义您希望能够处理的角度范围,并且该范围的长度最多为 2π。
我的答案可以相应调整,但需要一些工作来验证,如果您坚持 -π 到 +π 范围,您会变得更容易,因为您使用的是
atan2()
并且其输出在此范围内。如果您想使用输出 0-2π 范围内角度的
atan2()
修改版本,我建议使用atan2b 现在输出 0 到 2π 之间的角度,因为计算 atan2(-y ,-x) 与 atan2(y,x) 相差 π (mod 2π)
如果您打算采用这种方法,请不要计算
-atan2b(y,x)
;而是计算atan2b(-y,x)
(等效 mod 2π),以使输出角度范围保持不变。A simple answer that will work for principal angles (angles over the range θ = -π to +π) is as follows:
where the first equation is your forward transformation, and the second equation is one of many inverse transformations.
The reason for this is that what you're doing is equivalent to a reflection + scaling + unit-magnitude-normalization of the cartesian coordinate pair (x,y) = (r cos θ, r sin θ) for r =1, since atan2(y,x) = θ.
A specific transformation that will work is (x',y') = (1.5y, -2x).
θ' = atan2(y',x') = atan2(-2x, 1.5y) = atan2(-2Rcos θ, 1.5Rsin θ) = -atan2(2 cos θ, 1.5 sin θ),
with the last step true since atan2(ky,kx) = atan2(y,x) for any k > 0, and -atan2(y,x) = atan2(-y, x).
This can be reversed by solving for x and y, namely y = 1/1.5 * x' and x = -1/2 * y':
θ = atan2(y,x) = atan2(1/1.5 * x', -1/2 * y')
and we choose to multiply (x,y) by k = 3/R to leave the angle unchanged:
θ = atan2(2x'/R, -1.5y'/R) = atan2(2 cos θ', -1.5 sin θ')
Q.E.D.
edit: Jason points out, correctly, that your example angle 1.12π is not in the principal angle range -π to +π. You need to define the range of angles you wish to be able to handle, and it has to be a range of at most length 2π.
My answer can be adjusted accordingly, but it takes a bit of work to verify, and you would make it easier on yourself if you stuck to the -π to +π range, since you are using
atan2()
and its output is in this range.If you want to use a modified version of
atan2()
that outputs angles in the 0-2π range, I'd recommend usingwhere atan2b now outputs between 0 and 2π, since the calculation atan2(-y,-x) differs from atan2(y,x) by an angle of π (mod 2π)
If you're going to take this approach, don't calculate
-atan2b(y,x)
; instead calculateatan2b(-y,x)
, (equivalent mod 2π) so that the range of output angles is left unchanged.首先,
atan2
与 tan-1 或arctan
不同,如下面关于<的 Wiki 文章所示a href="http://en.wikipedia.org/wiki/Atan2" rel="nofollow noreferrer">atan2
:如您所见,如果没有有关
x
和 < 的一些信息,则无法将其映射回来代码>y。但是,如果x>0
始终为 true,那么您只需使用反正切函数等。您可以使用此表示来计算反函数:
在您的示例中,y = 2cos(r) 和 x = 1.5sin(r) >。因此,如果将上述表达式除以 y,则会得到 x/y 的形式,在您的情况下为 4/3 cot(r) 。
如果此表示正确,一些简单的代数将为您提供:
其中 r =
径向
和 k = cot(变换
/2)WolframAlpha 给出了一个解决方案:
但取决于您的资源,最好找到具有固定值 k 的函数的根。例如,如果k = 1.35,那么您需要求解:
任何合适的求解器(因此对您拥有的资源的评论)例如 MATLAB 将解决这个问题。 <一href="http://www.wolframalpha.com/input/?i=sqrt%28%28%284/3%29*cot%28r%29%29%5E2%20%2 b%20%28%283/4%29*tan%28r%29%29%5E2%29%20%2b%20%284/3%29*cot%28r%29%20-%201.35%20=% 200" rel="nofollow noreferrer">WolframAlpha 提供了以下近似真实解决方案:
First,
atan2
is not the same as tan-1 orarctan
as seen below from the Wiki article onatan2
:As you can see, you cannot map it back without some information regarding
x
andy
. However, ifx>0
is always true, then you just take use the inverse tangent function, etc.You could use this representation, to compute the inverse function:
In your example, y = 2cos(r) and x = 1.5sin(r). Therefore, if you divide the above expression with y, you get it in the form of x/y which in your case is 4/3 cot(r).
If this representation is correct, some simple algebra gives you:
where r =
radial
and k = cot(transformed
/2)WolframAlpha gave a solution to this:
But depending on your resources, it's probably better to find the root of the function with a fixed value of k. E.g. if k = 1.35, then you need to solve:
Any decent solver (and hence the comment on the resources you have) such as MATLAB will solve this. WolframAlpha provided the following approximate real solution: