using System;
public class VideoRotationCalculator
{
public static (int x, int y, int w, int h) CalculateRotatedVideoParams(
int videoX, int videoY, int videoW, int videoH,
int windowX, int windowY, int windowW, int windowH,
int screenW, int screenH)
{
// 1. 회전 후 사이즈 계산
int rotatedW = videoH;
int rotatedH = videoW;
// 2. 화면 크기 초과 여부 확인 및 조정
if (rotatedW > screenW)
{
rotatedW = screenW;
rotatedH = (int)((double)rotatedW * videoH / videoW);
}
if (rotatedH > screenH)
{
rotatedH = screenH;
rotatedW = (int)((double)rotatedH * videoW / videoH);
}
// 3. 윈도우 중심점 계산
double centerX = windowX + windowW / 2.0;
double centerY = windowY + windowH / 2.0;
// 4. 회전 후 위치 계산 (윈도우 중심 기준)
double rotatedX = centerX - rotatedW / 2.0;
double rotatedY = centerY - rotatedH / 2.0;
// 5. 화면 벗어나는 경우 보정
rotatedX = Math.Max(0, rotatedX);
rotatedY = Math.Max(0, rotatedY);
rotatedW = Math.Min(rotatedW, (int)(screenW - rotatedX));
rotatedH = Math.Min(rotatedH, (int)(screenH - rotatedY));
// 6. 비디오 기준으로 x, y, w, h 계산 (border 고려)
int borderX = (windowW - videoW) / 2;
int borderY = (windowH - videoH) / 2;
int finalX = (int)rotatedX - borderX;
int finalY = (int)rotatedY - borderY;
int finalW = rotatedW + 2 * borderX;
int finalH = rotatedH + 2 * borderY;
// 7. 최종 화면 벗어나는 경우 보정 (border 포함, border 제외 크기로 조정)
int availableWidth = screenW - 2 * borderX;
int availableHeight = screenH - 2 * borderY;
if (finalW > availableWidth)
{
finalW = availableWidth;
}
if (finalH > availableHeight)
{
finalH = availableHeight;
}
return (finalX, finalY, finalW, finalH);
}
public static void Main(string[] args)
{
// 예시 사용
int videoX = 100;
int videoY = 100;
int videoW = 1600;
int videoH = 900;
int windowX = 200;
int windowY = 200;
int windowW = 1920;
int windowH = 1080;
int screenW = 1920;
int screenH = 1080;
(int x, int y, int w, int h) = CalculateRotatedVideoParams(videoX, videoY, videoW, videoH, windowX, windowY, windowW, windowH, screenW, screenH);
Console.WriteLine($"Rotated Video: x={x}, y={y}, w={w}, h={h}");
// 다른 예시
videoW = 640;
videoH = 480;
(x, y, w, h) = CalculateRotatedVideoParams(videoX, videoY, videoW, videoH, windowX, windowY, windowW, windowH, screenW, screenH);
Console.WriteLine($"Rotated Video: x={x}, y={y}, w={w}, h={h}");
}
}
변경 사항:
availableWidth 및 availableHeight 계산: 화면 크기에서 좌우 border 크기(2 * borderX)와 위아래 border 크기(2 * borderY)를 뺀 값을 계산하여, border를 제외한 사용 가능한 화면 크기를 구합니다.