put*_*nny 4 c++ java pascal trigonometry
我需要帮助才能使我的代码更有效率,并将其清理一下.
由于此所示的图像,X和Y可以围绕整个屏幕任何位置,我想找到的角度吨.有没有办法可以减少这里的行数?
注意:原点位于左上角,向右/向下移动正向移动
o := MiddleOfScreenX - x;
a := MiddleOfScreenY - y;
t := Abs(Degrees(ArcTan(o / a)));
if(x > MiddleOfScreenX)then
begin
if(y > MiddleOfScreenY)then
t := 180 + t
else
t := 360 - t;
end
else
if(y > MiddleOfScreenY)then
t := 180 - t;
Run Code Online (Sandbox Code Playgroud)
代码是pascal,但是其他语言中使用类似语法或c ++或java的答案也很好.
:= sets the variable to that value
Abs() result is the absolute of that value (removes negatives)
Degrees() converts from radians to degrees
ArcTan() returns the inverse tan
Run Code Online (Sandbox Code Playgroud)
有关C函数,请参阅http://www.cplusplus.com/reference/clibrary/cmath/atan2/.
atan2需要2个独立的参数,因此可以确定象限.
pascal可能有arctan2见http://www.freepascal.org/docs-html/rtl/math/arctan2.html或http://www.gnu-pascal.de/gpc/Run-Time-System.html
o := MiddleOfScreenX - x;
a := MiddleOfScreenY - y;
t := Degrees(ArcTan2(o, a));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
360 次 |
| 最近记录: |