概要
cscの作法、調べてみた。
練習問題やってみた。
練習問題
コッホ曲線を表示せよ。
写真
サンプルコード
using System;
using System.Windows.Forms;
using System.Drawing;
class form1: Form {
double ANGLE = 0;
double LX = 20.0;
double LY = 150.0;
Graphics g;
form1() {
Text = "koch";
ClientSize = new Size(450, 200);
}
protected override void OnPaint(PaintEventArgs e) {
g = e.Graphics;
koch(5, 60);
base.OnPaint(e);
}
double fmod(double a, double b) {
double x = Math.Floor(a / b);
return a - b * x;
}
void turn(double a) {
ANGLE = fmod(ANGLE + a, 360.0);
}
void gmove(double l) {
Pen pen = new Pen(Color.Red);
double rd = 3.14159265359 / 180.0;
double x = l * Math.Cos(rd * ANGLE);
double y = -l * Math.Sin(rd * ANGLE);
int px = (int) LX;
int py = (int) LY;
LX += x;
LY += y;
g.DrawLine(pen, px, py, (int) LX, (int) LY);
}
void koch(int i, int j) {
if (i <= 1)
{
gmove(5.0);
return;
}
koch(i - 1, j);
turn(j);
koch(i - 1, j);
turn(-2 * j);
koch(i - 1, j);
turn(j);
koch(i - 1, j);
}
[STAThread]
public static void Main() {
Application.Run(new form1());
}
}
以上。
