Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ПЗ_ТП_Алексеев_220201_verson 1.docx
Скачиваний:
0
Добавлен:
01.03.2025
Размер:
480 Кб
Скачать

Файл MainWindow.Xaml.Cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

using System.Threading;

namespace П1_ТП

{

/// <summary>

/// Логика взаимодействия для Window1.xaml

/// </summary>

public partial class Window1 : Window

{

BitmapImage bitmap1, bitmap2, bitmap3, bitmap4, bitmap5;

int n = 0;

public Window1()

{

InitializeComponent();

Uri uri = new Uri(@"C:\images\image2_old.gif");

BitmapImage bitmap = new BitmapImage(uri);

image1.Source = bitmap;

Uri uri1 = new Uri(@"C:\images\image1.gif");

Uri uri2 = new Uri(@"C:\images\image2.gif");

Uri uri3 = new Uri(@"C:\images\image3.gif");

Uri uri4 = new Uri(@"C:\images\image4.gif");

Uri uri5 = new Uri(@"C:\images\image5.gif");

bitmap1 = new BitmapImage(uri1);

bitmap2 = new BitmapImage(uri2);

bitmap3 = new BitmapImage(uri3);

bitmap4 = new BitmapImage(uri4);

bitmap5 = new BitmapImage(uri5);

}

public double Ax;

public double Ay;

public double Bx;

public double By;

public double Cx;

public double Ex;

public double getCurve(double x1, double y1, double x2, double y2, double x3)

{

double k = (y2 - y1) / (x2 - x1);

double b = y1 - k * x1;

double y = k * x3 + b;

return y;

}

public double[] MonteCarlo(double Ax, double Ay, double Bx, double By, double Cx, double Ex, int n)

{

double[] result = new double[5];

double[] rezult = new double[5];

//------------

Random test = new Random();

//eto visota opisannogo pryamoygolnika

double RH = By - Ay;

//eto wirina opisannogo pryamoygolnika;

double RW = 0;

double MaxX = 0;

double MaxY = 0;

double MinX = 0;

double MinY = 0;

double Cy = By;

double Ey = Ay;

MaxX = Math.Max(Math.Max(Ax, Bx), Math.Max(Cx, Ex));

MaxY = Math.Max(Math.Max(Ay, By), Math.Max(Cy, Ey));

MinX = Math.Min(Math.Min(Ax, Bx), Math.Min(Cx, Ex));

MinY = Math.Min(Math.Min(Ay, By), Math.Min(Cy, Ey));

RW = MaxX - MinX;

RH = MaxY - MinY;

//Plowad Opisannogo pryamoygolnika:

double S_Rect_O = 0;

S_Rect_O = RW * RH; //plowad opisannogo recta

double points = 0; //eto kol-vo popavwih tochek

double Square_Karlo = 1; //hyi znaet zachem

double delta = 0; //pogrewnost

DateTime stop, start; //zasekaem time

TimeSpan time; //init time

double S_Teor = 0;

S_Teor = (By - Ay) * (((Ex - Ax) + (Cx - Bx)) / 2); //schitaem plowad po teorii

double RandX = 0; //init tochek dlya random

double RandY = 0; //analogichno

double newy = 0; //proverka popadaniya

start = System.DateTime.Now;

for (int i = 0; i < n; i++) //Monte_Karlo v rabote

{

if (i == 0)

{

start = System.DateTime.Now;

}//zasekaem vremya

RandX = (test.NextDouble() * (MaxX - MinX) + MinX); //generiryem

RandY = (test.NextDouble() * (MaxY - MinY) + MinY); //analogichno

// if(x3<x4) esli c<e(slychai 1)

if (Cx < Ex)

{

if (RandX < Bx)

{

newy = getCurve(Ax, Ay, Bx, By, RandX);

if (RandY <= newy)

{

points += 1;

}

}

if (RandX == Bx)

{

points += 1;

}

if (RandX == Cx)

{

points += 1;

}

if (RandX > Bx && RandX < Cx)

{

points += 1;

}

if (RandX > Cx)

{

newy = getCurve(Cx, Cy, Ex, Ey, RandX);

if (RandY >= newy)

{

points += 1;

}

}

}

//if(Cx==Ex) //esli c==e(slychai 2)

if (Cx == Ex)

{

if (RandX < Bx)

{

newy = getCurve(Ax, Ay, Bx, By, RandX);

if (RandY <= newy)

{

points += 1;

}

}

if (RandX >= Bx && RandX <= Cx)

{

points += 1;

}

}

//if(Cx>Ex) //esli c>e(slychai 3)

if (Cx > Ex)

{

if (RandX < Bx)

{

newy = getCurve(Ax, Ay, Bx, By, RandX);

if (RandY <= newy)

{

points += 1;

}

}

if (RandX == Bx)

{

points += 1;

}

if (RandX == Cx)

{

points += 1;

}

if (RandX == Ex)

{

points += 1;

}

if (RandX > Bx && RandX < Cx)

{

newy = getCurve(Cx, Cy, Ex, Ey, RandX);

if (RandY >= newy)

{

points += 1;

}

}

}

}

stop = System.DateTime.Now;

time = stop - start;

Square_Karlo = points / n * S_Rect_O;

delta = System.Math.Abs(((Square_Karlo - S_Teor) / S_Teor) * 100);

//------------

rezult[0] = n;

rezult[1] = points;

rezult[2] = Math.Round(Square_Karlo * 10000) / 10000;

rezult[3] = Math.Round(delta * 10000) / 10000;

rezult[4] = Convert.ToDouble(time.Milliseconds.ToString());

return rezult;

}

private void button7_Click(object sender, RoutedEventArgs e)

{

try

{

Ax = System.Convert.ToDouble(textBox30.Text);

Ay = System.Convert.ToDouble(textBox31.Text);

Bx = System.Convert.ToDouble(textBox32.Text);

By = System.Convert.ToDouble(textBox33.Text);

Cx = System.Convert.ToDouble(textBox34.Text);

Ex = System.Convert.ToDouble(textBox35.Text);

}

catch

{

MessageBox.Show("Ошибка", "Error", MessageBoxButton.OK, MessageBoxImage.Error);

return;

}

if (Ax > Bx || Bx > Cx || Ax > Ex || Ax > Cx || Ay > By)

{

MessageBox.Show("Проверьте правильность введенных данных", "Error", MessageBoxButton.OK, MessageBoxImage.Error);

return;

}

if (Ax == 0 && Bx == 0 && Cx == 0 && Ex == 0 && Ay == 0 && By == 0)

{

MessageBox.Show("Проверьте правильность введенных данных", "Error", MessageBoxButton.OK, MessageBoxImage.Error);

return;

}

double MaxX = 0;

double MaxY = 0;

double MinX = 0;

double MinY = 0;

double Cy = By;

double Ey = Ay;

MaxX = Math.Max(Math.Max(Ax, Bx), Math.Max(Cx, Ex));

MaxY = Math.Max(Math.Max(Ay, By), Math.Max(Cy, Ey));

MinX = Math.Min(Math.Min(Ax, Bx), Math.Min(Cx, Ex));

MinY = Math.Min(Math.Min(Ay, By), Math.Min(Cy, Ey));

double S_Rect_O = (MaxX - MinX) * (MaxY - MinY);

double S_Rect_Teor = (By - Ay) * (((Ex - Ax) + (Cx - Bx)) / 2);

double[] table_rez = new double[5];

//204*69+63/2 =

//------------

//int index = 0;

if (comboBox1.Text == "auto")

{

n = 1000;

for (int i = 0; i < 5; i++)

{

table_rez = MonteCarlo(Ax, Ay, Bx, By, Cx, Ex, n);

n *= 10;

if (table_rez[0] == 1000.0)

{

textBox6.Text = Convert.ToString(table_rez[0]);

textBox7.Text = Convert.ToString(table_rez[1]);

textBox8.Text = Convert.ToString(table_rez[2]);

textBox9.Text = Convert.ToString(table_rez[3]);

textBox10.Text = Convert.ToString(table_rez[4]);

}

else if (table_rez[0] == 10000.0)

{

textBox11.Text = Convert.ToString(table_rez[0]);

textBox12.Text = Convert.ToString(table_rez[1]);

textBox13.Text = Convert.ToString(table_rez[2]);

textBox14.Text = Convert.ToString(table_rez[3]);

textBox15.Text = Convert.ToString(table_rez[4]);

}

else if (table_rez[0] == 100000.0)

{

textBox16.Text = Convert.ToString(table_rez[0]);

textBox17.Text = Convert.ToString(table_rez[1]);

textBox18.Text = Convert.ToString(table_rez[2]);

textBox19.Text = Convert.ToString(table_rez[3]);

textBox20.Text = Convert.ToString(table_rez[4]);

}

else if (table_rez[0] == 1000000.0)

{

textBox21.Text = Convert.ToString(table_rez[0]);

textBox22.Text = Convert.ToString(table_rez[1]);

textBox23.Text = Convert.ToString(table_rez[2]);

textBox24.Text = Convert.ToString(table_rez[3]);

textBox25.Text = Convert.ToString(table_rez[4]);

}

else if (table_rez[0] == 10000000.0)

{

textBox26.Text = Convert.ToString(table_rez[0]);

textBox27.Text = Convert.ToString(table_rez[1]);

textBox28.Text = Convert.ToString(table_rez[2]);

textBox29.Text = Convert.ToString(table_rez[3]);

textBox36.Text = Convert.ToString(table_rez[4]);

}

textBox38.Text = Convert.ToString(S_Rect_O);

textBox40.Text = Convert.ToString(S_Rect_Teor);

}

}

else

{

n = Convert.ToInt32(comboBox1.Text);

table_rez = MonteCarlo(Ax, Ay, Bx, By, Cx, Ex, n);

if (table_rez[0] == 1000.0)

{

textBox6.Text = Convert.ToString(table_rez[0]);

textBox7.Text = Convert.ToString(table_rez[1]);

textBox8.Text = Convert.ToString(table_rez[2]);

textBox9.Text = Convert.ToString(table_rez[3]);

textBox10.Text = Convert.ToString(table_rez[4]);

}

else if (table_rez[0] == 10000.0)

{

textBox11.Text = Convert.ToString(table_rez[0]);

textBox12.Text = Convert.ToString(table_rez[1]);

textBox13.Text = Convert.ToString(table_rez[2]);

textBox14.Text = Convert.ToString(table_rez[3]);

textBox15.Text = Convert.ToString(table_rez[4]);

}

else if (table_rez[0] == 100000.0)

{

textBox16.Text = Convert.ToString(table_rez[0]);

textBox17.Text = Convert.ToString(table_rez[1]);

textBox18.Text = Convert.ToString(table_rez[2]);

textBox19.Text = Convert.ToString(table_rez[3]);

textBox20.Text = Convert.ToString(table_rez[4]);

}

else if (table_rez[0] == 1000000.0)

{

textBox21.Text = Convert.ToString(table_rez[0]);

textBox22.Text = Convert.ToString(table_rez[1]);

textBox23.Text = Convert.ToString(table_rez[2]);

textBox24.Text = Convert.ToString(table_rez[3]);

textBox25.Text = Convert.ToString(table_rez[4]);

}

else if (table_rez[0] == 10000000.0)

{

textBox26.Text = Convert.ToString(table_rez[0]);

textBox27.Text = Convert.ToString(table_rez[1]);

textBox28.Text = Convert.ToString(table_rez[2]);

textBox29.Text = Convert.ToString(table_rez[3]);

textBox36.Text = Convert.ToString(table_rez[4]);

}

}

}

//slychai 1

private void button1_Click(object sender, RoutedEventArgs e)

{

Ax = 15; Ay = 10;

Bx = 30; By = 30;

Cx = 40; Ex = 50;

image1.Source = bitmap1;

//-------

textBox30.Text = Convert.ToString(Ax); textBox31.Text = Convert.ToString(Ay);

textBox32.Text = Convert.ToString(Bx); textBox33.Text = Convert.ToString(By);

textBox34.Text = Convert.ToString(Cx); textBox35.Text = Convert.ToString(Ex);

}

// slychai 2

private void button2_Click(object sender, RoutedEventArgs e)

{

Ax = 15; Ay = 10;

Bx = 30; By = 30;

Cx = 50; Ex = 50;

image1.Source = bitmap2;

//-------

textBox30.Text = Convert.ToString(Ax); textBox31.Text = Convert.ToString(Ay);

textBox32.Text = Convert.ToString(Bx); textBox33.Text = Convert.ToString(By);

textBox34.Text = Convert.ToString(Cx); textBox35.Text = Convert.ToString(Ex);

}

// slychai 3

private void button3_Click(object sender, RoutedEventArgs e)

{

Ax = 3; Ay = 14;

Bx = 7; By = 25;

Cx = 20; Ex = 16;

image1.Source = bitmap3;

//-------

textBox30.Text = Convert.ToString(Ax); textBox31.Text = Convert.ToString(Ay);

textBox32.Text = Convert.ToString(Bx); textBox33.Text = Convert.ToString(By);

textBox34.Text = Convert.ToString(Cx); textBox35.Text = Convert.ToString(Ex);

}

// rand slychai 1

private void button4_Click(object sender, RoutedEventArgs e)

{

Random input = new Random();

for (int i = 0; i < 1000000; i++)

{

Ax = Convert.ToInt32(input.NextDouble() * 1000);

Ay = Convert.ToInt32(input.NextDouble() * 1000);

Bx = Convert.ToInt32(input.NextDouble() * 1000);

By = Convert.ToInt32(input.NextDouble() * 1000);

Cx = Convert.ToInt32(input.NextDouble() * 1000);

Ex = Convert.ToInt32(input.NextDouble() * 1000);

if (Cx < Ex && Ax < Bx && Ay < By && Bx < Cx && Ax < Ex && Ax < Cx)

{

break;

}

}

// RandX = (test.NextDouble() * (MaxX - MinX) + MinX);

image1.Source = bitmap4;

//-------

textBox30.Text = Convert.ToString(Ax); textBox31.Text = Convert.ToString(Ay);

textBox32.Text = Convert.ToString(Bx); textBox33.Text = Convert.ToString(By);

textBox34.Text = Convert.ToString(Cx); textBox35.Text = Convert.ToString(Ex);

}

// rand slychai 3

private void button5_Click(object sender, RoutedEventArgs e)

{

Random input = new Random();

for (int i = 0; i < 1000000; i++)

{

Ax = Convert.ToInt32(input.NextDouble() * 1000);

Ay = Convert.ToInt32(input.NextDouble() * 1000);

Bx = Convert.ToInt32(input.NextDouble() * 1000);

By = Convert.ToInt32(input.NextDouble() * 1000);

Cx = Convert.ToInt32(input.NextDouble() * 1000);

Ex = Convert.ToInt32(input.NextDouble() * 1000);

if (Cx > Ex && Ax < Bx && Ay < By && Bx < Cx && Ax < Ex && Ax < Cx)

{

break;

}

}

image1.Source = bitmap5;

//-------

textBox30.Text = Convert.ToString(Ax); textBox31.Text = Convert.ToString(Ay);

textBox32.Text = Convert.ToString(Bx); textBox33.Text = Convert.ToString(By);

textBox34.Text = Convert.ToString(Cx); textBox35.Text = Convert.ToString(Ex);

}

//обработчик события "Очистить"

private void button6_Click(object sender, RoutedEventArgs e)

{

textBox6.Text = ""; textBox7.Text = ""; textBox8.Text = ""; textBox9.Text = "";

textBox10.Text = ""; textBox11.Text = ""; textBox12.Text = ""; textBox13.Text = "";

textBox14.Text = ""; textBox15.Text = ""; textBox16.Text = ""; textBox17.Text = "";

textBox18.Text = ""; textBox19.Text = ""; textBox20.Text = ""; textBox21.Text = "";

textBox22.Text = ""; textBox23.Text = ""; textBox24.Text = ""; textBox25.Text = "";

textBox26.Text = ""; textBox27.Text = ""; textBox28.Text = ""; textBox29.Text = "";

textBox36.Text = "";

textBox30.Text = "";

textBox31.Text = "";

textBox32.Text = "";

textBox33.Text = "";

textBox34.Text = "";

textBox35.Text = "";

textBox38.Text = ""; textBox40.Text = "";

}

private void Window_Loaded(object sender, RoutedEventArgs e)

{

}

private void textBox39_TextChanged(object sender, TextChangedEventArgs e)

{

}

private void textBox30_TextChanged(object sender, TextChangedEventArgs e)

{

}

}

}

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]