• Advertisement
Sign in to follow this  

Maximum frame size based on texture size and frame count

This topic is 1805 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

By frame I mean a frame of animation in a texture kinda like a sprite sheet.

 

I need to write a method (in C#) that takes a texture size and the number of frames required and returns the maximum size of a frame (let's assume for now the frame is a square).

 

 

ie.

 

public static Size GetMaxFrameSize(Size textureSize, int frameCount)
{
    ...
}

 

So say I have a texture size of 1024 x 1024 and I have 64 frames of animation the routine would return 128 x 128.

 

Another routine that I need is one that does the same thing but also takes into account the frame size. So it returns the maximum frame size based on the frame size but maintains the aspect ratio of the frame.

 

ie.

 

 

public static Size GetMaxFrameSize(Size textureSize, Size frameSize, int frameCount)
{
    ...
}

 

So in other words I need to be able to fit the required number of animation frames into a given texture size but if the frames wont fit I need to calculate the maximum size so they will fit.

Share this post


Link to post
Share on other sites
Advertisement

Has anyone tackled this problem before or know where I can find an algorithm for solving such a problem? Do I need to provide more info?

Share this post


Link to post
Share on other sites

I believe I've solved the problem.

 

public static Size FixAspectRatio(Size originalSize, Size newSize)
{
    return FixAspectRatio(new SizeF(originalSize.Width, originalSize.Height), new SizeF(newSize.Width, newSize.Height)).ToSize();
}
 
public static SizeF FixAspectRatio(SizeF originalSize, SizeF newSize)
{
    if (originalSize.Width > originalSize.Height)
    {
        float aspectRatio = originalSize.Height / originalSize.Width;
        newSize.Height = newSize.Width * aspectRatio;
    }
    else
    {
        float aspectRatio = originalSize.Width / originalSize.Height;
        newSize.Width = newSize.Height * aspectRatio;
    }
 
    return newSize;
}
 
public static Size GetMaxFrameSize(Size textureSize, int frameCount)
{
    if (frameCount == 0)
        return Size.Empty;
 
    int dimension = Math.Min(textureSize.Width, textureSize.Height);
    double square = Math.Ceiling(Math.Sqrt(frameCount));
    int size = (int)Math.Floor(dimension / square);
 
    return new Size(size, size);
}
 
public static Size GetMaxFrameSize(Size textureSize, Size frameSize, int frameCount)
{
    Size maxSize = GetMaxFrameSize(textureSize, frameCount);
    return FixAspectRatio(frameSize, maxSize);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement