private void button1_Click(object sender, EventArgs e) { if (NewModel) { t = 0; N = int.Parse(textBox1.Text); R = double.Parse(textBox2.Text); Vmax = double.Parse(textBox3.Text); Xmax = double.Parse(textBox4.Text) / 2; Ymax = double.Parse(textBox5.Text) / 2; dt = double.Parse(textBox6.Text); K = double.Parse(textBox7.Text);//забираем переменные из меню X = new double[N]; Y = new double[N]; Vx = new double[N]; Vy = new double[N];//ввод массива с N переменными Cs = new double[N]; Cn = new int[N]; for (int i = 0; i < N; i++) { X[i] = (2 * rnd.NextDouble() - 1) * (Xmax - R); Y[i] = (2 * rnd.NextDouble() - 1) * (Ymax - R); Vx[i] = (2 * rnd.NextDouble() - 1) * Vmax; Vy[i] = (2 * rnd.NextDouble() - 1) * Vmax; Cs[i] = 0; Cn[i] = 0; }//задается случайное значение скорости и начальное положение for (int i = 0; i < 500; i++) { distrV[i] = 0; } Nans = 0; Gr1.Clear(pictureBox1.BackColor); //чистит поле Gr1.DrawRectangle(pen2, (int)((Bmp1.Width / 2) - K * Xmax), (int)((Bmp1.Height / 2) - K * Ymax), (int)(2 * K * Xmax), (int)(2 * K * Ymax)); //рисуется область Gr2.DrawImage(Bmp1, 0, 0); for (int i = 0; i < N; i++) { Gr2.DrawEllipse(pen1, (int)((Bmp1.Width / 2) + K * (X[i] - R)), (int)((Bmp1.Height / 2) - K * (Y[i] + R)), (int)(2 * K * R), (int)(2 * K * R)); Gr2.FillEllipse(Br, (int)((Bmp1.Width / 2) + K * (X[i] - R)), (int)((Bmp1.Height / 2) - K * (Y[i] + R)), (int)(2 * K * R), (int)(2 * K * R)); }// рисуются молекулы pictureBox1.Image = Bmp2; NewModel = false; } посмотретьToolStripMenuItem.Enabled = false; //кнопку "посмотреть" больше не нажать сохранитьToolStripMenuItem.Enabled = false; button1.Enabled = false; //нельзя нажать первую кнопку button2.Enabled = true; //можно нажать вторую кнопку timer1.Enabled = true; //таймер включается //Xscr = (int)((Bmp1.Width / 2) + K*X);Yscr = (int)((Bmp1.Width / 2) + K*Y); }
private void Button1_Click(object sender, EventArgs e) { if (NewModel) { T = 0; Nans = 0; N = Convert.ToInt32(textBox1.Text); //int.Parse R = Convert.ToDouble(textBox2.Text); Vmax = Convert.ToDouble(textBox3.Text); Xmax = Convert.ToDouble(textBox4.Text) / 2; Ymax = Convert.ToDouble(textBox5.Text) / 2; dt = Convert.ToDouble(textBox6.Text); K = Convert.ToDouble(textBox7.Text); X = new double[N]; Y = new double[N]; CS = new double[N]; Vx = new double[N]; Vy = new double[N]; CN = new int[N]; Gr1.Clear(pictureBox1.BackColor); Gr1.DrawRectangle(P2, (int)(Bmp1.Width / 2 - K * Xmax), (int)(Bmp1.Height / 2 - K * Ymax), (int)(2 * K * Xmax), (int)(2 * K * Ymax)); Gr2.DrawImage(Bmp1, 0, 0); for (int i = 0; i < N; i++) { CS[i] = 0; CN[i] = 0; X[i] = (2 * Rnd.NextDouble() - 1) * (Xmax - R); Y[i] = (2 * Rnd.NextDouble() - 1) * (Ymax - R); Vx[i] = (2 * Rnd.NextDouble() - 1) * Vmax; Vy[i] = (2 * Rnd.NextDouble() - 1) * Vmax; } for (int i = 0; i < 500; i++) { DistrV[i] = 0; } for (int i = 0; i < N; i++) { Gr2.DrawEllipse(P1, (int)(Bmp1.Width / 2 + K * (X[i] - R)), (int)(Bmp1.Height / 2 - K * (Y[i] + R)), (int)(2 * K * R), (int)(2 * K * R)); Gr2.FillEllipse(Br, (int)(Bmp1.Width / 2 + K * (X[i] - R)), (int)(Bmp1.Height / 2 - K * (Y[i] + R)), (int)(2 * K * R), (int)(2 * K * R)); } pictureBox1.Image = Bmp2; NewModel = false; } сохранитьToolStripMenuItem.Enabled = false; посмотретьToolStripMenuItem.Enabled = false; button1.Enabled = false; button2.Enabled = true; timer1.Enabled = true; }
SolidBrush Br = new SolidBrush(Color.Yellow); //создание кисточки private void timer1_Tick(object sender, EventArgs e) { t++; textBox8.Text = Convert.ToString(t * dt); //движение частиц и рикошет от стенок for (int i = 0; i < N; i++) { X[i] += Vx[i] * dt; Y[i] += Vy[i] * dt; Cs[i] += Math.Sqrt(Vx[i] * Vx[i] + Vy[i] * Vy[i]) * dt; if ((X[i] >= Xmax - R) && (Vx[i] > 0)) { Vx[i] = -Vx[i]; } if ((X[i] <= -Xmax + R) && (Vx[i] < 0)) { Vx[i] = -Vx[i]; } if ((Y[i] >= Ymax - R) && (Vy[i] > 0)) { Vy[i] = -Vy[i]; } if ((Y[i] <= -Ymax + R) && (Vy[i] < 0)) { Vy[i] = -Vy[i]; } } //соударение частиц между собой for (int i = 0; i < N - 1; i++) { for (int j = i + 1; j < N; j++) { Px = X[j] - X[i]; Py = Y[j] - Y[i]; Psq = Px * Px + Py * Py; if (Psq <= 4 * R * R) { VP1x = ((Vx[i] * Px + Vy[i] * Py) * Px / Psq); VP1y = ((Vx[i] * Px + Vy[i] * Py) * Py / Psq); VP2x = ((Vx[j] * Px + Vy[j] * Py) * Px / Psq); VP2y = ((Vx[j] * Px + Vy[j] * Py) * Py / Psq); VN1x = Vx[i] - VP1x; VN1y = Vy[i] - VP1y; VN2x = Vx[j] - VP2x; VN2y = Vy[j] - VP2y; if ((VP2x - VP1x) * Px + (VP2y - VP1y) * Py < 0) { Vx[i] = VP2x + VN1x; Vy[i] = VP2y + VN1y; Vx[i] = VP1x + VN2x; Vy[j] = VP1y + VN2y; Cn[i]++; Cn[j]++; } } } } if (t % time == 0)//если остаток от деления =0 { Nans++; for (int i = 0; i < N - 1; i++) { int index = (int)(Math.Sqrt(Vx[i] * Vx[i] + Vy[i] * Vy[i]) / dv); distrV[index]++; } } Gr2.DrawImage(Bmp1, 0, 0); for (int i = 0; i < N; i++) { Gr2.DrawEllipse(pen1, (int)((Bmp1.Width / 2) + K * (X[i] - R)), (int)((Bmp1.Height / 2) - K * (Y[i] + R)), (int)(2 * K * R), (int)(2 * K * R)); Gr2.FillEllipse(Br, (int)((Bmp1.Width / 2) + K * (X[i] - R)), (int)((Bmp1.Height / 2) - K * (Y[i] + R)), (int)(2 * K * R), (int)(2 * K * R)); } pictureBox1.Image = Bmp2; }
private void openFileDialog1_FileOk(object sender, CancelEventArgs e) { f = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read); DataOut = new BinaryReader(f); N = DataOut.ReadInt32(); R = DataOut.ReadDouble(); Vmax = DataOut.ReadDouble(); Xmax = DataOut.ReadDouble(); Ymax = DataOut.ReadDouble(); dt = DataOut.ReadDouble(); K = DataOut.ReadDouble(); X = new double[N]; Y = new double[N]; CS = new double[N]; Vx = new double[N]; Vy = new double[N]; CN = new int[N]; for (int i = 0; i < N; i++) { X[i] = DataOut.ReadDouble(); } for (int i = 0; i < N; i++) { Y[i] = DataOut.ReadDouble(); } for (int i = 0; i < N; i++) { Vx[i] = DataOut.ReadDouble(); } for (int i = 0; i < N; i++) { Vy[i] = DataOut.ReadDouble(); } T = DataOut.ReadInt32(); for (int i = 0; i < N; i++) { CS[i] = DataOut.ReadDouble(); } for (int i = 0; i < N; i++) { CN[i] = DataOut.ReadInt32(); } Nans = DataOut.ReadInt32(); for (int i = 0; i < N; i++) { DistrV[i] = DataOut.ReadInt32(); } DataOut.Close(); NewModel = false; посмотретьToolStripMenuItem.Enabled = true; textBox1.Text = Convert.ToString(N); textBox2.Text = Convert.ToString(R); textBox3.Text = Convert.ToString(Vmax); textBox4.Text = Convert.ToString(Xmax); textBox5.Text = Convert.ToString(Ymax); textBox6.Text = Convert.ToString(dt); textBox7.Text = Convert.ToString(K); textBox8.Text = Convert.ToString(T * dt); Gr1.Clear(pictureBox1.BackColor); Gr1.DrawRectangle(P2, (int)(Bmp1.Width / 2 - K * Xmax), (int)(Bmp1.Height / 2 - K * Ymax), (int)(2 * K * Xmax), (int)(2 * K * Ymax)); Gr2.DrawImage(Bmp1, 0, 0); for (int i = 0; i < N; i++) { Gr2.DrawEllipse(P1, (int)(Bmp1.Width / 2 + K * (X[i] - R)), (int)(Bmp1.Height / 2 - K * (Y[i] + R)), (int)(2 * K * R), (int)(2 * K * R)); Gr2.FillEllipse(Br, (int)(Bmp1.Width / 2 + K * (X[i] - R)), (int)(Bmp1.Height / 2 - K * (Y[i] + R)), (int)(2 * K * R), (int)(2 * K * R)); } pictureBox1.Image = Bmp2; }
private void Timer1_Tick(object sender, EventArgs e) { T++; textBox8.Text = Convert.ToString(T * dt); for (int i = 0; i < N; i++) { X[i] += Vx[i] * dt; Y[i] += Vy[i] * dt; CS[i] += Math.Sqrt(Vx[i] * Vx[i] + Vy[i] * Vy[i]) * dt; if (((X[i] >= Xmax - R) && (Vx[i] > 0)) || ((X[i] <= -Xmax + R) && (Vx[i] < 0))) { Vx[i] = -Vx[i]; } if (((Y[i] >= Ymax - R) && (Vy[i] > 0)) || ((Y[i] <= -Ymax + R) && (Vy[i] < 0))) { Vy[i] = -Vy[i]; } } for (int i = 0; i < N - 1; i++) { for (int j = i + 1; j < N; j++) { Px = X[j] - X[i]; Py = Y[j] - Y[i]; Psq = Px * Px + Py * Py; if (Psq <= 4 * R * R) { VP1x = (Vx[i] * Px + Vy[i] * Py) * Px / Psq; VP1y = (Vx[i] * Px + Vy[i] * Py) * Py / Psq; VP2x = (Vx[j] * Px + Vy[j] * Py) * Px / Psq; VP2y = (Vx[j] * Px + Vy[j] * Py) * Py / Psq; if ((VP2x - VP1x) * Px + (VP2y - VP1y) * Py < 0) { VN1x = Vx[i] - VP1x; VN1y = Vy[i] - VP1y; VN2x = Vx[j] - VP2x; VN2y = Vy[j] - VP2y; Vx[i] = VP2x + VN1x; Vy[i] = VP2y + VN1y; Vx[j] = VP1x + VN2x; Vy[j] = VP1y + VN2y; CN[i]++; CN[j]++; } } } } if (T % Time == 0) { Nans++; for (int i = 0; i < N - 1; i++) { int index = (int)(Math.Sqrt(Vx[i] * Vx[i] + Vy[i] * Vy[i]) / dV); DistrV[index]++; } } Gr2.DrawImage(Bmp1, 0, 0); for (int i = 0; i < N; i++) { Gr2.DrawEllipse(P1, (int)(Bmp1.Width / 2 + K * (X[i] - R)), (int)(Bmp1.Height / 2 - K * (Y[i] + R)), (int)(2 * K * R), (int)(2 * K * R)); Gr2.FillEllipse(Br, (int)(Bmp1.Width / 2 + K * (X[i] - R)), (int)(Bmp1.Height / 2 - K * (Y[i] + R)), (int)(2 * K * R), (int)(2 * K * R)); } Bmp2.GetPixel(200, 200); pictureBox1.Image = Bmp2; }