-1

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];
    }
Atilla Jax
  • 615
  • 5
  • 15
  • 2 points don't form an angle. You need two lines (two vectors) to form an angle. You are forgetting about the 3rd point - the center of rotation. Then 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. – Sulthan Jan 20 '15 at 10:00
  • Do you mean the image's anchorpoint? – Atilla Jax Jan 20 '15 at 10:10
  • That really depends on how you expect it to work. I assume that the center point of the image is probably what you want. Anyway, you need 3 points to define a rotation, not 2. – Sulthan Jan 20 '15 at 10:12
  • Yes. The center point of my image is where I would like rotation to be. So I will try your suggestion. Many thanks – Atilla Jax Jan 20 '15 at 10:27
  • The correct anchor point is `planeObj.imageView.layer.anchorPoint = CGPointMake(0.5, 0.357);` – Atilla Jax Jan 20 '15 at 10:28
  • You need to convert that to the same coordinate spaces, that means `CGPointMake(imageView.frame.origin.x + imageView.layer.anchorPoint.x * imageView.frame.size.width, imageView.frame.origin.y + imageView.layer.anchorPoint.y * imageView.frame.size.height)` – Sulthan Jan 20 '15 at 10:31
  • Sorry about this, just not done much with angles before. But thanks – Atilla Jax Jan 20 '15 at 10:41

1 Answers1

2

Try this:

Convert your CGPoint to a vector then use:

 /*
  * 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);
}

and convert back to CGPoint.

David Silverfarmer
  • 455
  • 1
  • 3
  • 13
  • btw, we are speaking about 90 degrees (π/2 radians). You might be interested to know that `cos(π / 2) == 0` and `sin(π / 2) == 1`. That will make this specific case a lot easier... – Sulthan Jan 20 '15 at 09:54
  • @DavidSilverfarmer Copy-paste mistake :) I think to really help the OP the answer should be extended to explain how to get the vector - there must be 3 points, not only 2. The OP is forgetting about the center of rotation. – Sulthan Jan 20 '15 at 09:57
  • @Sulthan Just making sure :) – David Silverfarmer Jan 20 '15 at 09:58