我有两个角度a和b,我想计算两个角度之间的绝对差值。实例
>> absDiffDeg(360,5)
ans = 5
>> absDiffDeg(-5,5)
ans = 10
>> absDiffDeg(5,-5)
ans = 10
答案 0 :(得分:6)
归一化差值,不需要abs运算,因为mod(x,y)取y的符号。
func scrollViewDidScroll(scrollView: UIScrollView) {
// Work out which view is showing on the left. 0 to N-1
// Based on each being 320 wide.
CGFloat offset=scrollView.contentOffset.x;
NSUInteger leftHandSideViewIndex=(NSUInteger)offset.x/320;
// Left hand side alpha is 1 minus percentage (as a fraction) off screen
self.coloredViews[leftHandSide].alpha =
1.0 - (offset - (320.0 * leftHandSideViewIndex))/320.0;
// Right hand side alpha is 1 - percentage (as a fraction) on screen
// Only do this if there is a right hand side.
if (leftHandSideViewIndex < self.coloredViews.count-1){
self.coloredViews[leftHandSide+1].alpha =
1.0 - ((320.0 * leftHandSizeViewIndex+1)-offset)/320.0;
}
}
这是0-360之间的数字,但我们想要的最小角度在0-180之间。最简单的方法是
normDeg = mod(a-b,360);
答案 1 :(得分:3)
使用角度进行数学运算时,首先将它们标准化很有用。此功能将所有角度标准化为(-180,180)范围:
normalizeDeg=@(x)(-mod(-x+180,360)+180)
现在可以使用此函数进行标准化,可以计算绝对差值:
absDiffDeg=@(a,b)abs(normalizeDeg(normalizeDeg(a)-normalizeDeg(b)))
答案 2 :(得分:2)
uning unwrap怎么样?这是一个尝试:
absDiffDeg = @(a,b) abs(diff(unwrap([a,b]/180*pi)*180/pi));
最佳,