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;
- namespace koh
- {
- public partial class Emulator : Form
- {
- static Pen pen1;
- static Graphics g;
- static Pen pen2;
- public Emulator()
- {
- InitializeComponent();
- }
- private void Draw(object sender, EventArgs e)
- {
- g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
- //Объявляем объект "g" класса Graphics
- g = CreateGraphics();
- g.Clear(Color.Black);//Зарисовка экрана черным фоном
- //Определим коорднаты исходного треугольника
- var point1 = new PointF(200, 200);
- var point2 = new PointF(500, 200);
- var point3 = new PointF(350, 400);
- for (int d = 15; d <= 175; ++d)
- {
- Random rnd = new Random();
- //Выбираем цвета зарисовки
- for (int m = 1; m < 5; m++) {
- Pen pen1a = new Pen(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255), rnd.Next(255)), m);
- float x1, x2, x3, y1, y2, y3;
- x1 = 450; x1 -= d;
- y1 = 400; y1 -= d;
- x2 = 50; x2 += d;
- y2 = 400; y2 -= d;
- x3 = 250;
- y3 = 50; y3 += d;
- var point1a = new PointF(x1, y1);
- var point2a = new PointF(x2, y2);
- var point3a = new PointF(x3, y3);
- g.DrawLine(pen1a, point1a, point2a);
- g.DrawLine(pen1a, point2a, point3a);
- g.DrawLine(pen1a, point3a, point1a);
- for (int n = 0; n < 5;n++ )
- {
- Fractal(point1a, point2a, point3a, n);
- Fractal(point2a, point3a, point1a, n);
- Fractal(point3a, point1a, point2a, n);
- }
- }}
- }
- //рекурсивная функция рисования кривой Коха
- static int Fractal(PointF p1, PointF p2, PointF p3, int iter)
- {
- //n -количество итераций
- if (iter > 0) //условие выхода из рекурсии
- {
- g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
- Random rnd = new Random();
- Pen pen2a = new Pen(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255), rnd.Next(255)), 1);
- //средняя треть отрезка
- var p4 = new PointF((p2.X + 2 * p1.X) / 3, (p2.Y + 2 * p1.Y) / 3) ;
- var p5 = new PointF((2 * p2.X + p1.X) / 3, (p1.Y + 2 * p2.Y) / 3) ;
- //координаты вершины угла
- var ps = new PointF((p2.X + p1.X) / 2, (p2.Y + p1.Y) / 2) ;
- var pn = new PointF((4 * ps.X - p3.X) / 3, (4 * ps.Y - p3.Y) / 3) ;
- //рисуем его
- g.DrawLine(pen2a, p4, pn);
- g.DrawLine(pen2a, p5, pn);
- g.DrawLine(pen2a, p4, p5);
- //рекурсивно вызываем функцию нужное число раз
- Fractal(p4, pn, p5, iter - 1);
- Fractal(pn, p5, p4, iter - 1);
- Fractal(p1, p4, new PointF((2 * p1.X + p3.X) / 3, (2 * p1.Y + p3.Y) / 3), iter - 1);
- Fractal(p5, p2, new PointF((2 * p2.X + p3.X) / 3, (2 * p2.Y + p3.Y) / 3), iter - 1);
- }
- return iter;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement