barus's diary

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

C#グラフィック コッホ曲線 for VS2013 Express

 

C#グラフィック コッホ曲線 for VS2013 Express

回帰2回の場合

f:id:hatakeka:20160912163648p:plain

回帰3回の場合

f:id:hatakeka:20160912164103p:plain

参考 URL
C#でグラフィック
http://dobon.net/vb/dotnet/graphics/drawrectangle.html
コッホ曲線
https://codezine.jp/article/detail/73

 

 

 

参考ページを元にC#用に作った。

 

 


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);
 
            pen.Dispose();
            gra.Dispose();
 
            //PictureBox1に表示する
            pictureBox1.Image = canvas;
 
        }
 
        public void drwKoch_main(Graphics gra, Pen pen)
        {
 
            int kaisu = int.Parse(textBox1.Text);
 
            //始点を3つ指定
            MyPoint3D P = new MyPoint3D(100, 160, 0);
            MyPoint3D Q = new MyPoint3D(400, 160, 0);
            MyPoint3D R = new MyPoint3D(250, 420, 0);
 
            //コッホ曲線描画
            drawKoch(gra, pen, P, Q, kaisu);
            drawKoch(gra, pen, Q, R, kaisu);
            drawKoch(gra, pen, R, P, kaisu);      
 
        }
 
        //コッホ曲線を描くメソッド
        public void drawKoch(Graphics g, Pen pen, MyPoint3D a, MyPoint3D b, int n)
        {
 
            //メソッド内部で使用する3点を生成します
            MyPoint3D c = new MyPoint3D();
            MyPoint3D d = new MyPoint3D();
            MyPoint3D e = new MyPoint3D();
 
            int xx, yy;
            double angle1, angle2, distance;
 
            c.x = (2 * a.x + b.x) / 3;
            c.y = (2 * a.y + b.y) / 3;
            d.x = (a.x + 2 * b.x) / 3;
            d.y = (a.y + 2 * b.y) / 3;
            xx = b.x - a.x;
            yy = -(b.y - a.y);
 
            distance = Math.Sqrt(xx * xx + yy * yy) / Math.Sqrt(3);
            
            if (xx >= 0)
            {  //元になる直線が右上がりの場合
                angle1 = Math.Atan*1;
                e.y = a.y - (int)(distance * Math.Sin(angle1));
            }
            else
            {       //元になる直線が右下がりの場合
                angle2 = Math.Atan*2;
                e.y = b.y - (int)(distance * Math.Sin(angle2));
            }
 
            //最後なので、実際に線を引きます
            if (n <= 0)
            {
                g.DrawLine(pen, a.x, a.y, c.x, c.y);    //点Aから点Cへ
                g.DrawLine(pen, c.x, c.y, e.x, e.y);    //点Cから点Eへ
                g.DrawLine(pen, e.x, e.y, d.x, d.y);    //点Eから点Dへ
                g.DrawLine(pen, d.x, d.y, b.x, b.y);    //点Dから点Bへ
            }
            //最後ではないので、更にメソッドを呼び出します(再帰処理)
            else
            {
                drawKoch(g, pen, a, c, n - 1);    //点Aから点Cへ
                drawKoch(g, pen, c, e, n - 1);    //点Cから点Eへ
                drawKoch(g, pen, e, d, n - 1);    //点Eから点Dへ
                drawKoch(g, pen, d, b, n - 1);    //点Dから点Bへ
            }
        }     
 
        
 
    }
 
 
}


 

 

Point3D構造体を使用しようとしたが、PresentationCore.dllがなかったので
MyPoint3Dクラスを作成。

 

参考にした、drawKoch()関数は、Java言語で書かれているが
メソッド名が、似ていて簡単にC#に移動出来た。
C#の場合、PENを変数に教えてあげる必要がある。

C#の場合
g.DrawLine(pen, a.x, a.y, c.x, c.y); //点Aから点Cへ

Javaの場合
g.drawLine(a.x,a.y,c.x,c.y); //点Aから点Cへ

違いはこれくらいか。

 

 

猫でもわかるC#プログラミング 第3版 (猫でもわかるプログラミング)

猫でもわかるC#プログラミング 第3版 (猫でもわかるプログラミング)

 

 

 

textBox1.Textは回帰回数を指定出来る。
エディタ側で、textBox1.Text上で右クリックしてプロパティを選択し
text値の値を3にして下さい。

f:id:hatakeka:20160912163933p:plain

 

textBox1.Textのプロパティ値のtextを3にする。

 

f:id:hatakeka:20160912163956p:plain

 

これで、初期設定が3になっています。

 

 

終わり。

 

 

 


 

 


 

 


 

 

 

 

作って覚えるVisual C# 2015 デスクトップアプリ入門

作って覚えるVisual C# 2015 デスクトップアプリ入門

 
現場ですぐに使える! VisualC#2015逆引き大全500の極意

現場ですぐに使える! VisualC#2015逆引き大全500の極意

 

 

 

 

 

 

*1:double)yy / xx) + Math.PI / 6;

                e.x = a.x + (int)(distance * Math.Cos(angle1

*2:double)yy / xx) - Math.PI / 6;

                e.x = b.x + (int)(distance * Math.Cos(angle2