由于老师要求要实现一个填充算法,而每次填充都需要一个源图形【不规则】,用mspaint自己画太麻烦,于是打算自己动手随机生成。

这里用的是 Polygen()函数,但是注意首尾相接,另外,为了保证规则点,所以采用的是按顺序随机生成点,类似时钟上的刻度分布。

void RandomRect(TCanvas *c,int halfvertexs,int weight,int height)
{
RandSeed=Now();
randomize(); TPoint *v;
v = (TPoint*)malloc(sizeof(TPoint) * (halfVertexs * 2 + 1)); c->Pen->Color = clBlack;
//generate the point randomly
for (int i = 0; i < halfVertexs ; i++)
v[i] = Point(random(width / halfVertexs) + width / halfVertexs * i,random(height / 2));
for (int i = halfVertexs; i < halfVertexs * 2; i++)
v[i] = Point(width + random(width / halfVertexs) - ( width / halfVertexs)
* (i - halfVertexs + 1),random(height / 2) + height / 2);
v[halfVertexs * 2] = Point(v[0].x,v[0].y);
for(int i = 0;i < halfVertexs * 2;i++)
c->Pixels[v[i].x][v[i].y] = clBlack;
c->Polygon(v,halfVertexs * 2);
}

  

  这样,一个随机生成不规则图形(仮)的函数就完成了

05-06 21:51