Search code examples
iosimagedictionaryscale

how to calculate scale factor between 2 NON-SIMILAR rectangles


Hope somebody can help me :)

I'm trying to scale a point from a map (Latitude, Longitude) to an image (x, y). For that i need to find the scale factor between the 2 NON-SIMILAR rectangles (i think).

I'll clarify, let's say:

Rectangle 1: A(40.0, 50.0) B(40.0, 56.0) C(43.0, 56.0) D(43.0, 50.0)

(Latitude Delta = 3, Longitude Delta = 6).

Rectangle 2: E(0, 0) F(500, 0) G(500, 300) H(0, 300)

(X Delta = 500, Y Delta = 300).

How can i scale a point P(41.5, 52.5) from rectangle 1 to point (x, y) on rectangle 2?

UPDATE:

General idea:

I'm trying to display the user current location (Lat, Lon) on a custom image (not a map image, a drawing of my own) therefor i can't use maps (MKMapKit, Google, Tom-Tom).

I have the user current location (via CoreLocation) and an image (800x460).

The area that i'm mapping is small so i don't need to worry about the earth's curve.

I'm trying to find a formula that'll help me scale my user (Lat, Lon) location into my image (on my iPhone screen)

Thanks!!!


Solution

  • Well , getting the rectangle that is similar with the first one but fits in the second one is not hard. Here's how:

    CGRect rect1, rect2; //your rectangles.
    float scale = MIN(rect1.size.width / rect2.size.width , rect1.size.height / rect2.size.height);
    CGRect resultedRect;
    resultedRect.origin = rect2.origin;
    resultedRect.size = CGSizeMake(rect2.size.width * scale, rect2.size.height * scale); 
    //resultedRect is similar with rect1 but fits in rect2. You can now multiply your point with "scale" and get the new position.
    

    I'm not sure this is what you are trying to achieve.

    Regards,

    George