pborawski

Frakral Manderbrota C#

May 14th, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.70 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.Windows.Forms;
  9.  
  10. namespace WindowsFormsApplication7
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         private int ile_iteracji, scaling;
  15.         double xr0,yr0, rszer, rwys;
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.             ile_iteracji = 10;
  20.             scaling = 2;
  21.             xr0 = -0.5;
  22.             yr0 = 0.0;
  23.             rszer = 3.0;
  24.             rwys = 3.0;
  25.            
  26.         }
  27.  
  28.         private void button2_Click(object sender, EventArgs e)
  29.         {
  30.             Close();
  31.         }
  32.         private void button1_Click(object sender, EventArgs e)
  33.         {
  34.             Rysuj();
  35.         }
  36.         private double Manderbrot(double a, double b)
  37.         {
  38.             const double inf = 3.0;
  39.             double x = 0, y = 0, xx;
  40.             int i;
  41.             for(i = 0; i < ile_iteracji; i++)
  42.             {
  43.                 xx = x * x - y * y + a;
  44.                 if (xx < -inf || xx > inf)
  45.                     break;
  46.                 y = 2 * x * y + b;
  47.                 if (y < -inf || y > inf)
  48.                     break;
  49.                 x = xx;
  50.             }
  51.             if (i == ile_iteracji)
  52.                 return 0;
  53.             else
  54.                 return (double)i / (double)ile_iteracji;
  55.         }
  56.         private void Rysuj()
  57.         {
  58.             int eszer = pictureBox1.Width;
  59.             int ewys = pictureBox1.Height;
  60.             int xe0 = 0, ye0 = 0;
  61.             double a, b, v;
  62.             int r, g, bb;
  63.            
  64.             pictureBox1.Image = new Bitmap(eszer, ewys);
  65.             Bitmap bmp = (Bitmap)pictureBox1.Image;
  66.  
  67.             Skala s = new Skala(xe0, ye0, eszer, ewys, xr0, yr0, rszer, rwys);
  68.  
  69.             for (int xe = xe0; xe < eszer; xe++)
  70.             {
  71.                 a = s.daj_real_x(xe);
  72.                 for (int ye = ye0; ye < ewys; ye++)
  73.                 {
  74.                     b = s.daj_real_y(ye);
  75.                     v = Manderbrot(a, b);
  76.                     r = Convert.ToInt32(v * 100);
  77.                     g = Convert.ToInt32(v * 200);
  78.                     bb = Convert.ToInt32(v * 250);
  79.                     bmp.SetPixel(xe, ye, Color.FromArgb(r,g,bb));
  80.                 }
  81.             }
  82.         }
  83.        
  84.         private void textBox2_TextChanged(object sender, EventArgs e)
  85.         {
  86.             scaling = Convert.ToInt32(textBox2.Text);
  87.         }
  88.  
  89.         private void textBox1_TextChanged(object sender, EventArgs e)
  90.         {
  91.             ile_iteracji = Convert.ToInt32(textBox1.Text);
  92.         }
  93.  
  94.        
  95.  
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment