是否有一个功能可以将一个圆圈的度数转移到0以上? [英] Is there a function to shift degrees of a circle past 0?

查看:222
本文介绍了是否有一个功能可以将一个圆圈的度数转移到0以上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Delphi XE2中找到类似于 Inc()的某个功能,它允许我从当前数量的度数中加减法,导致新的学位。例如,如果我有一个点在一个圆圈周围5度,我想减去10,我不应该得到-5度,而不是355(360 - 5)。与添加过去360相同 - 当它达到360时应该返回到0。



在Delphi中是否有这样的东西,所以我不必重写它?可能在 Math 单位?

解决方案

 使用
System.SysUtils,Math;

功能WrapAngle(angle:Double):Double;
Const
modAngle:Double = 360.0;
begin
结果:= angle - modAngle * Floor(angle / modAngle);
结束

begin
WriteLn(FloatToStr(WrapAngle(-5)));
WriteLn(FloatToStr(WrapAngle(5-720)));
WriteLn(FloatToStr(WrapAngle(360)));
ReadLn;
结束。

产生结果:

  355 
5
0

更新: p>

正如@Giel所发现的,在XE3中有一个新功能 DegNormalize()这样做。甚至快25%。诀窍是用 Int()替换 Floor()调用,如果结果为负,将 modAngle 添加到结果中。


I'm looking for a function somewhere in Delphi XE2 similar to Inc() which allows me to add/subtract a number of degrees from a current number of degrees and result in the new degrees. For example, if I have a point currently at 5 degrees around a circle, and I want to subtract 10, I should not get -5 degrees, but rather 355 (360 - 5). Same as adding past 360 - it should go back to 0 when it reaches 360.

Is there anything like this already in Delphi so I don't have to re-write it? Perhaps in the Math unit?

解决方案

uses
  System.SysUtils,Math;

Function WrapAngle( angle : Double) : Double;
Const
  modAngle : Double = 360.0;
begin
  Result := angle - modAngle*Floor(angle/modAngle);
end;

begin
  WriteLn(FloatToStr(WrapAngle(-5)));
  WriteLn(FloatToStr(WrapAngle(5-720)));
  WriteLn(FloatToStr(WrapAngle(360)));
  ReadLn;    
end.

Produces result:

355
5 
0

Update:

As @Giel found, in XE3 there is a new function DegNormalize() which does the job. Even about 25% faster. The trick is to replace the Floor() call with an Int() instead, and if the result is negative, add modAngle to the result.

这篇关于是否有一个功能可以将一个圆圈的度数转移到0以上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆