g r i m m w e r k s <[EMAIL PROTECTED]> wrote:
> I've got a bunch of images that I need to scale to fit a maxWidth and
> maxHeight; trouble is these images are all sorts of possible dimensions.
> 
> Say we want to fit a 320x240 area, if I request a rescaling for a rect of
> 640x480, it will of course check for whether the requested rect is landscape
> or portrait, and scale it to meet the 320x240.

Hi g r i m m w e r k s,


on mGetBestFit(aSourceRect, aTargetRect) -----------------------------
  -- INPUT: <aSourceRect> and <aTargetRect> must both be rects
  -- OUTPUT: The largest rect with the same proportions as
  --         <aSourceRect> which fits inside <aTargetRect>
  --------------------------------------------------------------------
  
  tSourceWidth  = float(aSourceRect.width)
  tSourceHeight = float(aSourceRect.height)
  
  tTargetWidth  = aTargetRect.width
  tTargetHeight = aTargetRect.height
  
  -- Determine the scale: greater than 1 indicates that aSourceRect
  -- is larger than aTargetRect
  
  tWidthScale = tSourceWidth / tTargetWidth
  tScale      = tSourceHeight / tTargetHeight
  
  if tScale < tWidthScale then
    -- Width is proportionally greater than height
    tScale = tWidthScale
  end if
  
  -- Scale width and height accordingly
  tWidth   = tSourceWidth / tScale
  tHeight  = tSourceHeight / tScale
  
  -- Place the scaled rect inside aTargetRect
  tOffsetH = aTargetRect.left + (tTargetWidth - tWidth) / 2
  tOffsetV = aTargetRect.top + (tTargetHeight - tHeight) / 2
  
  return rect(tOffsetH, tOffsetV, tWidth+tOffsetH, tHeight+tOffsetV)
end mGetBestFit


Cheers,

James

[To remove yourself from this list, or to change to digest mode, go to 
http://www.penworks.com/lingo-l.cgi  To post messages to the list, email 
[EMAIL PROTECTED]  (Problems, email [EMAIL PROTECTED]). Lingo-L is for 
learning and helping with programming Lingo.  Thanks!]

Reply via email to