Search code examples
c++openglpixelformularectangles

Scaling to match the size


I need a formula to scale a rectangle to fit into a bigger/wider rectangle. I only need to worry on the small rectangle.

The given values only I have are:

Big rectangle:

  1. width
  2. height (I don't think this is needed)
  3. point (i.e x,y)

Small rectangle:

  1. width (not really read-only, but still depends)
  2. height (read only)
  3. scale (I need a formula to compute what value would this be)
  4. point

Values are relative to screen pixels.

enter image description here


Solution

  • Find

    a = width1 / height1;
    b = width2 / height2;
    
    if(a>b)
    {
      scale = height1 / height2;
      point.y = y; (from big rectangle)
      point.x = (width1 - width2 * scale) / 2 + x;
    }
    else
    {
      scale = width1 / width2;
      point.x = x; (from big rectangle)
      point.y = (height1 - height2 * scale) / 2 + y;
    }
    

    From what I understand, this should do what you wanted.

    Edit: See PureW answer for getting the scale only.