功能:有效边表转换算法 参数说明: vertices2---顶点列表 VertexNum ---顶点数目备注: DrawPixelint x int y --绘制像素点x yvoid CPolygon_ConversionViewActive_Edge_Table_Conersionint Vertices2 int VertexNum return;补全代码vector类
void CPolygon_ConversionView::Active_Edge_Table_Conersion(int Vertices[][2], int VertexNum)
{
//将顶点列表存入vector中
vector
//求出多边形的最大最小Y值
int minY = INT_MAX, maxY = INT_MIN;
for (int i = 0; i < VertexNum; i++)
{
if (Vertices[i][1] < minY)
minY = Vertices[i][1];
if (Vertices[i][1] > maxY)
maxY = Vertices[i][1];
}
//初始化AET和ET
vector<Edge> AET, ET;
for (int i = 0; i < VertexNum; i++)
{
Edge edge;
edge.yMax = VertexList[i].y;
int nextIndex = (i + 1) % VertexNum;
//确定边的上端点和下端点
if (VertexList[i].y < VertexList[nextIndex].y)
{
edge.x = VertexList[i].x;
edge.yMin = VertexList[i].y;
edge.k = (float)(VertexList[nextIndex].x - VertexList[i].x) / (VertexList[nextIndex].y - VertexList[i].y);
}
else
{
edge.x = VertexList[nextIndex].x;
edge.yMin = VertexList[nextIndex].y;
edge.k = (float)(VertexList[i].x - VertexList[nextIndex].x) / (VertexList[i].y - VertexList[nextIndex].y);
}
ET.push_back(edge);
}
//从y= minY开始扫描
for (int y = minY; y <= maxY; y++)
{
//将ET中yMin值为y的边添加到AET中
for (auto it = ET.begin(); it != ET.end();)
{
if (it->yMin == y)
{
AET.push_back(*it);
it = ET.erase(it);
}
else
{
it++;
}
}
//按照x值从小到大对AET中的边排序
sort(AET.begin(), AET.end(), [](const Edge& a, const Edge& b) { return a.x < b.x; });
//处理完全在当前扫描线上的边
for (auto it = AET.begin(); it != AET.end();)
{
if (it->yMax == y)
{
it = AET.erase(it);
}
else
{
it++;
}
}
//绘制当前扫描线与AET中边的交点
for (int i = 0; i < AET.size(); i += 2)
{
int xStart = (int)(AET[i].x + 0.5);
int xEnd = (int)(AET[i + 1].x + 0.5);
for (int x = xStart; x <= xEnd; x++)
{
DrawPixel(x, y);
}
}
//更新AET中边的x值
for (auto& edge : AET)
{
edge.x += edge.k;
}
}
}
原文地址: http://www.cveoy.top/t/topic/bd6F 著作权归作者所有。请勿转载和采集!