.net 用笔(不同颜色)循环绘制线

发布于 2024-12-20 09:55:03 字数 341 浏览 3 评论 0原文

我想用不同的颜色画线(大约100条)。这些线条将画成一个循环并获得随机颜色。这是我的代码:

for( int i = 0; i < 100 < i++ )
{
srand( time(NULL) );
int index = rand() % 99;
Pen^  my_pen = gcnew Pen((Color)CustomColorTables[index]);
g->drawLine(my_pen,startPointAray[i],stopPointArray[i]);
}

但它用相同的颜色绘制所有线条???

注意:我检查了随机值,生成随机值没有问题。

I want to draw lines ( about 100 ) with different colors. The lines will draw in a loop and get random colors. Here is my code:

for( int i = 0; i < 100 < i++ )
{
srand( time(NULL) );
int index = rand() % 99;
Pen^  my_pen = gcnew Pen((Color)CustomColorTables[index]);
g->drawLine(my_pen,startPointAray[i],stopPointArray[i]);
}

But it draws all lines with the same color???

Note: I checked the random values, there is no problem about generating random values.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

因为看清所以看轻 2024-12-27 09:55:03

尝试放置以下行:

srand( time(NULL) );

BEFORE 进入 for 循环。
在您的情况下,您每次都会重置伪随机序列,并且您可能会获得错误的序列。
然后使用:

int index = (100*rand()) % 99;

因为 rand() 本身返回一个从 0 到 1 的数字,并且您将始终收到 99 作为 % 结果。

Try to place the line:

srand( time(NULL) );

BEFORE entering the for loop.
In your case you are resetting each time the pseudo random sequence and you probably obtain a wrong sequence.
Then use:

int index = (100*rand()) % 99;

because rand() by itself returns a number from 0 to 1 and you will always receive 99 as a % result.

北方的巷 2024-12-27 09:55:03

尝试将:替换

CustomColorTables[index];

为:

CustomColorTables[i];

如果我相信它比以前更好,问题在于您生成和使用随机值索引的方式。

try to replace:

CustomColorTables[index];

with:

CustomColorTables[i];

if it works better than before as I believe, the issue is in the way you generate and use the random value index.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文