【发布时间】:2012-09-25 19:30:59
【问题描述】:
我正在实现基于扫描线的多边形填充算法。 我知道通用算法,我现在正在尝试用 C++ 实现它。
我需要将边缘表实现为活动边缘列表。
我已将活动边列表设为一个向量,以便于动态插入和删除。
然而,边缘表有点混乱。
我正在尝试使用向量数组来实现边缘表,这些向量将包含我制作的结构。
这是结构。
struct Bucket
{
// Fields of a bucket list
int ymax, x, dx, dy, sum;
};
我有一个 for 循环,它遍历传入的顶点数组,然后它创建一个桶并将桶插入到边表中 ymin 索引处的边表中。
我的问题是我很难遍历边缘表并访问各个存储桶。
这里是桶向量数组的声明:
// This array is the edge table which has a max index of 300
// The window for the program is never more than 300 by 300
vector<Bucket> et[300];
这是我的 for 循环,它在边缘表上进行迭代,试图打印出项目。 我尝试使用迭代器和普通 int 作为索引,但在尝试打印每个存储桶的值时都不起作用。
// Debugging the edge table, prints out all buckets
vector<Bucket>::iterator it;
for(int j = 0; j < 300; j++)
{
for(it = et[j].begin(); it < et[j].end(); it++)
{
// printf(*it);
// printf();
}
for(int q = 0; q < et[j].size(); q++)
{
printf("ymax = %d", q[0]);
}
}
【问题讨论】:
-
在最后一个循环中尝试
for (int q = 0; q < et[j].size(); q++) { printf("et[%d][%d].ymax = %d\n", j, q, et[j][q].ymax); }。 -
@Michael - 谢谢,这行得通。
-
在 for 循环中使用迭代器时,您的比较应使用
!=而不是<。许多类型的迭代器无法与<和>等关系运算符相比。
标签: c++ iterator polygon edge-list scanline