关于那个脑袋的很漂漂的图形的C#版本
2009-11-24 18:11 Ivony... 阅读(5289) 评论(30) 编辑 收藏 举报在这篇文章里,脑袋给我们画了一个很漂漂的图形http://www.cnblogs.com/Ninputer/archive/2009/11/24/1609364.html。
由于脑袋已经凌驾于语言之上,写出来的东东不利于C#er消化吸收,经过0.n小时的努力,完成了这个图形的C#版本。
因为这个玩意儿是典型的并行计算的场景,所以以下代码使用了.NET Framework 4.0里面的并行库(哦还有复数类),已在VS2010下编译运行通过。大家可以自己调节参数玩。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Numerics;
using System.IO;
using System.Drawing.Imaging;
using System.Diagnostics;
namespace NinputerExam1
{
class Program
{
static void Main( string[] args )
{
Size size = new Size( 4096, 4096 );
int[] map = new int[size.Width * size.Height];
var points = from x in Enumerable.Range( 0, size.Width ) from y in Enumerable.Range( 0, size.Height ) select new Point( x, y );
Stopwatch watch = new Stopwatch();
watch.Start();
Console.WriteLine( "begin Calculate..." );
points.AsParallel().ForAll( point => map[point.X * size.Height + point.Y] = Calculate( point, size ) );
Console.WriteLine( "Calculate finished... Elapsed: {0}", watch.Elapsed );
watch.Restart();
var colorMap = Array.ConvertAll( map, value => ComposeColor( value ) );
Console.WriteLine( "begin Drawing" );
using ( var bitmap = new Bitmap( size.Width, size.Height, PixelFormat.Format32bppRgb ) )
{
for ( int x = 0; x < size.Width; x++ )
{
for ( int y = 0; y < size.Height; y++ )
{
bitmap.SetPixel( x, y, colorMap[x * size.Height + y] );
}
}
using ( var stream = new FileStream( @"c:\Temp\1.png", FileMode.Create, FileAccess.Write ) )
{
bitmap.Save( stream, ImageFormat.Png );
}
}
Console.WriteLine( "Drawing finished... Elapsed: {0}", watch.Elapsed );
Console.ReadLine();
}
private const int maxIter = 4096;
public static int Calculate( Point point, Size size )
{
Complex c = new Complex( (double) point.X / size.Width * 4 - 2, (double) point.Y / size.Height * 4 - 2 );
Complex z = 0;
int count = 0;
do
{
z = z * z + c;
count++;
}
while ( z.Magnitude < 4 && count < maxIter );
return count;
}
public static Color ComposeColor( int value )
{
double _value;
if ( value == 0 )
_value = 0;
else
_value = Math.Log( value ) / Math.Log( maxIter );
return Color.FromArgb( 0xFF, 0, (int) ( Math.Sin( _value * Math.PI ) * 0xFF * 0.75 ), (int) ( ( Math.Sin( _value * Math.PI ) ) * 0xFF ) );
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Numerics;
using System.IO;
using System.Drawing.Imaging;
using System.Diagnostics;
namespace NinputerExam1
{
class Program
{
static void Main( string[] args )
{
Size size = new Size( 4096, 4096 );
int[] map = new int[size.Width * size.Height];
var points = from x in Enumerable.Range( 0, size.Width ) from y in Enumerable.Range( 0, size.Height ) select new Point( x, y );
Stopwatch watch = new Stopwatch();
watch.Start();
Console.WriteLine( "begin Calculate..." );
points.AsParallel().ForAll( point => map[point.X * size.Height + point.Y] = Calculate( point, size ) );
Console.WriteLine( "Calculate finished... Elapsed: {0}", watch.Elapsed );
watch.Restart();
var colorMap = Array.ConvertAll( map, value => ComposeColor( value ) );
Console.WriteLine( "begin Drawing" );
using ( var bitmap = new Bitmap( size.Width, size.Height, PixelFormat.Format32bppRgb ) )
{
for ( int x = 0; x < size.Width; x++ )
{
for ( int y = 0; y < size.Height; y++ )
{
bitmap.SetPixel( x, y, colorMap[x * size.Height + y] );
}
}
using ( var stream = new FileStream( @"c:\Temp\1.png", FileMode.Create, FileAccess.Write ) )
{
bitmap.Save( stream, ImageFormat.Png );
}
}
Console.WriteLine( "Drawing finished... Elapsed: {0}", watch.Elapsed );
Console.ReadLine();
}
private const int maxIter = 4096;
public static int Calculate( Point point, Size size )
{
Complex c = new Complex( (double) point.X / size.Width * 4 - 2, (double) point.Y / size.Height * 4 - 2 );
Complex z = 0;
int count = 0;
do
{
z = z * z + c;
count++;
}
while ( z.Magnitude < 4 && count < maxIter );
return count;
}
public static Color ComposeColor( int value )
{
double _value;
if ( value == 0 )
_value = 0;
else
_value = Math.Log( value ) / Math.Log( maxIter );
return Color.FromArgb( 0xFF, 0, (int) ( Math.Sin( _value * Math.PI ) * 0xFF * 0.75 ), (int) ( ( Math.Sin( _value * Math.PI ) ) * 0xFF ) );
}
}
}
最后附上高清大图一张:
补充高清局部图一张:
我用的小本性能有限,实在不忍心再虐待了,各位大大自己测测性能吧(好像比我想象中的好点点)。
代码更新:
1、改用SetPixel了,大图性能会好很多(因为我本来没打算画大图)。。。。要更快需要用非常手段,记得园子里以前有。
2、提出了ComposeColor的过程,现在的计算时间更精确。