1 计算公式
已知
△
A
B
C
\triangle ABC
△ABC 的三边
a
,
b
,
c
a,b,c
a,b,c 和面积
S
S
S,则三角形内接圆的半径为:
r
=
2
S
a
+
b
+
c
r=\cfrac{2S}{a+b+c}
r=a+b+c2S?
2 实现代码
给定三角形的三个顶点坐标,返回该三角形内切圆半径。
#include <iostream>
#include <pcl\io\pcd_io.h>
using namespace std;
typedef pcl::PointXYZ PointT;
double getIncircleRadius(const PointT &pa, const PointT &pb, const PointT &pc)
{
Eigen::Vector4f p1(pa.x, pa.y, pa.z, 0);
Eigen::Vector4f p2(pb.x, pb.y, pb.z, 0);
Eigen::Vector4f p3(pc.x, pc.y, pc.z, 0);
double p2p1, p3p2, p1p3;
p2p1 = (p2 - p1).norm();
p3p2 = (p3 - p2).norm();
p1p3 = (p1 - p3).norm();
double semiperimeter;
semiperimeter = (p2p1 + p3p2 + p1p3) / 2.0;
double area;
area = sqrt(semiperimeter * (semiperimeter - p2p1) * (semiperimeter - p3p2) * (semiperimeter - p1p3));
double radius;
radius = 2 * area / (p2p1 + p3p2 + p1p3);
return radius;
}
int main()
{
PointT p1, p2, p3;
p1.x = 0;
p1.y = 0;
p1.z = 0;
p2.x = 10;
p2.y = 0;
p2.z = 0;
p3.x = 5;
p3.y = 10;
p3.z = 10;
double radius;
radius = getIncircleRadius(p1, p2, p3);
cout << "->三角形内接圆半径为:" << radius << endl;
return 0;
}
3 结果展示
->三角形内接圆半径为:3.53553
相关链接:
PCL:计算三角形外接圆半径
|