Kanako's Programming


  

正N角形を描くプログラム(C#のソース)

using System;
using System.Drawing;
using System.Windows.Forms;

//正N角形を描く;
class Program : Form {
	public static void Main() {
		Application.Run(new Program());
	}

//N角形のNの値を指定する(既存:7)
	 private int number = 7;

	protected override void OnPaint(PaintEventArgs e) {

//ゆっくり描画したい場合は、 500より数字を大きくする;
		int time = 500;

//もっと大きく描画したい場合は、100より数字を大きくする。
		int r = 100;

		double angle = 0;
		int a0 = 0;
		int a1 = 0;
		int t0 = 0;
		int t1 = 0;
		int w0,w1;
		for (int i=0; i<number+1; ++i) {
			Point points = new Point();
			points = GetPoint(angle,r);
			angle += 360 / number;
			Graphics g = e.Graphics;
			Pen pen = new Pen(Color.Black,5);
			w0 = points.X;
			w1 = points.Y;
			if (i == 0) {
				a0 = w0;
				a1 = w1;
				t0 = w0;
				t1 = w1;
			}else if (i == number) {
				g.DrawLine(pen, t0, t1, a0, a1);
				System.Threading.Thread.Sleep(time);
			}else {
				g.DrawLine(pen, t0, t1, w0, w1);
				System.Threading.Thread.Sleep(time);
				t0 = w0;
				t1 = w1;
			}
			pen.Dispose();
		}
		System.Threading.Thread.Sleep(time);
		Application.Exit(); 
	}

	public Point GetPoint(double angle,int r) {
		angle += 90;
		Point p = new Point();
		double q,t;
		q = 100 + r * Math.Cos(Math.PI * angle / 180);
		t = 100 - r * Math.Sin(Math.PI * angle / 180);
		p.X = (int)q;
		p.Y = (int)t;
		return p;
	}
}

BACK(Kanako Suto's Pageへ)
このページへの質問・コメントを歓迎致します。