//-------------------------------------------------------------
//	波形描画用のメソッド
//
//	作成者：三上直樹，2022/11/11 作成，ⓒ三上直樹 2022
//-------------------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace アンダーサンプリング
{
	public partial class Form1 : Form
	{
		private void Drawing(Graphics gr)
		{
			Pen penBlk = new Pen(Color.Black, 1);
			Pen penBlu = new Pen(Color.Blue, 1) { LineJoin = LineJoin.Round };
			Pen penSky = new Pen(Color.SkyBlue, 1) { LineJoin = LineJoin.Round };

			gr.TranslateTransform(115, 50);

			// 元のアナログ信号
			DrawWave(gr, vA_, penBlu, "元のアナログ信号");

			int y0 = (int)Math.Abs(Y_UNIT_) + 25;
			gr.DrawLine(penBlk, 0, y0, DT_*DATA_D_, y0);
			for (int n=0; n<=DATA_D_; n+=100)	// 目盛： 1 ms ごと
				gr.DrawLine(penBlk, n*DT_, y0+2, n*DT_, y0-2);
			for (int n=0; n<=DATA_D_; n+=100)
				gr.DrawString((n/100).ToString(), Font, Brushes.Black, n*DT_-5, y0+8);
			gr.DrawString("[ms]", Font, Brushes.Black, DT_*DATA_D_+5, y0+8);

			// 標本化された信号
			DrawWave(gr, vA_, penSky, "", DY0_);
			DrawSamples(gr, vD_, penBlu, "離散時間信号");

			// 再生されたアナログ信号
			DrawWave(gr, vR_, penBlu, "再生アナログ信号", DY0_-20);
		}

		void DrawWave(Graphics gr, double[] xn, Pen myPen, string str, int dy0 = 0)
		{
			gr.TranslateTransform(0, dy0);
			gr.DrawLine(Pens.Black, 0, 0, DATA_D_*DT_, 0);
			gr.DrawString(str, Font, Brushes.Black, -105, -5);

			PointF[] pt = new PointF[DATA_D_];
			for (int n=0; n<DATA_D_; n++)
				pt[n] = new PointF(DT_*n, Y_UNIT_*(float)xn[n+START_]);
			gr.DrawLines(myPen, pt);
		}

		// 標本化データの表示
		void DrawSamples(Graphics gr, double[] xn, Pen myPen, string str, int dy0 = 0)
		{
			gr.TranslateTransform(0, dy0);
			gr.DrawLine(Pens.Black, 0, 0, DATA_D_*DT_, 0);
			gr.DrawString(str, Font, Brushes.Black, -110, -5);

			const float r0 = 3;
			for (int n=0; n<DATA_D_; n+=10)
			{
				float x = DT_*n-r0;
				float y = Y_UNIT_*(float)xn[n+START_]-r0;
				gr.FillEllipse(Brushes.White, x, y, 2*r0, 2*r0);
				gr.DrawEllipse(myPen, x, y, 2*r0, 2*r0);
			}
		}
	}
}
