はじめてのC#

や、実は全然初めてじゃないけどまだまだ初心者の範囲だし実際始めてのトピックなので取り上げてみました。・・・て言ってもただソースを載せているだけですけどね(^^;
ひととおり文法を覚えてようやくGDIの書き方を覚えた程度。C#らしいプログラムを書くのはもうちょっと先かな?まだまだベターC++ぐらいにしか使えていません。


内容は前トピックの増加曲線を実際に書いてみるプログラムです。ちょうどいい練習になりました

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;

namespace CreateCurve
{
	/// <summary>
	/// Form1 の概要の説明です。
	/// </summary>
	public class CreateCurve : System.Windows.Forms.Form
	{
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.TrackBar alphaBar;
		private System.Windows.Forms.TrackBar betaBar;
		/// <summary>
		/// 必要なデザイナ変数です。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public CreateCurve()
		{
			//
			// Windows フォーム デザイナ サポートに必要です。
			//
			InitializeComponent();

			//
			// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
			//
			PictureDraw();
		}

		/// <summary>
		/// 使用されているリソースに後処理を実行します。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows フォーム デザイナで生成されたコード 
		/// <summary>
		/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
		/// コード エディタで変更しないでください。
		/// </summary>
		private void InitializeComponent()
		{
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.alphaBar = new System.Windows.Forms.TrackBar();
			this.betaBar = new System.Windows.Forms.TrackBar();
			((System.ComponentModel.ISupportInitialize)(this.alphaBar)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.betaBar)).BeginInit();
			this.SuspendLayout();
			// 
			// pictureBox1
			// 
			this.pictureBox1.BackColor = System.Drawing.Color.White;
			this.pictureBox1.Cursor = System.Windows.Forms.Cursors.Cross;
			this.pictureBox1.Location = new System.Drawing.Point(24, 16);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(400, 352);
			this.pictureBox1.TabIndex = 0;
			this.pictureBox1.TabStop = false;
			// 
			// alphaBar
			// 
			this.alphaBar.Location = new System.Drawing.Point(8, 384);
			this.alphaBar.Maximum = 100;
			this.alphaBar.Name = "alphaBar";
			this.alphaBar.Size = new System.Drawing.Size(432, 50);
			this.alphaBar.TabIndex = 1;
			this.alphaBar.TickFrequency = 5;
			this.alphaBar.TickStyle = System.Windows.Forms.TickStyle.TopLeft;
			this.alphaBar.Value = 50;
			this.alphaBar.Scroll += new System.EventHandler(this.alphaBar_Scroll);
			// 
			// betaBar
			// 
			this.betaBar.Location = new System.Drawing.Point(8, 448);
			this.betaBar.Maximum = 90;
			this.betaBar.Name = "betaBar";
			this.betaBar.Size = new System.Drawing.Size(432, 50);
			this.betaBar.TabIndex = 2;
			this.betaBar.TickFrequency = 5;
			this.betaBar.TickStyle = System.Windows.Forms.TickStyle.TopLeft;
			this.betaBar.Value = 45;
			this.betaBar.Scroll += new System.EventHandler(this.betaBar_Scroll);
			// 
			// CreateCurve
			// 
			this.AutoScale = false;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
			this.ClientSize = new System.Drawing.Size(450, 524);
			this.Controls.Add(this.betaBar);
			this.Controls.Add(this.alphaBar);
			this.Controls.Add(this.pictureBox1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "CreateCurve";
			this.ShowInTaskbar = false;
			this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
			this.Text = "CreateCurve";
			((System.ComponentModel.ISupportInitialize)(this.alphaBar)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.betaBar)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		protected void PictureDraw()
		{
			pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height) ;
			Graphics g = Graphics.FromImage(pictureBox1.Image) ;

			int pictureHeight = pictureBox1.Size.Height;
			int pictureWidth = pictureBox1.Size.Width;

			double alpha = (double)alphaBar.Value / ( alphaBar.Maximum - alphaBar.Minimum );
			double beta = Math.Tan( ( Math.PI / 2 ) * (double)betaBar.Value / ( betaBar.Maximum - betaBar.Minimum + 1 ) );
			int alphaY = pictureHeight - pictureHeight * alphaBar.Value / ( alphaBar.Maximum - alphaBar.Minimum );
			int alphaX = pictureWidth * alphaBar.Value / ( alphaBar.Maximum - alphaBar.Minimum );

			//斜線
			using( Pen pen = new Pen( Color.Black ) )
			{
				pen.Width = 1;
				g.DrawLine( pen, 0, pictureHeight, pictureWidth, 0 );
			}

			//alpha中心の破線
			using( Pen pen = new Pen( Color.Red ) )
			{
				pen.Width = 1;
				pen.DashStyle = DashStyle.Dot;
				//横線
				g.DrawLine( pen, 0, alphaY, pictureWidth, alphaY );
				//縦線
				g.DrawLine( pen, alphaX, 0, alphaX, pictureHeight );
			}

			//曲線
			using( Pen pen = new Pen( Color.Blue ) )
			{
				int plotSize = 100;
				pen.Width = 2;

				PointF[] CurvePoint = new PointF[plotSize * 2 + 1];
				for( int x1 = 0; x1 < plotSize; ++x1 )
				{
					double dx = alpha * x1 / plotSize;
					double dy = Curve( dx, alpha, beta );
					CurvePoint[x1].X = (float)( dx * pictureWidth );
					CurvePoint[x1].Y = (float)( ( 1.0 - dy ) * pictureHeight );
				}
				for( int x2 = 0; x2 < plotSize; ++x2 )
				{
					double dx = alpha + ( 1.0 - alpha ) * x2 / plotSize;
					double dy = Curve( dx, alpha, beta );
					CurvePoint[x2 + plotSize].X = (float)( dx * pictureWidth );
					CurvePoint[x2 + plotSize].Y = (float)( ( 1.0 - dy ) * pictureHeight );
				}
				CurvePoint[plotSize * 2].X = pictureWidth;
				CurvePoint[plotSize * 2].Y = 0;

				g.DrawLines( pen, CurvePoint );
			}


		}

		protected double Curve( double x, double alpha, double beta )
		{
			if( x < 0 || 1 < x )
			{
				throw new Exception( "out of range" );
			}

			if( x == 0.0 )
			{
				return 0;
			}
			else
			{
				if( x < alpha )
				{
					return alpha * Math.Pow( x / alpha , beta );
				}
				else if( x > alpha )
				{
					return 1 - ( 1 - alpha ) * Math.Pow( ( 1 - x ) / ( 1 - alpha ), beta );
				}
				else
				{
					return alpha;
				}
			}
		}

		/// <summary>
		/// アプリケーションのメイン エントリ ポイントです。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new CreateCurve());
		}

		private void alphaBar_Scroll(object sender, System.EventArgs e)
		{
			PictureDraw();
		}

		private void betaBar_Scroll(object sender, System.EventArgs e)
		{
			PictureDraw();
		}
	}
}