Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Threading;
- namespace easy_code {
- public partial class MainForm : Form {
- public Bitmap[] bitmaps;//[номер кадра]
- public double[,] tasks;//[номер кадра,0(re)/1(im)]
- public Thread[] threads;
- public int image_size;
- public int num_frames_on_thread;
- public int ms_framerate;
- void MainFormFormClosing(object sender, FormClosingEventArgs e) {
- endAllThreads(); // ВАЖНО, а то процесс не убьется. Должно быть событие на самой форме
- }
- public MainForm() {
- InitializeComponent();
- //http://www.cyberforum.ru/windows-forms/thread110436.html#a_Q3
- System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
- endAllThreads();//если по кнопке заебошить - нужен вызов на всякий пожарный
- int initial_re = -3, final_re = 2;//не включительно
- int initial_im = -2, final_im = 1;//не включительно
- double step_re_and_im = 0.05;
- image_size = 300;
- ms_framerate = 33;
- int num_threads = 2;
- double sum_re = final_re - initial_re;
- double sum_im = final_im - initial_im;
- int num_frames = Convert.ToInt32(Math.Floor(sum_re / step_re_and_im));
- if (sum_re < sum_im) {
- num_frames = Convert.ToInt32(Math.Floor(sum_im / step_re_and_im));
- }
- num_frames_on_thread = num_frames / num_threads;
- num_frames = num_frames_on_thread * num_threads; // реальное кол-во
- // генерация задания
- tasks = new double[num_frames,2];
- for (int i = 0; i < num_frames; i++) {
- double re = initial_re + sum_re / num_frames * i;
- double im = initial_im + sum_im / num_frames * i;
- tasks[i,0] = re;
- tasks[i,1] = im;
- //System.Diagnostics.Debug.WriteLine("Кадр №" + i.ToString() + ": re=" + re.ToString() + " im=" + im.ToString());
- }
- // запуск потоков
- bitmaps = new Bitmap[num_frames];
- threads = new Thread[num_threads + 1];// +1 !!!!!!
- for (int i = 0; i < num_threads; i++) {
- threads[i] = new Thread(new ParameterizedThreadStart(generate));
- threads[i].Start(i);
- }
- // запуск анимации
- threads[num_threads] = new Thread(animate);
- threads[num_threads].Start();
- }
- // ПОТОК генерации битмапов
- public void generate(object thread_data) {
- int thread_id = (int)thread_data;
- for (int i = (thread_id * num_frames_on_thread); i < ((thread_id+1)*num_frames_on_thread); i++) {
- bitmaps[i] = new Bitmap(image_size, image_size);
- for (int x = 0; x < image_size; x++) {
- for (int y = 0; y < image_size; y++) {
- double a = (x - image_size / 2) / (0.5 * image_size);
- double b = (y - image_size / 2) / (0.5 * image_size);
- Complex c = new Complex(tasks[i,0], tasks[i,1]);
- Complex z = new Complex(a, b);
- int j;
- for (j = 0; j < 300; j++) {
- j++;
- z.Square();
- z.Add(c);
- if (z.Magnitude() > 4) break;
- }
- bitmaps[i].SetPixel(x, y, Color.FromArgb((j * 6) % 255, (j * 2) % 255, (j * 4) % 255));
- }
- }
- }
- }
- public void animate() {
- // ждем завершение генерации
- for (int i = 0; i < bitmaps.Length; i++) {
- if (bitmaps[i] == null) {
- i--;
- Thread.Sleep(100);
- }
- }
- System.Diagnostics.Debug.WriteLine("Генерация завершена");
- while (true) {
- for (int i = 0; i < bitmaps.Length; i++) {
- pictureBox1.Image = bitmaps[i];
- Thread.Sleep(ms_framerate);
- }
- }
- }
- void endAllThreads() {
- try {
- for (int i = 0; i < threads.Length; i++) {
- threads[i].Abort();
- }
- } catch (Exception) {}
- }
- }
- }
- class Complex {
- public double a; // Re
- public double b; // Im
- public Complex(double a, double b) {
- this.a = a;
- this.b = b;
- }
- public void Square() {
- double temp = (a * a) - (b * b);
- b = 2 * a * b;
- a = temp;
- }
- public double Magnitude() {
- return (a * a) + (b * b);
- }
- public void Add(Complex c) {
- a += c.a;
- b += c.b;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement