Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.24 KB | None | 0 0
  1. using System.Windows.Forms;
  2. using System.Drawing;
  3. using System;
  4.  
  5. namespace Mandelbrot
  6. {
  7.     static class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             Application.Run(new Scherm());
  12.         }
  13.  
  14.         class Scherm : Form
  15.         {
  16.             // Declaraties
  17.             public TextBox midxtext;
  18.             public TextBox midytext;
  19.             public TextBox maxtext;
  20.             public TextBox schaaltext;
  21.             public Label middenx;
  22.             public Label middeny;
  23.             public Label schaal;
  24.             public Label max;
  25.             public Button okbutton;
  26.             public Button resetbutton;
  27.             public Label check;
  28.             public bool getekend = false;
  29.             public Bitmap bitmapscherm;
  30.  
  31.             public Scherm()
  32.             {
  33.                 // Nieuwe objecten aanmaken (soort van)
  34.                 this.midxtext = new TextBox();
  35.                 this.midytext = new TextBox();
  36.                 this.maxtext = new TextBox();
  37.                 this.schaaltext = new TextBox();
  38.                 this.middenx = new Label();
  39.                 this.middeny = new Label();
  40.                 this.schaal = new Label();
  41.                 this.max = new Label();
  42.                 this.okbutton = new Button();
  43.                 this.resetbutton = new Button();
  44.                 this.check = new Label();
  45.                 this.bitmapscherm = new Bitmap(400, 400);
  46.                 this.BackColor = Color.Black;
  47.  
  48.                 /***************/
  49.                 //Eigenschappen//
  50.                 /***************/
  51.  
  52.  
  53.                 // Textbox: midxtext
  54.                 this.midxtext.Location = new Point(72, 6);
  55.                 this.midxtext.Name = "midxtext";
  56.                 this.midxtext.Size = new Size(90, 20);
  57.                 this.midxtext.Text = "0"; ;
  58.  
  59.                 // Textbox: midytext
  60.                 this.midytext.Location = new Point(72, 32);
  61.                 this.midytext.Name = "midytext";
  62.                 this.midytext.Size = new Size(90, 20);
  63.                 this.midytext.TabIndex = 1;
  64.                 this.midytext.Text = "0";
  65.  
  66.                 // Textbox: maxtext
  67.                 this.maxtext.Location = new Point(293, 32);
  68.                 this.maxtext.Name = "maxtext";
  69.                 this.maxtext.Size = new Size(52, 20);
  70.                 this.maxtext.TabIndex = 2;
  71.                 this.maxtext.Text = "100";
  72.  
  73.                 // Textbox: schaaltext
  74.                 this.schaaltext.Location = new Point(293, 6);
  75.                 this.schaaltext.Name = "schaaltext";
  76.                 this.schaaltext.Size = new Size(120, 20);
  77.                 this.schaaltext.TabIndex = 3;
  78.                 this.schaaltext.Text = "0,01";
  79.  
  80.                 // Label: middenx
  81.                 this.middenx.AutoSize = true;
  82.                 this.middenx.Location = new Point(12, 9);
  83.                 this.middenx.Name = "middenx";
  84.                 this.middenx.Size = new Size(55, 13);
  85.                 this.middenx.TabIndex = 4;
  86.                 this.middenx.Text = "Midden X:";
  87.                 this.middenx.ForeColor = Color.White;
  88.                 this.middenx.BackColor = Color.Black;
  89.  
  90.                 // Label: middeny
  91.                 this.middeny.AutoSize = true;
  92.                 this.middeny.Location = new Point(12, 35);
  93.                 this.middeny.Name = "middeny";
  94.                 this.middeny.Size = new Size(55, 13);
  95.                 this.middeny.TabIndex = 5;
  96.                 this.middeny.Text = "Midden Y:";
  97.                 this.middeny.ForeColor = Color.White;
  98.                 this.middeny.BackColor = Color.Black;
  99.  
  100.                 // Label: schaal
  101.                 this.schaal.AutoSize = true;
  102.                 this.schaal.Location = new Point(244, 9);
  103.                 this.schaal.Name = "schaal";
  104.                 this.schaal.Size = new Size(43, 13);
  105.                 this.schaal.TabIndex = 6;
  106.                 this.schaal.Text = "Schaal:";
  107.                 this.schaal.ForeColor = Color.White;
  108.                 this.schaal.BackColor = Color.Black;
  109.  
  110.                 // Label: max
  111.                 this.max.AutoSize = true;
  112.                 this.max.Location = new Point(244, 32);
  113.                 this.max.Name = "max";
  114.                 this.max.Size = new Size(30, 13);
  115.                 this.max.TabIndex = 7;
  116.                 this.max.Text = "Max:";
  117.                 this.max.ForeColor = Color.White;
  118.                 this.max.BackColor = Color.Black;
  119.  
  120.                 // Button: OK
  121.                 this.okbutton.Location = new Point(351, 29);
  122.                 this.okbutton.Name = "okbutton";
  123.                 this.okbutton.Size = new Size(62, 23);
  124.                 this.okbutton.TabIndex = 8;
  125.                 this.okbutton.Text = "OK";
  126.                 this.okbutton.UseVisualStyleBackColor = true;
  127.                 this.okbutton.BackColor = Color.White;
  128.  
  129.                 // Button: Reset
  130.                 this.resetbutton.Location = new Point(13, 480);
  131.                 this.resetbutton.Name = "resetbutton";
  132.                 this.resetbutton.Size = new Size(62, 23);
  133.                 this.resetbutton.TabIndex = 9;
  134.                 this.resetbutton.Text = "Reset";
  135.                 this.resetbutton.UseVisualStyleBackColor = true;
  136.                 this.resetbutton.BackColor = Color.White;
  137.  
  138.                 /***********************/
  139.                 // Einde eigenschappen //
  140.                 /***********************/
  141.  
  142.                 this.ClientSize = new Size(425, 530);
  143.                 this.Text = "Mandelbrot";
  144.  
  145.                 // Events
  146.                 this.Paint += this.bouwScherm;
  147.                 this.okbutton.Click += this.okklik;
  148.                 this.resetbutton.Click += this.resetklik;
  149.                 this.MouseClick += this.zoomin;
  150.  
  151.  
  152.                 // Controls.Add
  153.                 this.Controls.Add(this.okbutton);
  154.                 this.Controls.Add(this.resetbutton);
  155.                 this.Controls.Add(this.max);
  156.                 this.Controls.Add(this.schaal);
  157.                 this.Controls.Add(this.middeny);
  158.                 this.Controls.Add(this.middenx);
  159.                 this.Controls.Add(this.schaaltext);
  160.                 this.Controls.Add(this.maxtext);
  161.                 this.Controls.Add(this.midytext);
  162.                 this.Controls.Add(this.midxtext);
  163.                 this.Controls.Add(this.check);
  164.  
  165.  
  166.                 //Enter = knop indrukken
  167.                 this.AcceptButton = okbutton;
  168.                 Invalidate();
  169.             }
  170.  
  171.             public void okklik(object o, EventArgs e)
  172.             {
  173.                 Invalidate();
  174.             }
  175.  
  176.             public void resetklik(object o, EventArgs e)
  177.             {
  178.                 midxtext.Text = "0";
  179.                 midytext.Text = "0";
  180.                 schaaltext.Text = "0,01";
  181.                 maxtext.Text = "100";
  182.                 Invalidate();
  183.             }
  184.  
  185.  
  186.             public void zoomin(object o, MouseEventArgs e)
  187.             {
  188.                 if (e.X >= 13 && e.Y >= 69 && e.X <= 413 && e.Y <= 469)
  189.                 {
  190.                     double midx = double.Parse(midxtext.Text);
  191.                     double midy = double.Parse(midytext.Text);
  192.                     double schaal = double.Parse(schaaltext.Text);
  193.                     midx = (midx - 213 * schaal) + (e.X * schaal);
  194.                     midy = (midy + 269 * schaal) - (e.Y * schaal);
  195.                     schaal = schaal / 2;
  196.                     this.schaaltext.Text = schaal.ToString();
  197.                     this.midxtext.Text = midx.ToString();
  198.                     this.midytext.Text = midy.ToString();
  199.                     Invalidate();
  200.                 }
  201.             }
  202.  
  203.             public void bouwScherm(object o, PaintEventArgs pea)
  204.             {
  205.                 double xxx = double.Parse(midxtext.Text);
  206.                 double yyy = double.Parse(midytext.Text);
  207.                 int x = 0;
  208.                 int y = 0;
  209.                 double schaal = double.Parse(schaaltext.Text);
  210.                 pea.Graphics.DrawImage(bitmapscherm, 13, 69, 400, 400);
  211.                 while (y <= 399)
  212.                 {
  213.                     double y2 = (yyy + 200 * schaal) - y * schaal;
  214.                     while (x <= 399)
  215.                     {
  216.                         double x2 = (xxx - 200 * schaal) + x * schaal;
  217.                         int mandelgetal = mandelFormule(x2, y2);
  218.                         bitmapscherm.SetPixel(x, y, Color.FromArgb(2 * mandelgetal % 255, 3 * mandelgetal % 255, mandelgetal % 255));
  219.                         x++;
  220.                     }
  221.                     x = 0;
  222.                     y++;
  223.                     pea.Graphics.DrawImage(bitmapscherm, 13, 69, 400, 400);
  224.                 }
  225.             }
  226.  
  227.             private int mandelFormule(double x, double y)
  228.             {
  229.                 double a1 = 0.0;
  230.                 double a = 0.0;
  231.                 double b = 0.0;
  232.                 double pyth = 0.0;
  233.                 int teller = 0;
  234.                 double max = int.Parse(maxtext.Text);
  235.  
  236.                 while (pyth <= 2 && teller < max)
  237.                 {
  238.                     a1 = a * a - b * b + x;
  239.                     b = 2 * a * b + y;
  240.                     a = a1;
  241.                     pyth = Math.Sqrt(a * a + b * b);
  242.                     teller++;
  243.                 }
  244.                 return teller;
  245.             }
  246.         }
  247.     }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement