减法不起作用

Subtraction Not Working

下面的代码不起作用(它在 touchesBegan 中)

UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];

NSUInteger a = touchLocation.y;
NSUInteger b = touchLocation.x;
NSUInteger c = _player.center.y;
NSUInteger d = _player.center.x;

NSLog(@"%lu", (unsigned long) b);
NSLog(@"%lu", (unsigned long) a);
NSLog(@"%lu", (unsigned long) d);
NSLog(@"%lu", (unsigned long) c);

int offSetY = a - c;
int offSetX = b - d;
float slope = (offSetY/offSetX);

NSLog(@"%lu", (unsigned long) offSetX);
NSLog(@"%lu", (unsigned long) offSetY);
NSLog(@"%f", slope);

嗯,75% 正确。当我点击屏幕右上象限时,结果每个变量都很好,但 offSetX 超出了图表(通常或多或少超过 1800000000)。谁能帮我吗?我不明白为什么会出错。

为什么要转换为 NSUInteger 来计算坡度?请改用 CGFloat

UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];

CGPoint playerPoint = CGPointMake(100, 100);

CGFloat a = touchLocation.y;
CGFloat b = touchLocation.x;
CGFloat c = playerPoint.y;
CGFloat d = playerPoint.x;

NSLog(@"%f", b);
NSLog(@"%f", a);
NSLog(@"%f", d);
NSLog(@"%f", c);

CGFloat offSetY = a - c;
CGFloat offSetX = b - d;
CGFloat slope = (offSetY/offSetX);

NSLog(@"%f", offSetX);
NSLog(@"%f", offSetY);
NSLog(@"%f", slope);