1. Sinc(x)訊號繪圖
namespace Q1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
textBox1.Text = "-15";
textBox2.Text = "15";
}
double sinc(double x)
{
if (x == 0) return 1;
return Math.Sin(x) / x;
}
private void button1_Click(object sender, EventArgs e)
{
panel1.Refresh();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
var l = int.Parse(textBox1.Text);
var r = int.Parse(textBox2.Text);
double m = double.MaxValue, M = double.MinValue;
var h = 300;
var w = 800;
var my = (panel1.Height - h) / 2;
var mx = (panel1.Width - w) / 2;
var mid = w / (r - l) * -l;
PointF? lastDot = null;
for (decimal x = l; x <= r; x += 0.1m)
{
var v = (sinc((double)x));
var p = new PointF((int)(mx + (x + Math.Abs(l)) * (w / (r - l))),
(int)(my + (1 - v) * h / 6 * 5));
if (x % 5 == 0)
{
e.Graphics.DrawString(((int)x).ToString(),
new Font(FontFamily.GenericSerif, 14), Brushes.Black,
p.X, my + h / 6 * 5);
e.Graphics.DrawLine(new Pen(Brushes.Black, 2),
p.X, my + h / 6 * 5, p.X, my + h / 6 * 5 - 4);
}
if (x % 1 == 0)
{
e.Graphics.DrawLine(new Pen(Brushes.Black, 2),
p.X, my + h / 6 * 5, p.X, my + h / 6 * 5 - 2);
}
if (lastDot != null)
{
e.Graphics.DrawLine(new Pen(Brushes.Red, 2),
p, lastDot.Value);
}
lastDot = p;
if (v < m) m = v;
if (v > M) M = v;
}
//MessageBox.Show($"{m:f1}/{M:f1}");
e.Graphics.DrawLine(new Pen(Brushes.Black, 2),// x
mx + -20, my + h / 6 * 5, mx + w, my + h / 6 * 5);
e.Graphics.DrawLine(new Pen(Brushes.Black, 2), // y
mx + mid, my - 15, mx + mid, my + h + 15);
for (int i = 0; i < 5; i++)
{
e.Graphics.DrawString($"{1 - (0.2 * i):f1}",
new Font(FontFamily.GenericSerif, 14), Brushes.Black,
mx + mid - 40, my + h / 6 * (i) - 5);
}
e.Graphics.DrawString($"{-0.2:f1}",
new Font(FontFamily.GenericSerif, 14), Brushes.Black,
mx + mid - 40, my + h - 20);
for (int i = 0; i <= 30; i++)
{
if (i % 5 == 0)
e.Graphics.DrawLine(new Pen(Brushes.Black, 2),
mx + mid, my + (int)((double)h * i / 30),
mx + mid + 4, my + (int)((double)h * i / 30));
e.Graphics.DrawLine(new Pen(Brushes.Black, 2),
mx + mid, my + (int)((double)h * i / 30),
mx + mid + 2, my + (int)((double)h * i / 30));
}
}
}
}
Last updated