Possible Duplicate:
Convert to absolute value in Objective-C可能重复:在Objective-C中转换为绝对值
As i am playing with 2d graphics, i'd like to calculate amount of points object has moved between 2 CGPoints
. Given that object can move in both direction, i am only interested in sheer number of points representing the difference.
当我正在玩2D图形时,我想计算对象在2个CGPoints之间移动的点数。鉴于物体可以向两个方向移动,我只对表示差异的纯粹点数感兴趣。
In Java
i'd Math.abs(startpoint.x - endpoint.x)
在Java中我是Math.abs(startpoint.x - endpoint.x)
How can i do the same in Objective-C
?
我如何在Objective-C中做同样的事情?
1 个解决方案
#1
65
There are C functions from <math.h>
that will do what you want:
abs(int val);
labs(long val);
llabs(long long val);
fabs(double val);
fabsf(float val);
fabsl(long double val):
Given that CGPoint
structures are composed of CGFloat
s, you should use fabsf
here.
鉴于CGPoint结构由CGFloats组成,您应该在这里使用fabsf。
Check out the Wikipedia page
查看Wikipedia页面
#1
65
There are C functions from <math.h>
that will do what you want:
abs(int val);
labs(long val);
llabs(long long val);
fabs(double val);
fabsf(float val);
fabsl(long double val):
Given that CGPoint
structures are composed of CGFloat
s, you should use fabsf
here.
鉴于CGPoint结构由CGFloats组成,您应该在这里使用fabsf。
Check out the Wikipedia page
查看Wikipedia页面