• Create Account

## Maximum frame size based on texture size and frame count

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

607
Like
0Likes
Like

Posted 12 February 2013 - 05:55 PM

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.

607
Like
0Likes
Like

Posted 13 February 2013 - 11:36 AM

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?

607
Like
0Likes
Like

Posted 13 February 2013 - 01:35 PM

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);
}

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.