Кривая Госпера - C#
Формулировка задачи:
Листинг программы
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- *
- namespace Gosper_curve
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- Graphics g;
- Pen pen = new Pen(Color.Black);
- *
- void Draw(double x, double y, double l, double u, int t, int q)
- {
- // начало построения ломанных
- if (t > 0)
- {
- if (q == 1)
- {
- //формулы построения
- x += l * Math.Cos(u);
- y -= l * Math.Sin(u);
- u += Math.PI;
- }
- u -= 2 * Math.PI / 19;//соединение линий
- l /= Math.Sqrt(7); //масштаб
- //функции рисования
- Paint(ref x, ref y, l, u, t - 1, 0);
- Paint(ref x, ref y, l, u + Math.PI / 3, t - 1, 1);
- Paint(ref x, ref y, l, u + Math.PI, t - 1, 1);
- Paint(ref x, ref y, l, u + 2 * Math.PI / 3, t - 1, 0);
- Paint(ref x, ref y, l, u, t - 1, 0);
- Paint(ref x, ref y, l, u, t - 1, 0);
- Paint(ref x, ref y, l, u - Math.PI / 3, t - 1, 1);
- }
- else g.DrawLine(pen, (float)Math.Round(x), (float)Math.Round(y), (float)Math.Round(x + Math.Cos(u) * l), (float)Math.Round(y - Math.Sin(u) * l));
- }
- *
- void Paint(ref double x, ref double y, double l, double u, int t, int q)
- {
- Draw(x, y, l, u, t, q);
- x += l * Math.Cos(u);
- y -= l * Math.Sin(u);
- }
- *
- private void button1_Click(object sender, EventArgs e)
- {
- g = Graphics.FromHwnd(pictureBox1.Handle);
- Draw(150, 350, 300, 0, 3, 0);
- }
- }
- }
Решение задачи: «Кривая Госпера»
textual
Листинг программы
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- protected override void OnPaint(PaintEventArgs e)
- {
- e.Graphics.TranslateTransform(230, 100);
- Draw(e.Graphics, "A", 3, 60, new Dictionary<char, string>()
- {
- ['A'] = "A-B--B+A++AA+B-",
- ['B'] = "+A-BB--B-A++A+B"
- });
- }
- void Draw(Graphics g, string rule, int level, int angle, Dictionary<char, string> rules)
- {
- // Если дошли до нулевого уровня то рисуем линию и выходим
- if (level < 0)
- {
- g.DrawLine(Pens.Black, 0, 0, 0, 10);
- g.TranslateTransform(0, 10);
- return;
- }
- // Иначе, следуя правилу,
- foreach (var r in rule)
- {
- switch(r)
- {
- // поворачиваемся
- case '-': g.RotateTransform(+angle); break;
- case '+': g.RotateTransform(-angle); break;
- // или рекурсивно запускаем отрисовку правила из словаря
- default: Draw(g, rules[r], level - 1, angle, rules); break;
- }
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д