barus's diary

とても真面目なblogですにゃ

C#グラフィック ドラゴン曲線 for VS2013 Express

 

 

 

参考URL:https://codezine.jp/article/detail/73

 

回帰10回

f:id:hatakeka:20160912165554p:plain 

 

 


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
 
 
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public class  MyPoint3D
        {
            public int x;
            public int y;
            public int z;
 
            public MyPoint3D() { }
            public MyPoint3D(int _x, int _y, int _z)
            {
                x = _x;
                y = _y;
                z = _z;
            }
        }
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            //描画先とするImageオブジェクトを作成する
            Bitmap canvas = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            //ImageオブジェクトのGraphicsオブジェクトを作成する
            Graphics gra = Graphics.FromImage(canvas);
 
            //Penオブジェクトの作成(幅1の黒色)
            //(この場合はPenを作成せずに、Pens.Blackを使っても良い)
            Pen pen = new Pen(Color.Blue, 1);
           
            //コッホ曲線描画
            //drwKoch_main(gra, pen);
            //樹木曲線
            //drwTree_main(gra, pen);
            //ドラゴン曲線
            drawDragon_main(gra, pen);
 
            pen.Dispose();
            gra.Dispose();
 
            //PictureBox1に表示する
            pictureBox1.Image = canvas;
 
        }
 
        //ドラゴン
        public void drawDragon_main(Graphics gra, Pen pen)
        {
            int kaisu = int.Parse(textBox1.Text);
 
            //出発点となる一対の点を指定します
            MyPoint3D P = new MyPoint3D(170, 140, 0);
            MyPoint3D Q = new MyPoint3D(400, 350, 0);
 
            //対となる二点の間にドラゴン曲線を描きます
            drawDragon(gra, pen, P, Q, kaisu);
  
        }
 
        //ドラゴン曲線を描くメソッド
        public void drawDragon(Graphics g, Pen pen, MyPoint3D a, MyPoint3D b, int n)
        {
 
            MyPoint3D c = new MyPoint3D();
 
            int xx, yy;
            xx = b.x - a.x;
            yy = -(b.y - a.y);
 
            c.x = a.x + (xx + yy) / 2;
            c.y = b.y + (xx + yy) / 2;
 
            //最後なので、実際に線を引きます
            if (n <= 0)
            {
                g.DrawLine(pen, a.x, a.y, c.x, c.y);   //点Aから点Cへ
                g.DrawLine(pen, b.x, b.y, c.x, c.y);   //点Bから点Cへ
            }
            //最後ではないので、さらにメソッドを呼び出します(再帰処理)
            else
            {
                drawDragon(g, pen, a, c, n - 1);    //点Aから点Cへ
                drawDragon(g, pen, b, c, n - 1);    //点Bから点Cへ
            }
        }           
 
    }
 
 
}
 

 

drawDragon_main()
drawDragon()

関数を追加。

コッホ曲線と、樹木曲線をコメントアウト

 

            //コッホ曲線描画
            //drwKoch_main(gra, pen);
            //樹木曲線
            //drwTree_main(gra, pen);
            //ドラゴン曲線
            drawDragon_main(gra, pen);

 

textBox1.Text の数値を変えると回帰回数を指定出来る。
プロパティの初期設定の仕方は、コッホ曲線参照してみてください。

 

 

終わり。