Optimal triangle partitioning

Source: Internet
Author: User

 

# 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;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.