如何生成始终与 UIImage 当前位置左侧或右侧成 90 度角的“x,y”坐标?
How do I generate a `x,y` co-ordinates that is always 90 degrees either left or to the right of the current position of a UIImage?
如何生成一个 x,y
始终与 UIImage 当前位置向左或向右 90 度的坐标?
我在 generateRandomLocation
中有一个随机 x, y
的位置。
我需要生成一个新的 x,y
,它位于当前位置左侧 90 度或右侧 90 度。
然后我需要旋转图像,使其 "faces" 处于正确的位置。
我只是不确定如何计算 90 度。
尝试的解决方案;
不确定我做的是否正确。
CGPoint location;
// Create airplane object
for (int i=0; i<5; i++) {
location = [self generateRandomLocation];
VICAirplane *planeObj = [[VICAirplane alloc] init];
planeObj.x = location.x;
planeObj.y = location.y;
planeObj.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(location.x, location.y, _spriteWidth, _spriteHeight)];
planeObj.imageView.layer.anchorPoint = CGPointMake(0.5, 0.357); // Do not change this
planeObj.imageView.backgroundColor = [UIColor redColor];
planeObj.imageView.transform = CGAffineTransformIdentity;
[planeObj.imageView setImage:_sprite];
[self.worldMap addSubview:planeObj.imageView];
//
// Generate angle
NSNumber *angle = [self.airplaneAngles firstObject];
CGFloat angleToRotate = [angle floatValue]; // Will be 45 degrees
//
// You need two lines (two vectors) to form an angle.
// The two vectors will be defined as:
// CA (calculated as A - C) and
// CB (calculated as B - C) where C is the center of rotation and A, B are the two points
//
CGPoint point = CGPointMake(planeObj.imageView.frame.origin.x + planeObj.imageView.layer.anchorPoint.x * planeObj.imageView.frame.size.width,
planeObj.imageView.frame.origin.y + planeObj.imageView.layer.anchorPoint.y * planeObj.imageView.frame.size.height);
CGFloat CA = location.x - point.x;
CGFloat CB = location.y - point.y;
CGVector v = CGVectorMake(CA, CB);
CGVector vector = [self vectorFromVector:v rotateByAngle:angleToRotate];
CGPoint newLocation = CGPointMake(vector.dx, vector.dy);
//planeObj.imageView.transform = CGAffineTransformRotate(planeObj.imageView.transform, angleToRotate);
planeObj.rotateToLocation = newLocation;
planeObj.angleToRotate = angleToRotate;
// Add to array
[self.airplanes addObject:planeObj];
}
试试这个:
将您的 CGPoint 转换为矢量然后使用:
/*
* Returns the vector rotated by an angle
*/
- (CGVector)vectorFromVector:(CGVector)vector rotatetByAngle:(CGFloat)radians{
return CGVectorMake(cosf(radians) * vector.dx - sinf(radians) * vector.dy, sinf(radians) * vector.dx + cosf(radians) * vector.dy);
}
并转换回 CGPoint。
如何生成一个 x,y
始终与 UIImage 当前位置向左或向右 90 度的坐标?
我在 generateRandomLocation
中有一个随机 x, y
的位置。
我需要生成一个新的 x,y
,它位于当前位置左侧 90 度或右侧 90 度。
然后我需要旋转图像,使其 "faces" 处于正确的位置。
我只是不确定如何计算 90 度。
尝试的解决方案;
不确定我做的是否正确。
CGPoint location;
// Create airplane object
for (int i=0; i<5; i++) {
location = [self generateRandomLocation];
VICAirplane *planeObj = [[VICAirplane alloc] init];
planeObj.x = location.x;
planeObj.y = location.y;
planeObj.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(location.x, location.y, _spriteWidth, _spriteHeight)];
planeObj.imageView.layer.anchorPoint = CGPointMake(0.5, 0.357); // Do not change this
planeObj.imageView.backgroundColor = [UIColor redColor];
planeObj.imageView.transform = CGAffineTransformIdentity;
[planeObj.imageView setImage:_sprite];
[self.worldMap addSubview:planeObj.imageView];
//
// Generate angle
NSNumber *angle = [self.airplaneAngles firstObject];
CGFloat angleToRotate = [angle floatValue]; // Will be 45 degrees
//
// You need two lines (two vectors) to form an angle.
// The two vectors will be defined as:
// CA (calculated as A - C) and
// CB (calculated as B - C) where C is the center of rotation and A, B are the two points
//
CGPoint point = CGPointMake(planeObj.imageView.frame.origin.x + planeObj.imageView.layer.anchorPoint.x * planeObj.imageView.frame.size.width,
planeObj.imageView.frame.origin.y + planeObj.imageView.layer.anchorPoint.y * planeObj.imageView.frame.size.height);
CGFloat CA = location.x - point.x;
CGFloat CB = location.y - point.y;
CGVector v = CGVectorMake(CA, CB);
CGVector vector = [self vectorFromVector:v rotateByAngle:angleToRotate];
CGPoint newLocation = CGPointMake(vector.dx, vector.dy);
//planeObj.imageView.transform = CGAffineTransformRotate(planeObj.imageView.transform, angleToRotate);
planeObj.rotateToLocation = newLocation;
planeObj.angleToRotate = angleToRotate;
// Add to array
[self.airplanes addObject:planeObj];
}
试试这个:
将您的 CGPoint 转换为矢量然后使用:
/*
* Returns the vector rotated by an angle
*/
- (CGVector)vectorFromVector:(CGVector)vector rotatetByAngle:(CGFloat)radians{
return CGVectorMake(cosf(radians) * vector.dx - sinf(radians) * vector.dy, sinf(radians) * vector.dx + cosf(radians) * vector.dy);
}
并转换回 CGPoint。