Я хотел бы определить, принадлежит ли исходное разрешение указанному соотношению сторон в С# или VB.NET.
В настоящее время я написал это:
/// --------------------------------------------------------------------------
/// <summary>
/// Determine whether the source resolution belongs to the specified aspect ratio.
/// </summary>
/// --------------------------------------------------------------------------
/// <param name="resolution">
/// The source resolution.
/// </param>
///
/// <param name="aspectRatio">
/// The aspect ratio.
/// </param>
/// --------------------------------------------------------------------------
/// <returns>
/// <see langword="true"/> if the source resolution belongs to the specified aspect ratio;
/// otherwise, <see langword="false"/>.
/// </returns>
/// ----------------------------------------------------------------------------------------------------
public static bool ResolutionIsOfAspectRatio(Size resolution, Point aspectRatio) {
return (resolution.Width % aspectRatio.X == 0) &&
(resolution.Height % aspectRatio.Y == 0);
}
ВБ.NET:
Public Shared Function ResolutionIsOfAspectRatio(resolution As Size,
aspectRatio As Point) As Boolean
Return ((resolution.Width Mod aspectRatio.X) AndAlso
(resolution.Height Mod aspectRatio.Y)) = 0
End Function
Пример использования:
Size resolution = new Size(1920, 1080);
Point aspectRatio = new Point(16, 9);
bool result = ResolutionIsOfAspectRatio(resolution, aspectRatio);
Console.WriteLine(result);
Я просто хочу убедиться, что ничего не упускаю из концепции соотношения сторон, что может привести к неожиданным результатам при использовании написанной мной функции.
Тогда мой вопрос: подходит ли этот алгоритм для всех случаев? Если нет, то какие модификации я должен сделать, чтобы правильно выполнить эту операцию?
РЕДАКТИРОВАТЬ: я заметил, что алгоритм совершенно неверен, он принимает 640x480 как соотношение сторон 16: 9. Я не читал достаточно об основах, как вычислить это.