找出三角形的內切圓半徑的 C++ 程式


本教程中,我們將討論一個程式,用於求出給定三角形的內切圓半徑。

為此,我們將獲得特定三角形各邊的資訊,我們的任務是找出該三角形內切圓的半徑。

求出內切圓半徑的公式為:

三角形面積/三角形半周長

示例

#include <bits/stdc++.h>
using namespace std;
//calculating the radius of incircle
float calc_radius(float a, float b, float c) {
   if (a < 0 || b < 0 || c < 0)
      return -1;
   //half perimeter of triangle
   float p = (a + b + c) / 2;      
   //area of triangle
   float area = sqrt(p * (p - a) * (p - b) * (p - c));
   float radius = area / p;
   // Return the radius
   return radius;
}
int main() {
   float a = 4, b = 7, c = 9;
   cout << calc_radius(a, b, c) << endl;
   return 0;
}

輸出

1.34164

更新時間:2019 年 11 月 21 日

193 次瀏覽

開啟你的 職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.