using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; using System.Text; using System.Windows.Forms; namespace Carpet { public partial class Form1 : Form { public const int MD=5; public Image image; public Graphics g; // グラフィックイメージ用変数 public Brush [] brush = new Brush[MD]; // 描画ペン public Form1() { InitializeComponent(); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.DrawImage(image, 50, 50); } public void circle(int cnt, float Left, float Top, float SZ) { if (cnt <= 0) return; g.FillEllipse(brush[cnt % MD], Left , Top, SZ, SZ); float R = (float)((Math.Sqrt(2) / (4.0 + 2.0 * Math.Sqrt(2)) * (double)SZ)*1.005); float sR = 0.5F * SZ - 2F * R, SLDS = 0.5F * SZ - sR; float SLX = 0.5F * SZ - R, SL2 = SZ - 2F*R, R2 = R + R; circle(cnt - 1, Left + SLX, Top, R2); circle(cnt - 1, Left , Top + SLX, R2); circle(cnt - 1, Left + SL2, Top + SLX, R2); circle(cnt - 1, Left + SLX, Top + SL2, R2); circle(cnt - 1, Left + SLDS, Top + SLDS, sR * 2); } public void carpet(int cnt, float Left, float Top, float Size) { if (cnt < 1) return; float SZ = Size / 3, TP=Top; for(int i=0;i<3;i++){ float LF = Left; for (int j = 0; j < 3; j++, LF+=SZ) carpet(cnt - 1, LF, TP, SZ); TP=TP+SZ; } circle(cnt, Left + SZ, Top + SZ, SZ); } private void Form1_Load(object sender, EventArgs e) { image = new Bitmap(600, 600); g = Graphics.FromImage(image); g.Clear(Color.White); brush[0] = new SolidBrush(Color.FromArgb(255,0,0)); brush[1] = new SolidBrush(Color.FromArgb(255,255,64)); brush[2] = new SolidBrush(Color.FromArgb(0,0,127)); brush[3] = new SolidBrush(Color.FromArgb(0, 0, 255)); brush[4] = new SolidBrush(Color.FromArgb(0, 0, 0)); } private void button1_Click(object sender, EventArgs e) { carpet(6, 0F, 0F, 600F); this.Invalidate(); } private void button2_Click(object sender, EventArgs e) { g.FillEllipse(brush[0], 100, 100, 30, 30); this.Invalidate(); } } }