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; using System.IO; namespace m13 { public partial class MainForm : Form { public MainForm() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if (openFileDialog.ShowDialog() == DialogResult.OK) { pathTextBox.Text = openFileDialog.FileName; } } private int laststep = 0; private int tvsteps = 0; private void Render(int steps) { int s = 0; Bitmap bmp = new Bitmap(700, 700); Graphics g = Graphics.FromImage(bmp); g.Clear(Color.White); foreach (ZLine ln in commands) { ++s; g.DrawLine(new Pen(ln.c, ln.pen), 350 + ln.p1.X, 350 - ln.p1.Y, 350 + ln.p2.X, 350 - ln.p2.Y); if (s == steps) break; } g.Flush(); render.Image = bmp; } private void run_Click(object sender, EventArgs e) { if (!File.Exists(pathTextBox.Text)) { MessageBox.Show("Soubor neexistuje!", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //try { commands = ParseCommands(pathTextBox.Text); if (activateTV.Checked) { tvsteps = 1; Render(1); } else { tvsteps = 0; Render((int)(numOfSteps.Value)); } } /*catch { MessageBox.Show("Neznámá chyba!", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error); }/*/ } private struct ZLine { public Point p1; public Point p2; public Color c; public int pen; } private class commandInfo { virtual public int Type() { return 0; } } private class repeatInfo : commandInfo { public int n; public Int64 pos; override public int Type() { return 1; } } private class ifInfo : commandInfo { override public int Type() { return 2; } } private List commands = new List(); private List ParseCommands(string filename) { List ret = new List(); int angle = 90; double posx = 0; double posy = 0; int pen = 0; Color color = Color.Black; string func = ""; string numb = ""; bool justNumbers = false; Stack repeatStack = new Stack(); Byte[] byteArray = new Byte[1]; FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs); try { while (sr.BaseStream.Position < sr.BaseStream.Length) { sr.BaseStream.Read(byteArray, 0, 1); char c = (char)(byteArray[0]); if (c == '}') { commandInfo ri = repeatStack.Pop(); switch (ri.Type()) { case 1: if ((ri as repeatInfo).n > 0) { --(ri as repeatInfo).n; sr.BaseStream.Position = (ri as repeatInfo).pos; repeatStack.Push(ri); } break; case 2: break; } } if (justNumbers) { if (c == ')') { justNumbers = false; int n; switch (func) { case "left": n = int.Parse(numb); angle += n; if (angle < 0) angle += 360; if (angle >= 360) angle -= 360; break; case "right": n = int.Parse(numb); angle -= n; if (angle < 0) angle += 360; if (angle >= 360) angle -= 360; break; case "pen": n = int.Parse(numb); pen = n; break; case "forward": n = int.Parse(numb); double nposx = posx; double nposy = posy; nposx += (Math.Cos((angle * Math.PI) / 180) * n); nposy += (Math.Sin((angle * Math.PI) / 180) * n); if (pen > 0) { ZLine ln = new ZLine(); ln.p1 = new Point((int)posx, (int)posy); ln.p2 = new Point((int)nposx, (int)nposy); ln.c = color; ln.pen = pen; ret.Add(ln); } posx = nposx; posy = nposy; break; case "color": string[] colors = numb.Split(','); int r = int.Parse(colors[0]); int g = int.Parse(colors[1]); int b = int.Parse(colors[2]); if (r < 0) r = 0; if (g < 0) g = 0; if (b < 0) b = 0; if (r > 255) r = 255; if (g > 255) g = 255; if (b > 255) b = 255; color = Color.FromArgb(r, g, b); break; case "repeat": n = int.Parse(numb); while (sr.BaseStream.Position < sr.BaseStream.Length) { sr.BaseStream.Read(byteArray, 0, 1); if (byteArray[0] == '{') break; } repeatInfo ri = new repeatInfo(); ri.n = n; ri.pos = sr.BaseStream.Position; if (ri.n <= 0) { int numofs = 1; while ((sr.BaseStream.Position < sr.BaseStream.Length) && (numofs != 0)) { sr.BaseStream.Read(byteArray, 0, 1); char tmpc = (char)(byteArray[0]); if (tmpc == '{') ++numofs; else if (tmpc == '}') --numofs; } } else repeatStack.Push(ri); break; case "if": n = int.Parse(numb); while (sr.BaseStream.Position < sr.BaseStream.Length) { sr.BaseStream.Read(byteArray, 0, 1); if (byteArray[0] == '{') break; } if (n <= 0) { int numofs = 1; while ((sr.BaseStream.Position < sr.BaseStream.Length) && (numofs != 0)) { sr.BaseStream.Read(byteArray, 0, 1); char tmpc = (char)(byteArray[0]); if (tmpc == '{') ++numofs; else if (tmpc == '}') --numofs; } } else { repeatStack.Push(new ifInfo()); } break; } func = ""; numb = ""; } else if (((c >= '0') && (c <= '9')) || (c == '-') || (c == ',')) numb += c; } else { if ((c >= 'a') && (c <= 'z')) func += c; else if (c == '(') justNumbers = true; } } } finally { sr.Close(); fs.Close(); } return ret; } private void activateTV_CheckStateChanged(object sender, EventArgs e) { if (activateTV.Checked) Render(1); else Render((int)(numOfSteps.Value)); } private void prev_Click(object sender, EventArgs e) { if (!activateTV.Checked) return; if (--tvsteps < 1) tvsteps = 1; Render(tvsteps); label3.Text = "Televizní přenos (krok " + tvsteps.ToString() + "): "; } private void next_Click(object sender, EventArgs e) { if (!activateTV.Checked) return; if (++tvsteps >= commands.Count) tvsteps = commands.Count - 1; Render(tvsteps); label3.Text = "Televizní přenos (krok " + tvsteps.ToString() + "): "; } } }