cscの作法 その21 マンデルブロ集合

#概要

cscの作法、調べてみた。
練習問題やってみた。

#練習問題

マンデルブロ集合を表示せよ。

#参考にしたページ

http://ou812.web.fc2.com/CsTips/AnalysisMandelbrot.html

#写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/1a3f3219-965a-2832-6865-130e317eb395.png)


#サンプルコード


```
using System;
using System.Windows.Forms;
using System.Drawing;

class form1: Form {
	form1() {
		Text = "mandel";
		ClientSize = new Size(500, 500);
	}
	protected override void OnPaint(PaintEventArgs e) {
		Graphics g = e.Graphics;
		Complex c;
		Complex z;
		int nCount;
		int nCountMax = 64;
		int nWidth = 500;
		int nHeight = 500;
		Pen pen = new Pen(Color.Red);
		for (int x = 0; x < nWidth; x++)
		{
			for (int y = 0; y < nHeight; y++)
			{
				c = new Complex(-this.Width / 2 + x, this.Height / 2 - y);
				c *= 0.00625;
				nCount = 0;
				z = new Complex();
				while(true)
				{
					z = z * z + c;
					if (z.GetSquareModulus() >= 4.0) break;
					nCount++;
					if (nCount > nCountMax)
					{
						nCount = -1;
						break;
					}
				}
				pen.Color = GetColor(nCount);
				g.DrawLine(pen, x, y, x, y + 1);
			}
		}
		pen.Dispose();
		base.OnPaint(e);
	}
	private Color GetColor(int nCount) {
		int d,
			r,
			g,
			b;
		if (nCount < 0) return Color.Black;
		d = nCount % 16;
		d *= 256 / 16;
		int m = (int) (d / 42.667);
		switch(m)
		{
		case 0:
			r = 0;
			g = 6 * d;
			b = 255;
		break;
		case 1:
			r = 0;
			g = 255;
			b = 255 - 6 * (d - 43);
		break;
		case 2:
			r = 6 * (d - 86);
			g = 255;
			b = 0;
		break;
		case 3:
			r = 255;
			g = 255 - 6 * (d - 129);
			b = 0;
		break;
		case 4:
			r = 255;
			g = 0;
			b = 6 * (d - 171);
		break;
		case 5:
			r = 255 - 6 * (d - 214);
			g = 0;
			b = 255;
		break;
		default:
			r = 0;
			g = 0;
			b = 0;
		break;
		}
		return Color.FromArgb(r, g, b);
	}
	class Complex {
	public double Real {
		get
		{
			return _dReal;
		}
		set
		{
			_dReal = value;
		}
	}
	public double Imaginary {
		get
		{
			return _dImaginary;
		}
		set
		{
			_dImaginary = value;
		}
	}
	private double _dReal;
	private double _dImaginary;
	public Complex() {
		_dReal = 0.0;
		_dImaginary = 0.0;
	}
	public Complex(double dReal, double dImaginary) {
		_dReal = dReal;
		_dImaginary = dImaginary;
	}
	public virtual Complex Clone() {
		return (Complex)this.MemberwiseClone();
	}
	public override int GetHashCode() {
		return (_dReal.GetHashCode() ^ _dImaginary.GetHashCode());
	}
	public override string ToString() {
		if (_dImaginary >= 0)
		{
			return "(" + _dReal.ToString() + ", i" + _dImaginary.ToString() + ")";
		}
		else
		{
			return "(" + _dReal.ToString() + ", -i" + ((-1) * _dImaginary).ToString() + ")";
		}
	}
	public override bool Equals(object o) {
		if (o is Complex)
		{
			Complex c = (Complex)o;
			return (this == c);
		}
		return false;
	}
	public double GetArgument() {
		return (double)Math.Atan2(_dReal, _dImaginary);
	}
	public Complex GetConjugate() {
		return new Complex(_dReal, - _dImaginary);
	}
	public Complex GetSquare() {
		return new Complex(_dReal * _dReal - _dImaginary * _dImaginary, 2 * _dReal * _dImaginary);
	}
	public double GetModulus() {
		return Math.Sqrt(GetSquareModulus());
	}
	public double GetSquareModulus() {
		return _dReal * _dReal + _dImaginary * _dImaginary;
	}
	public Complex GetNormalize() {
		double dModulus = this.GetModulus();
		if (dModulus == 0)
		{
			return null;
		}
		return new Complex(_dReal / dModulus, _dImaginary / dModulus);
	}
	public bool Normalize() {
		double dModulus = this.GetModulus();
		if (dModulus == 0)
		{
			return false;
		}
		_dReal /= dModulus;
		_dImaginary /= dModulus;
		return true;
	}
	public static Complex operator + (Complex dat1, Complex dat2) {
		Complex ret = (Complex) dat1.Clone();
		ret.Real += dat2.Real;
		ret.Imaginary += dat2.Imaginary;
		return ret;
	}
	public static Complex operator + (Complex dat1, double dat2) {
		Complex ret = (Complex) dat1.Clone();
		ret.Real += dat2;
		ret.Imaginary += dat2;
		return ret;
	}
	public static Complex operator - (Complex dat1, Complex dat2) {
		Complex ret = (Complex) dat1.Clone();
		ret.Real -= dat2.Real;
		ret.Imaginary -= dat2.Imaginary;
		return ret;
	}
	public static Complex operator - (Complex dat1, double dat2) {
		Complex ret = (Complex) dat1.Clone();
		ret.Real -= dat2;
		ret.Imaginary -= dat2;
		return ret;
	}
	public static Complex operator * (Complex dat1, Complex dat2) {
		double dReal = dat1.Real * dat2.Real - dat1.Imaginary * dat2.Imaginary;
		double dImaginary = dat1.Real * dat2.Imaginary + dat1.Imaginary * dat2.Real;
		return new Complex(dReal, dImaginary);
	}
	public static Complex operator * (Complex dat1, double dat2) {
		Complex ret = (Complex) dat1.Clone();
		ret.Real *= dat2;
		ret.Imaginary *= dat2;
		return ret;
	}
	public static Complex operator / (Complex dat1, double dat2) {
		Complex ret = (Complex) dat1.Clone();
		ret.Real /= dat2;
		ret.Imaginary /= dat2;
		return ret;
	}
	public static bool operator == (Complex dat1, Complex dat2) {
		return (dat1.Real == dat2.Real) && (dat1.Imaginary == dat2.Imaginary);
	}
	public static bool operator !=(Complex dat1, Complex dat2) {
		return !(dat1 == dat2);
	}
	}
	[STAThread]
	public static void Main() {
		Application.Run(new form1());
	}
}





```

以上。