C#:如何计算宽高比

时间:2022-04-18 04:20:55

I am relatively new to programming. I need to calculate the aspect ratio(16:9 or 4:3) from a given dimension say axb. How can I achieve this using C#. Any help would be deeply appreciated.

我对编程比较陌生。我需要从给定的维度(比如axb)计算纵横比(16:9或4:3)。如何使用C#实现这一目标。任何帮助将深表感谢。

public string AspectRatio(int x, int y)
{
 //code am looking for
 return ratio
}

Thanks.

4 个解决方案

#1


8  

You need to find Greatest Common Divisor, and divide both x and y by it.

你需要找到最大公约数,并用它除x和y。

static int GCD(int a, int b)
{
    int Remainder;

    while( b != 0 )
    {
        Remainder = a % b;
        a = b;
        b = Remainder;
    }

    return a;
}

return string.Format("{0}:{1}",x/GCD(x,y), y/GCD(x,y));

PS

If you want it to handle something like 16:10 (which can be divided by two, 8:5 will be returned using method above) you need to have a table of predefined ((float)x)/y-aspect ratio pairs

如果你想要它处理像16:10(可以除以2,使用上面的方法将返回8:5)的东西你需要有一个预定义((浮点)x)/ y宽高比对的表

#2


5  

Since you only need to decide between 16:9 and 4:3, here's a much simpler solution.

由于您只需要在16:9和4:3之间做出决定,这是一个更简单的解决方案。

public string AspectRatio(int x, int y)
{
    double value = (double)x / y;
    if (value > 1.7)
        return "16:9";
    else
        return "4:3";
}

#3


3  

There're only several standard ratios like: 4:3, 5:4, 16:10, 16:9. GCD is a good idea, but it will fail for at least 16:10 ratios and 1366x768 resolution.

只有几个标准比率,如:4:3,5:4,16:10,16:9。 GCD是一个好主意,但它至少会以16:10的比例和1366x768的分辨率失败。

Pure GCD algorithm will get 683:384 for 1366x768, cause 683 is a prime, while resolution is almost 16:9 (16.0078125).

对于1366x768,纯GCD算法将获得683:384,因为683是素数,而分辨率几乎是16:9(16.0078125)。

I suppose, that for real tasks, one will need to implement rather complicated algorithm:

我想,对于实际任务,需要实现相当复杂的算法:

First try known aspect ratios (look for them at wikipedia), allowing some errors and only then use GCD as fallback.

首先尝试已知的宽高比(在*上查找它们),允许一些错误,然后才使用GCD作为后备。

Don't forget about 32:10 ;-)

不要忘记32:10 ;-)

#4


1  

You need to find the GCD (http://en.wikipedia.org/wiki/Greatest_common_divisor) and then:

你需要找到GCD(http://en.wikipedia.org/wiki/Greatest_common_divisor),然后:

return x/GCD + ":" + y/GCD;

#1


8  

You need to find Greatest Common Divisor, and divide both x and y by it.

你需要找到最大公约数,并用它除x和y。

static int GCD(int a, int b)
{
    int Remainder;

    while( b != 0 )
    {
        Remainder = a % b;
        a = b;
        b = Remainder;
    }

    return a;
}

return string.Format("{0}:{1}",x/GCD(x,y), y/GCD(x,y));

PS

If you want it to handle something like 16:10 (which can be divided by two, 8:5 will be returned using method above) you need to have a table of predefined ((float)x)/y-aspect ratio pairs

如果你想要它处理像16:10(可以除以2,使用上面的方法将返回8:5)的东西你需要有一个预定义((浮点)x)/ y宽高比对的表

#2


5  

Since you only need to decide between 16:9 and 4:3, here's a much simpler solution.

由于您只需要在16:9和4:3之间做出决定,这是一个更简单的解决方案。

public string AspectRatio(int x, int y)
{
    double value = (double)x / y;
    if (value > 1.7)
        return "16:9";
    else
        return "4:3";
}

#3


3  

There're only several standard ratios like: 4:3, 5:4, 16:10, 16:9. GCD is a good idea, but it will fail for at least 16:10 ratios and 1366x768 resolution.

只有几个标准比率,如:4:3,5:4,16:10,16:9。 GCD是一个好主意,但它至少会以16:10的比例和1366x768的分辨率失败。

Pure GCD algorithm will get 683:384 for 1366x768, cause 683 is a prime, while resolution is almost 16:9 (16.0078125).

对于1366x768,纯GCD算法将获得683:384,因为683是素数,而分辨率几乎是16:9(16.0078125)。

I suppose, that for real tasks, one will need to implement rather complicated algorithm:

我想,对于实际任务,需要实现相当复杂的算法:

First try known aspect ratios (look for them at wikipedia), allowing some errors and only then use GCD as fallback.

首先尝试已知的宽高比(在*上查找它们),允许一些错误,然后才使用GCD作为后备。

Don't forget about 32:10 ;-)

不要忘记32:10 ;-)

#4


1  

You need to find the GCD (http://en.wikipedia.org/wiki/Greatest_common_divisor) and then:

你需要找到GCD(http://en.wikipedia.org/wiki/Greatest_common_divisor),然后:

return x/GCD + ":" + y/GCD;