.net 用笔(不同颜色)循环绘制线
我想用不同的颜色画线(大约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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试放置以下行:
BEFORE 进入 for 循环。
在您的情况下,您每次都会重置伪随机序列,并且您可能会获得错误的序列。
然后使用:
因为 rand() 本身返回一个从 0 到 1 的数字,并且您将始终收到 99 作为 % 结果。
Try to place the line:
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:
because rand() by itself returns a number from 0 to 1 and you will always receive 99 as a % result.
尝试将:替换
为:
如果我相信它比以前更好,问题在于您生成和使用随机值索引的方式。
try to replace:
with:
if it works better than before as I believe, the issue is in the way you generate and use the random value index.