# Include <iostream>
# Include <math. h>
Using namespace STD;
// Input the vertex (two-dimensional) sequence of the polygon to construct the optimal triangle division. The so-called optimal triangle division refers to the convex polygon of N vertices, which is connected to non-adjacent vertices from any vertex.
// Can constitute a N-2 triangle, n vertices can constitute N (n-2) Triangle
// The weight of the side of the triangle is the Euclidean distance of the two adjacent points on the side, find the N-2 group with the smallest weight of the N (n-2) Triangle
Const double max = 1000000;
Double distance (double ** P, int I, Int J) // calculates the Euclidean distance of vertex I and j.
{
Return POW (P [I] [0]-P [J] [0], 2) + POW (P [I] [1]-P [J] [1], 2 );
}
Void optimaltrangledivision (double ** P, int N, Int & besti) // find the optimal triangle.
{
Double sum; // The sum of Euclidean distance between non-adjacent vertices
Double min = max;
Int I, J;
Int CNT; // used for counting, n vertices can have n-3 strip diagonal lines
For (I = 0; I <n; I ++)
{
Sum = 0;
For (CNT = 0, j = (I + 2) % N; CNT <n-3; j = (J + 1) % N, CNT ++)
{
Sum + = distance (p, I, j );
If (sum <min)
{
Min = sum;
Besti = I;
}
}
}
}
Void main ()
{
Int N;
Cout <"Number of vertices in the input polygon N :";
Cin> N;
Double ** P = new double * [2]; // The vertex array of the polygon.
Int I;
For (I = 0; I <n; I ++) P [I] = new double [2];
For (I = 0; I <n; I ++) // initialize the vertex Array
{
Cout <"Enter the abscissa of vertex <I <:";
Cin> P [I] [0];
Cout <"Enter the column coordinates of vertex <I <:";
Cin> P [I] [1];
}
Int besti;
Optimaltrangledivision (p, N, besti );
Int J, CNT;
Cout <"the optimal triangle division is:" <Endl;
For (CNT = 0, j = (besti + 2) % N; CNT <n-3; CNT ++, j = (J + 1) % N)
{
Cout <"from vertex" <besti <"to vertex" <j <Endl;
}
}