1 / 11

# 计算机图形学-有效边表算法源代码(共11页).docx

#include &lt;&gt;
#utDisplayFunc(Display);
Initial();
glutMainLoop();
return 0;
}
//比较2个点的高度
int compare(Point p1, Point p2)
{
if ( &gt; )
return 1;
else if ( == )
return 0;
return -1;
}

//由点数组生成线段数组
Line* create_lines(Point points[], int n)
{
Line *lines = (Line*)malloc(n * sizeof(Line));
for (int i = 0; i &lt; n; ++i)
{
Point p1 = points[i];
Point p2 = points[(i + 1) % n];
int result = compare(p1, p2);
if (result == 0)
lines[i].is_active = 0;
else
lines[i].is_active = 1;
lines[i].high_point = result &gt; 0 ? p1 : p2;
lines[i].low_point = result &lt; 0 ? p1 : p2;
lines[i].inverse_k = (double)( - ) / (double)( - );
}
return lines;
}
//获取线数组中最低的端点
Point get_lowest_point(Line lines[], int n)
{
Point lowest_point = lines[0].low_point;
for (int i = 1; i &lt; n; ++i)
{
Point low_point = lines[i].low_point;
if (compare(lowest_point, low_point) &gt; 0)
lowest_point = low_point;
}
return lowest_point;
}
//获取线数组中最高的端点
Point get_highest_point(Line lines[], int n)
{
Point highest_point = lines[0].high_point;

for (int i = 1; i &lt; n; ++i)
{
Point high_point = lines[i].high_point;
if (compare(highest_point, high_point) &lt; 0)
highest_point = high_point;
}
return highest_point;
}
//交换2个Line对象
void swap(Line &amp;l1, Line &amp;l2)
{
Line temp = l1;
l1 = l2;
l2 = temp;
}
//对线数组进行排序
void sort(Line lines[], int n)
{
//先按低端点的y坐标进行升序排序
for (int i = 0; i &lt; n; ++i)
{
int min_index = i;
for (int j = i + 1;