Free cookie consent management tool by TermsFeed Policy Generator

source: branches/crossvalidation-2434/HeuristicLab.Problems.GeneticProgramming.Views/3.3/LawnMower/SolutionView.cs @ 14419

Last change on this file since 14419 was 12911, checked in by gkronber, 9 years ago

#2472: created project structure for HeuristicLab.Problems.GeneticProgramming and added implementations of ArtificialAnt and LawnMower

File size: 2.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Drawing;
24using System.Windows.Forms;
25using HeuristicLab.Core.Views;
26using HeuristicLab.MainForm;
27using HeuristicLab.Problems.GeneticProgramming.LawnMower;
28
29namespace HeuristicLab.Problems.GeneticProgramming.Views.LawnMower {
30  [View("Lawn Mower View")]
31  [Content(typeof(Solution), IsDefaultView = true)]
32  public sealed partial class SolutionView : NamedItemView {
33    public new Solution Content {
34      get { return (Solution)base.Content; }
35      set { base.Content = value; }
36    }
37
38    public SolutionView()
39      : base() {
40      InitializeComponent();
41      pictureBox.Image = new Bitmap(200, 200);
42    }
43
44    protected override void OnContentChanged() {
45      base.OnContentChanged();
46      if (Content == null) {
47        using (var g = Graphics.FromImage(pictureBox.Image))
48          g.Clear(DefaultBackColor);
49      } else {
50        bool[,] lawn = Interpreter.EvaluateLawnMowerProgram(Content.Length, Content.Width, Content.Tree);
51        PaintTiles(pictureBox.Image, lawn);
52      }
53    }
54
55    private void PaintTiles(Image lawnImage, bool[,] mowed) {
56      int w = lawnImage.Width;
57      int h = lawnImage.Height;
58
59      float tileHeight = h / (float)mowed.GetLength(0);
60      float tileWidth = w / (float)mowed.GetLength(1);
61
62      tileWidth = Math.Min(tileWidth, tileHeight);
63      tileHeight = tileWidth; // draw square tiles
64
65      using (var g = Graphics.FromImage(lawnImage)) {
66        g.Clear(DefaultBackColor);
67        for (int i = 0; i < Content.Length; i++)
68          for (int j = 0; j < Content.Width; j++)
69            if (mowed[i, j]) {
70              g.FillRectangle(Brushes.Chartreuse, tileWidth * j, tileHeight * i, tileWidth, tileHeight);
71            } else {
72              g.FillRectangle(Brushes.DarkGreen, tileWidth * j, tileHeight * i, tileWidth, tileHeight);
73            }
74      }
75    }
76  }
77}
Note: See TracBrowser for help on using the repository browser.