ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมค้นหารัศมีของวงกลมในสามเหลี่ยมที่กำหนด
สำหรับสิ่งนี้ เราจะได้รับด้านของสามเหลี่ยมหนึ่งๆ และหน้าที่ของเราคือหารัศมีของวงกลมในสามเหลี่ยมนั้น
สูตรการหารัศมีของวงกลมคือ
พื้นที่ของสามเหลี่ยม/ครึ่งปริมณฑลของสามเหลี่ยม
ตัวอย่าง
#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