有两种类型的电视:传统的宽高比为4:3,宽屏的电视为16:9.我正在尝试编写一个函数,给出16:9电视的对角线给出4:3电视的对角线,其高度相等.我知道你可以使用毕达哥拉斯定理来解决这个问题,如果我知道其中两个边,但我只知道对角线和比率.
我写了一个通过猜测工作的函数,但我想知道是否有更好的方法.
我到目前为止的尝试:
// C#
public static void Main()
{
/*
* h = height
* w = width
* d = diagonal
*/
const double maxGuess = 40.0;
const double accuracy = 0.0001;
const double target = 21.5;
double ratio4by3 = 4.0 / 3.0;
double ratio16by9 = 16.0 / 9.0;
for (double h = 1; h < maxGuess; h += accuracy)
{
double w = h * ratio16by9;
double d = Math.Sqrt(Math.Pow(h, 2.0) + Math.Pow(w, 2.0));
if (d >= target)
{
double h1 = h;
double w1 = h1 * ratio4by3;
double d1 = Math.Sqrt(Math.Pow(h1, 2.0) + Math.Pow(w1, 2.0));
Console.WriteLine(" 4:3 Width: {0:0.00} Height: {1:00} Diag: {2:0.00}", w, h, d);
Console.WriteLine("16:9 Width: {0:0.00} Height: {1:00} Diag: {2:0.00}", w1, h1, d1);
return;
}
}
}
Run Code Online (Sandbox Code Playgroud)
sle*_*ske 10
对角线和比例就足够了:-).
设d为对角线,r为比率:r = w/h.
然后d²=w²+h².
它遵循r²h²+h²=d².那给了你
h²=d²/(r²+ 1)你可以解决:-).