0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

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

Last updated at Posted at 2020-01-03

概要

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

練習問題

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

参考にしたページ

写真

image.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());
	}
}





以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?