Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Threading;
  11.  
  12. namespace easy_code {
  13.     public partial class MainForm : Form {
  14.         public Bitmap[] bitmaps;//[номер кадра]
  15.         public double[,] tasks;//[номер кадра,0(re)/1(im)]
  16.         public Thread[] threads;
  17.         public int image_size;
  18.         public int num_frames_on_thread;
  19.         public int ms_framerate;
  20.        
  21.         void MainFormFormClosing(object sender, FormClosingEventArgs e) {
  22.             endAllThreads(); // ВАЖНО, а то процесс не убьется. Должно быть событие на самой форме
  23.         }
  24.        
  25.         public MainForm() {
  26.             InitializeComponent();
  27.             //http://www.cyberforum.ru/windows-forms/thread110436.html#a_Q3
  28.             System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
  29.            
  30.             endAllThreads();//если по кнопке заебошить - нужен вызов на всякий пожарный
  31.            
  32.             int initial_re = -3,  final_re = 2;//не включительно
  33.             int initial_im = -2,  final_im = 1;//не включительно
  34.             double step_re_and_im = 0.05;
  35.            
  36.             image_size = 300;
  37.             ms_framerate = 33;
  38.             int num_threads = 2;
  39.            
  40.            
  41.             double sum_re = final_re - initial_re;
  42.             double sum_im = final_im - initial_im;
  43.            
  44.             int num_frames = Convert.ToInt32(Math.Floor(sum_re / step_re_and_im));
  45.            
  46.             if (sum_re < sum_im) {
  47.                 num_frames = Convert.ToInt32(Math.Floor(sum_im / step_re_and_im));
  48.             }
  49.            
  50.             num_frames_on_thread = num_frames / num_threads;
  51.             num_frames = num_frames_on_thread * num_threads; // реальное кол-во
  52.            
  53.            
  54.             // генерация задания
  55.             tasks = new double[num_frames,2];
  56.            
  57.             for (int i = 0; i < num_frames; i++) {
  58.                 double re = initial_re + sum_re / num_frames * i;
  59.                 double im = initial_im + sum_im / num_frames * i;
  60.                 tasks[i,0] = re;
  61.                 tasks[i,1] = im;
  62.                 //System.Diagnostics.Debug.WriteLine("Кадр №" + i.ToString() + ": re=" + re.ToString() + " im=" + im.ToString());
  63.             }
  64.            
  65.            
  66.             // запуск потоков
  67.             bitmaps = new Bitmap[num_frames];
  68.             threads = new Thread[num_threads + 1];// +1 !!!!!!
  69.            
  70.             for (int i = 0; i < num_threads; i++) {
  71.                 threads[i] = new Thread(new ParameterizedThreadStart(generate));
  72.                 threads[i].Start(i);
  73.             }
  74.            
  75.             // запуск анимации
  76.             threads[num_threads] = new Thread(animate);
  77.             threads[num_threads].Start();
  78.         }
  79.        
  80.         // ПОТОК генерации битмапов
  81.         public void generate(object thread_data) {
  82.             int thread_id = (int)thread_data;
  83.            
  84.             for (int i = (thread_id * num_frames_on_thread); i < ((thread_id+1)*num_frames_on_thread); i++) {
  85.                 bitmaps[i] = new Bitmap(image_size, image_size);
  86.                
  87.                 for (int x = 0; x < image_size; x++) {
  88.                     for (int y = 0; y < image_size; y++) {
  89.                         double a = (x - image_size / 2) / (0.5 * image_size);
  90.                         double b = (y - image_size / 2) / (0.5 * image_size);
  91.                         Complex c = new Complex(tasks[i,0], tasks[i,1]);
  92.                         Complex z = new Complex(a, b);
  93.                        
  94.                         int j;
  95.                         for (j = 0; j < 300; j++) {
  96.                             j++;
  97.                             z.Square();
  98.                             z.Add(c);
  99.                             if (z.Magnitude() > 4) break;
  100.                         }
  101.                        
  102.                         bitmaps[i].SetPixel(x, y, Color.FromArgb((j * 6) % 255, (j * 2) % 255, (j * 4) % 255));
  103.                     }
  104.                 }
  105.             }
  106.         }
  107.        
  108.         public void animate() {
  109.             // ждем завершение генерации
  110.             for (int i = 0; i < bitmaps.Length; i++) {
  111.                 if (bitmaps[i] == null) {
  112.                     i--;
  113.                     Thread.Sleep(100);
  114.                 }
  115.             }
  116.            
  117.             System.Diagnostics.Debug.WriteLine("Генерация завершена");
  118.            
  119.             while (true) {
  120.                 for (int i = 0; i < bitmaps.Length; i++) {
  121.                     pictureBox1.Image = bitmaps[i];
  122.                     Thread.Sleep(ms_framerate);
  123.                 }
  124.             }
  125.         }
  126.        
  127.         void endAllThreads() {
  128.             try {
  129.                 for (int i = 0; i < threads.Length; i++) {
  130.                     threads[i].Abort();
  131.                 }
  132.             } catch (Exception) {}
  133.         }
  134.     }
  135. }
  136.  
  137. class Complex {
  138.     public double a; // Re
  139.     public double b; // Im
  140.  
  141.     public Complex(double a, double b) {
  142.         this.a = a;
  143.         this.b = b;
  144.     }
  145.    
  146.     public void Square() {
  147.         double temp = (a * a) - (b * b);
  148.         b = 2 * a * b;
  149.         a = temp;
  150.     }
  151.    
  152.     public double Magnitude() {
  153.         return (a * a) + (b * b);
  154.     }
  155.    
  156.     public void Add(Complex c) {
  157.         a += c.a;
  158.         b += c.b;
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement