用qt写一个函数实现:输入三角形三个点,返回一条边长度,这条边长度和其他边长度差异最大,同时返回这条边顶点
时间: 2023-05-11 22:03:02 浏览: 75
c++用函数写输入三边——判断三角形形状并计算面积周长
5星 · 资源好评率100%
非常感谢您的提问!以下是我为您提供的代码:
```cpp
#include <QtMath>
struct Point {
double x;
double y;
};
double distance(Point p1, Point p2) {
double dx = p1.x - p2.x;
double dy = p1.y - p2.y;
return qSqrt(dx * dx + dy * dy);
}
void findMaxDiffEdge(Point p1, Point p2, Point p3, double& maxDiff, Point& maxDiffVertex) {
double d1 = distance(p1, p2);
double d2 = distance(p2, p3);
double d3 = distance(p3, p1);
double diff1 = qAbs(d1 - d2);
double diff2 = qAbs(d2 - d3);
double diff3 = qAbs(d3 - d1);
if (diff1 > diff2 && diff1 > diff3) {
maxDiff = diff1;
maxDiffVertex = p3;
} else if (diff2 > diff1 && diff2 > diff3) {
maxDiff = diff2;
maxDiffVertex = p1;
} else {
maxDiff = diff3;
maxDiffVertex = p2;
}
}
int main() {
Point p1 = {0, 0};
Point p2 = {3, 0};
Point p3 = {0, 4};
double maxDiff;
Point maxDiffVertex;
findMaxDiffEdge(p1, p2, p3, maxDiff, maxDiffVertex);
qDebug() << "Max difference: " << maxDiff << ", Vertex: (" << maxDiffVertex.x << ", " << maxDiffVertex.y << ")";
return 0;
}
```
这个函数使用了 Qt 的数学库 qSqrt() 来计算两点之间的距离。它接受三个点作为参数,计算出三条边的长度,并找出其中长度差异最大的那条边,同时返回这条边的顶点。在这个例子中,我们使用了三个点 (0,0), (3,0) 和 (0,4) 来测试函数。
阅读全文