需要找到构成最大周长的 3 个点

Need to find 3 points that make up the largest perimeter

本文关键字:周长 个点      更新时间:2023-10-16

我有一个叫做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; 
}