在新课堂上:
<body>
<div class="mycontent">
<div class="topcontent">
<button class="button">Menu</button>
</div>
<div class="othercontent">
<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia
voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.</p>
<p>Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem
ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur?</p>
<p>Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</p>
<p>At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt
mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas
assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et vo</p>
</div>
</div>
</body>
然后在类Initi方法中:
private Random r = new Random();
public int numberOfPoints = 100;
public Bitmap bmpWithPoints;
然后方法GetBitmapWithEllipses:
public void Init()
{
bmpWithPoints = GetBitmapWithEllipses(1.0f);
}
在Form1顶部和构造函数中:
private Bitmap GetBitmapWithEllipses(float radius)
{
Bitmap bmp = new Bitmap(512, 512);
Random r = new Random();
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.Black);
g.SmoothingMode = SmoothingMode.AntiAlias;
List<RectangleF> rects = new List<RectangleF>();
rects.Add(new RectangleF(0, 0, 0, 0));
for (int x = 0; x < numberOfPoints; x++)
{
for (int y = 0; y < numberOfPoints; y++)
{
Color c = Color.FromArgb(
r.Next(0, 256),
r.Next(0, 256),
r.Next(0, 256));
PointF p = new PointF(r.Next(bmp.Width), r.Next(bmp.Height));
RectangleF rect = new RectangleF(p, new SizeF(radius * 2, radius * 2));
if (!rects.Any(tmp => tmp.IntersectsWith(rect)))
{
rects.Add(rect);
g.FillEllipse(new SolidBrush(c), rect);
}
else
{
y--;
}
}
}
}
return bmp;
}
在我遇到问题之前,方法GetBitmapWithEllipses很慢,因为将点数设置为50.现在这部分工作正常,但现在将半径大小更改为5.0f或10.0f时速度非常慢。
当我在行中的Form1中将点数设置为10或20或50时:
DopplerEffect de = new DopplerEffect();
public Form1()
{
InitializeComponent();
de.bmpWithPoints = new Bitmap(512, 512);
de.numberOfPoints = 50;
de.Init();
}
类中的GetBitmapWithEllipses方法可以快速运行。 但是如果我在课堂上改变每个点的半径大小,例如1.0f,它将很快工作。但是,如果我将点数设置为50并将半径大小更改为10.0f,例如方法GetBitmapWithEllipses将非常慢。甚至到5.0f
在更改每个点的半径大小时,有没有办法让它快速工作?
答案 0 :(得分:1)
您在循环中随机创建圆圈,如果新创建的圆圈与之前创建的圆圈重叠,则向后步进循环。
当无线电增长时,重叠的可能性增加,因此您向后退一步并且循环需要太长时间才能完成。事实上,无法保证你的循环完成!
您可以注释检查重叠的代码和使循环向后退的代码,然后您将获得快速结果:
//if (!rects.Any(tmp => tmp.IntersectsWith(rect)))
//{
// rects.Add(rect);
g.FillEllipse(new SolidBrush(c), rect);
//}
//else
//{
// y--;
//}