需要找到构成最大周长的 3 个点
Need to find 3 points that make up the largest perimeter
我有一个叫做point的结构。显然,它提供了一个点(x和y)的坐标。我有一系列要点。
我需要做的是找到构成最大三角形的 3 个点。我尝试了很多选择,但我没有成功。你能想到一种算法来创造我需要的结果吗?
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
struct point {
double x, y;
};
int main()
{
double n, c1, c2;
double max(0), max1(0), max2(0), temp;
point *a = NULL;
cout << "Enter the number of points: ";
cin >> n;
a = new point[n];
for (int i = 0; i < n; i++) {
cin >> c1 >> c2;
point dot;
dot.x = c1;
dot.y = c2;
a[i] = dot;
};
for (int i = 0; i < n-1; i++) { // here I'm running out of ideas
for (int j = 1; j < n; j++) {
temp = sqrt((a[i].x + a[i].y)*(a[i].x + a[i].y)- (a[j].x + a[j].y)*(a[j].x + a[j].y));
if (temp > max)
}
}
您可以遍历所有三点集。请注意,最好使用向量而不是数组,将计算周长的代码放在单独的函数中很有用
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
struct point {
double x, y;
};
double perim(point p1, point p2, point p3)
{
double result = 0;
result += sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
result += sqrt(pow(p2.x - p3.x, 2) + pow(p2.y - p3.y, 2));
result += sqrt(pow(p3.x - p1.x, 2) + pow(p3.y - p1.y, 2));
return result;
}
int main()
{
double n, c1, c2;
double max(0), temp;
int p1 = 0, p2 = 0, p3 = 0;
vector <point> a;
cout << "Enter the number of points: ";
cin >> n;
for (int i = 0; i < n; i++) {
cin >> c1 >> c2;
point dot;
dot.x = c1;
dot.y = c2;
a.push_back(dot);
};
for (int i = 0; i < n - 2; i++) { // here I'm running out of ideas
for (int j = i+1; j < n - 1; j++) {
for (int k = j+1; k < n; k++) {
temp = perim(a[i], a[j], a[k]);
if (temp > max) {
max = temp;
p1 = i; p2 = j; p3 = k;
}
}
}
}
cout << p1 << " "<<p2<< " "<<p3;
}
相关文章:
- 在不传递参数数量且只有3个点的情况下,如何使用变差函数
- PCL:将两个点云缩放到相同的大小
- 如何从两个点创建矩形?
- 在C++中使用 PCL 在同一窗口中查看多个点云
- 如何校准相机焦距,平移和旋转给定四个点
- 从 3 个视点在 3 张图像上有 3 个点如何在 3D 空间中获取其坐标?
- 代码不会打印回 numSum 作为 numSum = 数组中的 2 个点
- opengl3 2 个点向量的颜色相同
- 如何在无限轴上找到 N 个点,以便从 M 点到其最近的 N 的距离总和最小
- 如何使用灵活的碰撞库在两个点云或点云与机器人末端执行器模型之间进行碰撞检测
- 如何在给定两个点的线上移动对象?
- 具有单个函数的两个点的距离和坡度
- 如何将两个点的向量保留在一个向量中
- 检查三个点是否线性
- 使用三个点(x,y),如何确定它是否是三角形
- 阵列C 中两个点函数之间的距离
- C++试图在给定max/min x/y/z值、要在集合中的n个点和要递增的m个值的情况下生成每一组可能的点
- 需要找到构成最大周长的 3 个点
- 如何在路径规格中工作两个点
- 使用指针和结构C 计算两个点之间的距离,分割故障问题