Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1265_HeuristicLab.Visualization/HeuristicLab.Visualization/3.3/Primitives/Grid.cs @ 17328

Last change on this file since 17328 was 13753, checked in by jkarder, 9 years ago

#1265: worked on visualization

  • brought back support for SnapToGrid methods
  • updated license headers
File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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;
24
25namespace HeuristicLab.Visualization {
26  public class Grid : PrimitiveBase, IGrid {
27    protected const int CellSpacing = 10;
28
29    public virtual PointD Origin { get; set; }
30    public virtual double XPrecision { get; set; }
31    public virtual double YPrecision { get; set; }
32
33    public virtual PointD GetBottomLeftGridPoint(PointD point) {
34      var nx = (int)Math.Floor((point.X - Origin.X) / XPrecision);
35      var ny = (int)Math.Floor((point.Y - Origin.Y) / YPrecision);
36      return new PointD(Origin.X + nx * XPrecision, Origin.Y + ny * YPrecision);
37    }
38
39    public virtual PointD GetUpperRightGridPoint(PointD point) {
40      var nx = (int)Math.Ceiling((point.X - Origin.X) / XPrecision);
41      var ny = (int)Math.Ceiling((point.Y - Origin.Y) / YPrecision);
42      return new PointD(Origin.X + nx * XPrecision, Origin.Y + ny * YPrecision);
43    }
44
45    public virtual PointD GetNearestGridPoint(PointD point) {
46      var bl = GetBottomLeftGridPoint(point);
47      var ur = GetUpperRightGridPoint(point);
48      return new PointD(
49        Math.Abs(bl.X - point.X) <= Math.Abs(ur.X - point.X) ? bl.X : ur.X,
50        Math.Abs(bl.Y - point.Y) <= Math.Abs(ur.Y - point.Y) ? bl.Y : ur.Y);
51    }
52
53    public Grid(IChart chart) : this(chart, 1.0) { }
54    public Grid(IChart chart, double precision) : base(chart) {
55      Origin = new PointD(0, 0);
56      XPrecision = precision;
57      YPrecision = precision;
58    }
59
60    public override bool ContainsPoint(PointD point) {
61      return false;
62    }
63
64    public override void Move(Offset delta) { }
65
66    public override void Move(PointD point, Offset delta) { }
67
68    public override void SnapToGrid(IGrid grid) { }
69
70    public override void SnapToGrid(PointD point, IGrid grid) { }
71
72    public override void Draw(Graphics graphics) {
73      if (!(Chart.Size.Width > 0) || !(Chart.Size.Height > 0)) return;
74      graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
75      var pen = new Pen(Color.LightGray, 1.0f);
76      var pixelSize = Chart.TransformWorldToPixel(Chart.Size); // size of the drawing area in pixel coordinates
77
78      var numberOfXParallelLines = (int)Math.Floor(pixelSize.Height / (double)CellSpacing); // how many x parallel lines can be drawn
79      var numberOfYParallelLines = (int)Math.Floor(pixelSize.Width / (double)CellSpacing); // how many y parallel lines can be drawn
80      if (numberOfXParallelLines <= 0 || numberOfYParallelLines <= 0) return;
81      var cellWorldSize = new SizeD(Chart.Size.Width / numberOfYParallelLines, Chart.Size.Height / numberOfXParallelLines); // the cellSize in world coordinates
82      cellWorldSize.Width = Math.Pow(10, Math.Floor(Math.Log10(cellWorldSize.Width)));
83      cellWorldSize.Height = Math.Pow(10, Math.Floor(Math.Log10(cellWorldSize.Width)));
84      var cellPixelSize = Chart.TransformWorldToPixel(cellWorldSize);
85      while (cellPixelSize.Width < 25 || cellPixelSize.Height < 25) {
86        if (cellPixelSize.Width < 25) cellWorldSize.Width *= 2;
87        if (cellPixelSize.Height < 25) cellWorldSize.Height *= 2;
88        cellPixelSize = Chart.TransformWorldToPixel(cellWorldSize);
89        if (cellPixelSize.Width < 25) cellWorldSize.Width *= 2.5;
90        if (cellPixelSize.Height < 25) cellWorldSize.Height *= 2.5;
91        cellPixelSize = Chart.TransformWorldToPixel(cellWorldSize);
92      }
93      var firstX = Math.Floor(Chart.LowerLeft.X / cellWorldSize.Width) * cellWorldSize.Width;
94      var firstY = Math.Floor(Chart.LowerLeft.Y / cellWorldSize.Height) * cellWorldSize.Height;
95      double x = firstX, y = firstY;
96      var axisyLabel = new Font(FontFamily.GenericMonospace, 7.0f, FontStyle.Regular, GraphicsUnit.Point, 0);
97      var axisxLabel = new Font(FontFamily.GenericMonospace, 7.0f, FontStyle.Regular, GraphicsUnit.Point, 0, true);
98      var axisDrawing = new SolidBrush(Color.DarkGray);
99      try {
100        var bottom = Chart.TransformWorldToPixel(Chart.Size).Height;
101        while (x <= Chart.LowerLeft.X + Chart.Size.Width) {
102          var start = Chart.TransformWorldToPixel(new PointD(x, firstY));
103          var end = Chart.TransformWorldToPixel(new PointD(x, Chart.LowerLeft.Y + Chart.Size.Height));
104          graphics.DrawLine(pen, start, end);
105          var axis = x.ToString("0.##");
106          double stringwidth = graphics.MeasureString(axis, axisyLabel).Width;
107          graphics.DrawString(axis, axisyLabel, axisDrawing, end.X - (int)(stringwidth / 2.0), bottom - 15);
108          x += cellWorldSize.Width;
109        }
110        while (y <= Chart.LowerLeft.Y + Chart.Size.Height) {
111          var start = Chart.TransformWorldToPixel(new PointD(firstX, y));
112          var end = Chart.TransformWorldToPixel(new PointD(Chart.LowerLeft.X + Chart.Size.Width, y));
113          graphics.DrawLine(pen, start, end);
114          graphics.DrawString(y.ToString("0.##"), axisxLabel, axisDrawing, 0, start.Y);
115          y += cellWorldSize.Height;
116        }
117      } finally {
118        axisDrawing.Dispose();
119        axisxLabel.Dispose();
120        axisyLabel.Dispose();
121      }
122    }
123  }
124}
Note: See TracBrowser for help on using the repository browser.