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?
I have a location which is a random x, y
in generateRandomLocation
.
I require to generate a new x,y
which is 90 degrees to the left or 90 degrees to the right of the current position.
I then need to rotate the image so that it "faces" the correct position.
I am just unsure how to do the 90 degree calculation.
Attempted solution;
not sure if I'm doing it right.
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];
}