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;
namespace Lab_8
{
public partial class Form1 : Form
{
#region
public Form1()
{
InitializeComponent();
}
#endregion
Graphics g;
#region Очистка экрана
private void button1_Click(object sender, EventArgs e)
{
Refresh();
}
#endregion
#region Треугольник Серпинского
// http://www.fractalworld.xaoc.ru/Sierpinski_triangle
private void button2_Click(object sender, EventArgs e)
{
int N = Convert.ToInt32(textBox1.Text);
Serpinsky(N);
}
private void Serpinsky(int Iter)
{
g = Graphics.FromHwnd(Handle);
Point[] p = new Point[3];
p[0] = new Point(600, 10);
p[1] = new Point(300, 650);
p[2] = new Point(900, 650);
int N = 0;
Random r = new Random();
Point newPt;
int i = r.Next(3);//берем случайную из трех
Point pp = p[i];
if (Iter == 0)
{
return;
}
else if (Iter < 0)
{
return;
}
do
{
Pen pen = new Pen(Color.Black);
g.DrawRectangle(pen, pp.X, pp.Y, 1, 1);
i = r.Next(3);
newPt = new Point((pp.X + p[i].X) / 2, (pp.Y + p[i].Y) / 2); // середина между двумя точками
pp = newPt;
N++;
}
while (N != Iter);
}
#endregion
#region Кривая Коха
// http://fractalworld.xaoc.ru/Koch_curve
private void button3_Click(object sender, EventArgs e)
{
int N = Convert.ToInt32(textBox1.Text);
Koch(N);
}
private void Koch(int Iter)
{
g = Graphics.FromHwnd(Handle);
Random r = new Random();
Point m = new Point(10, 650);
int N = 0, rad = 1000;
double x = 0, y = 0, p = 0, t = 0;
if (Iter == 0)
{
return;
}
else if (Iter < 0)
{
return;
}
do
{
p = r.NextDouble(); // от 0 - 1
t = x; // координата по x
if (p <= 0.5) // типо середина
{
x = 0.5 * x + 1 / (2 * Math.Pow(3, 0.5)) * y;
y = 1 / (2 * Math.Pow(3, 0.5)) * t - 0.5 * y;
}
else
{
x = 0.5 * x - 1 / (2 * Math.Sqrt(3)) * y + 0.5;
y = -1 / (2 * Math.Sqrt(3)) * t - 0.5 * y + 1 / (2 * Math.Sqrt(3));
}
N++;
g.DrawRectangle(new Pen(Color.Black), (float)(m.X + Math.Round(rad * x)), (float)(m.Y - Math.Round(rad * y)), 1, 1);
} while (N != Iter);
}
#endregion
#region Пустыня
private void button4_Click(object sender, EventArgs e)
{
int N = Convert.ToInt32(textBox1.Text);
int x = Convert.ToInt32(textBox2.Text);
int y = Convert.ToInt32(textBox3.Text);
CantorDust(N, x, y);
}
private void CantorDust(int Iter, int a, int b)
{
g = Graphics.FromHwnd(this.Handle);
Point[] p = new Point[4];
int N = 0;
p[0] = new Point(50, 50);
p[1] = new Point(50, 900);
p[2] = new Point(900, 50);
p[3] = new Point(900, 900);
Random r = new Random();
Point newPt;
int i = r.Next(4);
Point pp = p[i];
if (Iter == 0 || Iter < 0)
{
return;
}
if (a < 2 || a > 3 || b > 3 || b < 2)
{
return;
}
do
{
Pen pen = new Pen(Color.Black);
g.DrawRectangle(pen, pp.X, pp.Y, 1, 1);
i = r.Next(4);
newPt = new Point((pp.X + p[i].X) / a, (pp.Y + p[i].Y) / b);
pp = newPt;
N++;
} while (N != Iter);
}
#endregion
#region Папоротник
// http://fractalworld.xaoc.ru/Fern
private void button5_Click(object sender, EventArgs e)
{
int N = Convert.ToInt32(textBox1.Text);
ListFern(N);
}
private void ListFern(int N)
{
Pen p = new Pen(Color.Green);
g = Graphics.FromHwnd(Handle);
Random rnd = new Random();
int W = 500; // зум
int kmax = 20; // четкость
if (N == 0 || N < 0)
{
return;
}
for (int i = 0; i < N; i++)
{
float x = (float)(rnd.NextDouble() * 0.5);// рандом координат
float y = (float)(rnd.NextDouble() * 0.5);
double nx, ny;
for (int k = 0; k < kmax; k++)
{
double dice = rnd.NextDouble();
if (dice < 0.01)
{
nx = 0.0;
ny = 0.16 * y;
}
else if (dice < 0.86)
{
nx = 0.85 * x + 0.04 * y;
ny = -0.04 * x + 0.85 * y + 1.6;
}
else if (dice < 0.93)
{
nx = 0.2 * x - 0.26 * y;
ny = 0.23 * x + 0.22 * y + 1.6;
}
else
{
nx = -0.15 * x + 0.28 * y;
ny = 0.26 * x + 0.24 * y + 0.44;
}
x = (float)nx;
y = (float)ny;
}
g.DrawRectangle(p, (float)(W - (5.0 - x) * W / 10.0), (float)((10.0 - y) * W / 10.0), 0.5f, 0.5f);
}
}
#endregion
#region KamTorus
private void button6_Click(object sender, EventArgs e)
{
int N = Convert.ToInt32(textBox1.Text);
if (N >= 36)
{
return;
}
else
KamTorus(N);
}
private void KamTorus(int Iter)
{
// http://www.opita.net/node/184
g = Graphics.FromHwnd(Handle);
long a, nx, ny;
double an, can, san, can1, san1, ax, ay;
double x, xa, x1, x2, x3, y, y1, y2, y3, rand1, rand2;
// находим координаты середины окна
nx = this.Width / 2;
ny = this.Height / 2;
// начальное значение. от куда начинаем
ax = 600.0;
ay = ax;
// рандомим
Random ran = new Random();
rand1 = ran.Next(20000);
rand2 = ran.Next(20000);
rand1 = 50000 - 5 * rand1;
rand2 = 50000 - 5 * rand2;
//еще рандомим значения, чтобы увеличить шанс выпадения случайной величины
an = 10.0 * (rand1 - rand2); // ищим разницу
// вычисление синусов и косинусов для поворотов
can = 0.99 * Math.Cos(an);
san = 0.99 * Math.Sin(an);
can1 = 1.01 * Math.Cos(an);
san1 = 1.01 * Math.Sin(an);
//начальные значения относительно которых рисуем внешнее ядро
x3 = 0.01;
y3 = 0.01;
if (Iter == 0) return;
do
{
//для внешней части
xa = x3 * x3 - y3;
x2 = x3 * can1 + xa * san1;
y2 = x3 * san1 - xa * can1;
x3 = x2;
y3 = y2;
x = x2;
y = y2;
a = 0;
// внутренняя
do
{
Pen pen = new Pen(Color.Coral);
// рисуем орбиту
// вычисляем координаты текущей точки
xa = x * x - y;
x1 = x * can + xa * san;
y1 = x * san - xa * can;
x = x1;
y = y1;
a++;
g.DrawLine(pen, (int)Math.Round(ax * x + nx), (int)Math.Round(ay * y + ny), (int)Math.Round(ax * x + nx) - 1, (int)Math.Round(ay * y + ny) + 1);
} while ((Math.Abs(x1) <= Iter * Iter) && (Math.Abs(y1) <= Iter * Iter) && a <= Iter); //разброс
} while ((Math.Abs(x2) <= Iter) && (Math.Abs(y2) <= Iter)); // разброс
}
#endregion
}
}