关于那个脑袋的很漂漂的图形的C#版本

在这篇文章里,脑袋给我们画了一个很漂漂的图形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 ) );
    }

  }

}
 
最后附上高清大图一张:

1 

补充高清局部图一张:

1 (2)

 

我用的小本性能有限,实在不忍心再虐待了,各位大大自己测测性能吧(好像比我想象中的好点点)。

 

代码更新:

1、改用SetPixel了,大图性能会好很多(因为我本来没打算画大图)。。。。要更快需要用非常手段,记得园子里以前有。

2、提出了ComposeColor的过程,现在的计算时间更精确。

转载于:https://www.cnblogs.com/Ivony/archive/2009/11/24/1609760.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值