// ■反復関数系によるシダの葉の描画 // button1を貼り付けbutton1.Text="再実行", button1_ClickをClickイベントハンドラに、 // Form1_LoadをフォームのLoadイベントハンドラに設定すること。 // ボタンをクリックすればするほど濃い画像になる。 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace IFS2 { public partial class Form1 : Form { public Image image; public Graphics g; // グラフィックイメージ用変数 public Matrix matrix=new Matrix(); // 変換マトリックス public double[, ,] Fsys1 = new double[,,] // 反復関数 {{{ 0.856, 0.0414}, {-0.0205, 0.858}}, {{ 0.244,-0.3850}, { 0.1760, 0.224}}, {{-0.144, 0.3900}, { 0.1810, 0.259}}, {{ 0.000, 0.0000}, { 0.3550, 0.216}}}; public double[,] Fsys2 = new double[,] // 定数項 { { 0.07, 0.147 }, { 0.393, 0.102 }, { 0.527, -0.014 }, { 0.486, 0.05 } }; public double[] PrTab = new double[] {0.73, 0.86, 0.99};// 累積確率(最後の1.00は書かないこと) public Form1() { InitializeComponent(); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.Transform = matrix; e.Graphics.DrawImage(image,0,0); } private void button1_Click(object sender, EventArgs e) { int ID; double X1, Y1; Brush brush = new SolidBrush(Color.DarkGreen); Random RD = new Random(); double X0 = 1.0; double Y0 = 1.0; for (int i = 0; i < 20000; i++) { double R = RD.NextDouble(); for (ID = 0; ID < PrTab.Length; ID++) if (R < PrTab[ID]) break; X1 = Fsys1[ID, 0, 0] * X0 + Fsys1[ID, 0, 1] * Y0 + Fsys2[ID, 0]; Y1 = Fsys1[ID, 1, 0] * X0 + Fsys1[ID, 1, 1] * Y0 + Fsys2[ID, 1]; if (X1 < 1.0 && Y1 < 1.0 && X1 >= 0 && Y1 >= 0) g.FillRectangle(brush, (float)(X1*1000), (float)(1000 - Y1*1000), 1, 1); X0 = X1; Y0 = Y1; } this.Invalidate(); } private void window(float X1, float Y1, float X2, float Y2, float R) { // 変換マトリックスの設定 float W = ClientSize.Width; float H = ClientSize.Height; float SX = W / (X2 - 1); float SY = H / (Y2 - Y1); float MX = -SX * X1; float MY = -SY * Y1; matrix.Scale(SX, SY); matrix.Rotate(R); matrix.Translate(MX, MY); } private void Form1_Load(object sender, EventArgs e) { image = new Bitmap(1000, 1000); g = Graphics.FromImage(image); g.Clear(Color.White); window(0, 0, 1000, 1000, 0); button1_Click(sender, e); // 1回だけ実行 } } }