The following forum(s) have migrated to Microsoft Q&A (Preview): Developing Universal Windows apps!
Visit Microsoft Q&A (Preview) to post new questions.

Learn More

 locked
Geometry: How to find the size of a rectangle? RRS feed

  • Question

  • I've described my query in pictorial representation below.

    I'm picking 2 photos from the gallery, one acts as background and the other acts as foreground. Taking performance into consideration I've compressed both the images for reframing foreground over static background.

    After reframing, I've the measurements of rectangle's XY, xy, AB, ab and pq, how to know the measurement of rectangle cd?


    Clipr : Best ever clipping tool in Windows Phone!
    Download here

    Tuesday, September 30, 2014 4:23 PM

Answers

  • Looking at your code you are using a uniform scale factor (it doesn't matter in any way how you got to what scaling factor to use). If you use one scale factor to downscale you will have to use the same scale factor for upscaling.

    It's however not sufficient to just scale the size of the separate images but you will also have to scale their positions. I assume the background is at (0,0) so scaling won't move it but if the foreground image's position isn't scaled along with the size of the pictures it will move during scaling.

    Wednesday, October 1, 2014 7:48 AM

All replies

  • If you divide X by 2 to get x and Y by 2 to get y you will be able to apply the same scale factor to get back to the original dimensions. Basically all is just simple scaling.

    X : scale = x

    Y : scale = y

    A : scale = a

    B : scale = b

    c = p * scale

    d = q * scale

    You'll likely have to apply the same scale factor to the position of the p, q rectangle so it resides at the correct position in the resulting image.

    Tuesday, September 30, 2014 4:31 PM
  • I forgot to say one thing.. The way I'm compressing the image is in relative to the UI element MaxWidth and MaxHeight like this.

    int newWidth;
                int newHeight;
    
                if (originalWidth > maxWidth || originalHeight > maxHeight)
                {
                    var widthratio = (double)maxWidth / originalWidth;
                    var heightratio = (double)maxHeight / originalHeight;
                    var scale = Math.Min(widthratio, heightratio);
    
                    newWidth = (int)(originalWidth * scale);
                    newHeight = (int)(originalHeight * scale);
                }
                else
                {
                    newWidth = (int)originalWidth;
                    newHeight = (int)originalHeight;
                }
    
                return new Windows.Foundation.Size(newWidth, newHeight);


    Clipr : Best ever clipping tool in Windows Phone!
    Download here

    Tuesday, September 30, 2014 4:52 PM
  • Doesn't change the basic concept - you still have a scale factor. Given that you receive your compressed width by multiplying with the scale factor it will be less then 1. So to reverse it you will have to divide by the scale factor (doing the reverse operation of multiplication).
    Tuesday, September 30, 2014 4:55 PM
  • Okay got it! Will try it in my app and post the result here soon.

    Clipr : Best ever clipping tool in Windows Phone!
    Download here

    Tuesday, September 30, 2014 4:59 PM
  • Nope it's not working. I want c & d values in relation to original background rectangle. The scale factor is neither same for XY & AB nor xy & ab.

    I'll give you the detailed values from my app..tell me how we get c & d.

    X = 500, Y = 300;

    A = 200, B = 450;

    x = 150, y = 90;

    p = 31.11, q = 70;

    c = ?, d = ?;


    Clipr : Best ever clipping tool in Windows Phone!
    Download here

    Tuesday, September 30, 2014 7:23 PM
  • If you don't rescale with the same scale factor than you will have to rescale c & d by the factor you used to get from A & B to a & b but you will likely still have to use the scale factor from X & Y to x & y to get the position correctly after scaling.

    Is there a specific reason why you use different scale factors? Seems to artificially complicate the issue by doing it that way.

    Wednesday, October 1, 2014 7:06 AM
  • As I've mentioned in a method above, I'm reducing the image based on the UI element measurements so that user will at least have clear picture while manipulating. The way I'm doing that is like described below

    int newWidth;
    int newHeight;
    
    if (originalWidth > UIElement.Width|| originalHeight > UIElement.Height)
    {
         var widthratio = (double)UIElement.Width / originalWidth;
         var heightratio = (double)UIElement.Height / originalHeight;
         var scale = Math.Min(widthratio, heightratio);
    
         newWidth = (int)(originalWidth * scale);
         newHeight = (int)(originalHeight * scale);
    }
    else
    {
         newWidth = (int)originalWidth;
         newHeight = (int)originalHeight;
    }
    
    return new Windows.Foundation.Size(newWidth, newHeight);
    So the scale depends on the original image and output element measurements.

    I'm bit confused with your explanation, can you give me out a formula ?


    Clipr : Best ever clipping tool in Windows Phone!
    Download here

    Wednesday, October 1, 2014 7:30 AM
  • Looking at your code you are using a uniform scale factor (it doesn't matter in any way how you got to what scaling factor to use). If you use one scale factor to downscale you will have to use the same scale factor for upscaling.

    It's however not sufficient to just scale the size of the separate images but you will also have to scale their positions. I assume the background is at (0,0) so scaling won't move it but if the foreground image's position isn't scaled along with the size of the pictures it will move during scaling.

    Wednesday, October 1, 2014 7:48 AM