Hey,
Ik heb weer een vraagje. Ditmaals ben ik zelf heel dicht bij het antwoord, maar echt het laatste stapje lukt niet.
Ik probeer een mandelbrot te maken, dat is allemaal gelukt. Nu probeer ik in te zoomen met de muisklik. Ook dat lukt (bijna), want de waarden worden wel aangepast in de textbox etc, maar hij zoomt niet in. pas als ik op ok druk.
Ziet iemand waarom hij niet inzoomt? want volgens mij doe ik gewoon alles goed.
Mvg,
William
Ik heb weer een vraagje. Ditmaals ben ik zelf heel dicht bij het antwoord, maar echt het laatste stapje lukt niet.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Mandelbrot
{
public partial class Form1 : Form
{
Bitmap MandelbrotBitmap = new Bitmap(400, 400);
int maxIterations = 0;
double scale;
double omzettenXBegin;
double omzettenYBegin;
public Form1()
{
InitializeComponent();
okButton.Click += drawMandel;
pictureBox1.Invalidate();
}
private void Form1_Load(object o, EventArgs e)
{
pictureBox1.MouseClick += pictureBox1_Click;
}
public void drawMandel(object o, EventArgs e)
{
//X en Y coördinaten
double mandelX;
double mandelY;
middenY.Text = middenY.Text.Replace(".", ",");
middenX.Text = middenX.Text.Replace(".", ",");
omzettenYBegin = double.Parse(middenY.Text);
omzettenXBegin = double.Parse(middenX.Text);
schaal.Text = schaal.Text.Replace(".", ",");
scale = double.Parse(schaal.Text);
maxIterations = int.Parse(max.Text);
for (int x = 0; x < MandelbrotBitmap.Width; x++)
{
for (int y = 0; y < MandelbrotBitmap.Height; y++)
{
mandelX = ((x - (MandelbrotBitmap.Width / 2)) * scale) + omzettenXBegin;
mandelY = ((y - (MandelbrotBitmap.Height / 2)) * scale) + omzettenYBegin;
int mandel = berekenMandel(mandelX, mandelY, maxIterations);
if (mandel % 2 != 0)
{
MandelbrotBitmap.SetPixel(x, y, Color.White);
}
else
{
MandelbrotBitmap.SetPixel(x, y, Color.Black);
}
}
}
pictureBox1.Image = MandelbrotBitmap;
pictureBox1.Invalidate();
}
public int berekenMandel(double x, double y, int maxIterations)
{
double a = 0; // Zetten van vars
double b = 0;
double mandelGetal = 0;
int iteration;
for (iteration = 0; iteration < maxIterations; iteration++)
{
double newA = a * a - b * b + x; // Waarde in tijdelijke double zetten
b = 2 * a * b + y; // B uitrekenen met orginele A
a = newA; // A naar nieuwe waarde zetten
mandelGetal = a * a + b * b; // Pytagoras
if (mandelGetal > 4)
{
return iteration; // Waarde is groter dan 4, dus gelukt!
}
}
return 0;
}
private void pictureBox1_Click(object o, MouseEventArgs mea)
{
int muisLocatieX = mea.X;
int muisLocatieY = mea.Y;
double nieuwOmzettenXBegin = (mea.X - (MandelbrotBitmap.Width / 2)) * scale + omzettenXBegin;
double nieuwOmzettenYBegin = (mea.Y - (MandelbrotBitmap.Height / 2)) * scale + omzettenYBegin;
scale = scale / 2;
schaal.Text = scale.ToString();
middenX.Text = nieuwOmzettenXBegin.ToString();
middenY.Text = nieuwOmzettenYBegin.ToString();
pictureBox1.Invalidate();
}
}
}
Ik probeer een mandelbrot te maken, dat is allemaal gelukt. Nu probeer ik in te zoomen met de muisklik. Ook dat lukt (bijna), want de waarden worden wel aangepast in de textbox etc, maar hij zoomt niet in. pas als ik op ok druk.
Ziet iemand waarom hij niet inzoomt? want volgens mij doe ik gewoon alles goed.
Mvg,
William