用戶輸入第n個點。我需要檢查多邊形是否存在,然後確定類型 - 凹面或凸面多邊形。我知道如果每個角度都在180度以下,多邊形就是凸的。所以問題歸結爲尋找多邊形的每個內角。我一直在尋找公式或算法,但沒有成功。如何確定多邊形的類型
實施例:
輸入:N = 4;
POINT1:(5; 6)
POINT2:(4; -5)
點3:(-5; 4)
Point4:(1-5; 5)
預期輸出:多邊形是凸
這是到目前爲止的代碼:日現在它只能找到飛機中各點之間的最大和最小距離。
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
double a[15][2];
int n;
cin >> n;
if (n <= 0 && n > 15)
return 1;
for (int i = 0; i < n; i++)
{
cout << "x" << i << " = , y" << i << " = ";
cin >> a[i][0] >> a[i][1];
}
double maxDistance = 0.0;
double minDistance = 0.0;
double maxpoint1[2];
double maxpoint2[2];
double minpoint1[2];
double minpoint2[2];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (j != i)
{
double x1 = a[i][0];
double x2 = a[j][0];
double y1 = a[i][1];
double y2 = a[j][1];
double currentDistance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
if (currentDistance > maxDistance)
{
maxDistance = currentDistance;
maxpoint1[0] = x1;
maxpoint1[1] = y1;
maxpoint2[0] = x2;
maxpoint2[1] = y2;
}
if (minDistance > currentDistance)
{
currentDistance = minDistance;
minpoint1[0] = x1;
minpoint1[1] = y1;
minpoint2[0] = x2;
minpoint2[1] = y2;
}
cout << "x1 = " << x1 << " y1 = " << y1 << " x2 = " << x2 << " y2 = " << y2;
cout << endl << "Distance is " << currentDistance;
cout << endl;
}
}
}
cout << "The max distance is: " << maxDistance << " between x1 = " << maxpoint1[0] << " y1 = " << maxpoint1[1] << " and x2 = " << maxpoint2[0] << " y2 = " << maxpoint2[1];
cout << "The min distance is: " << minDistance << " between x1 = " << minpoint1[0] << " y1 = " << minpoint1[1] << " and x2 = " << minpoint2[0] << " y2 = " << minpoint2[1];
return 0;
}