如何只用斜边和纵横比来计算直角三角形腿的长度?

时间:2018-01-18 16:25:58

标签: c++

程序的目的是告诉您显示器的宽度和高度,因为您知道对角线尺寸和宽高比。这是我到目前为止的代码,但我不确定如何在给定数据时正确计算widthIn和heightIn。

#include "stdafx.h"
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
double width, height, diagonal, widthIn, heightIn, aspectRatio, mathPlaceholder;

//Data Collection
cout << "Enter the width value of the display: ";
cin >> width;

cout << "Enter the height value of the display: ";
cin >> height;

cout << "Enter the length of the diagonal in inches: ";
cin >> diagonal;

//Calculations


//Outputting Results
cout << "\n The width of your display is " << widthIn << " inches." << endl;
cout << "The height of your display is " << heightIn << " inches." << endl;



return 0;
}

1 个答案:

答案 0 :(得分:3)

您可以使用毕达哥拉斯来计算边。

height^2+width^2 = diagonal^2 (1)

因为你知道宽高比,由

给出
ratio = width:height (2)

你可以将(2)代入(1)并获得

height^2 + (ratio*height)^2 = diagonal^2  
(1+ratio^2)*height^2 = diagonal^2  
height = sqrt(diagonal^2/(1+ratio^2))

再次使用(2)

width  = ratio * height