Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Refactoring-Ticket419/HeuristicLab.Visualization/Transform.cs @ 1235

Last change on this file since 1235 was 862, checked in by shofstad, 16 years ago

added code comments (#406)

File size: 2.6 KB
Line 
1using System.Drawing;
2
3namespace HeuristicLab.Visualization {
4  public static class Transform {
5
6    /// <summary>
7    /// Screen to world transformations
8    /// </summary>
9    /// <param name="rect"></param>
10    /// <param name="viewport"></param>
11    /// <param name="clippingArea"></param>
12    /// <returns></returns>
13    public static RectangleD ToWorld(Rectangle rect, Rectangle viewport, RectangleD clippingArea) {
14      double x1 = ToWorldX(rect.Left, viewport, clippingArea);
15      double y1 = ToWorldY(rect.Bottom, viewport, clippingArea);
16      double x2 = ToWorldX(rect.Right, viewport, clippingArea);
17      double y2 = ToWorldY(rect.Top, viewport, clippingArea);
18      return new RectangleD(x1, y1, x2, y2);
19    }
20
21    public static PointD ToWorld(Point point, Rectangle viewport, RectangleD clippingArea) {
22      double x = ToWorldX(point.X, viewport, clippingArea);
23      double y = ToWorldY(point.Y, viewport, clippingArea);
24      return new PointD(x, y);
25    }
26
27    public static double ToWorldX(int x, Rectangle viewport, RectangleD clippingArea) {
28      return clippingArea.X1 + clippingArea.Width/viewport.Width*(x - viewport.Left);
29    }
30
31    public static double ToWorldY(int y, Rectangle viewport, RectangleD clippingArea) {
32      return clippingArea.Y1 - clippingArea.Height/viewport.Height*(y - viewport.Bottom);
33    }
34
35    /// <summary>
36    /// World to screen transformations
37    /// </summary>
38    /// <param name="rect"></param>
39    /// <param name="viewport"></param>
40    /// <param name="clippingArea"></param>
41    /// <returns></returns>
42    public static Rectangle ToScreen(RectangleD rect, Rectangle viewport, RectangleD clippingArea) {
43      int left = ToScreenX(rect.X1, viewport, clippingArea);
44      int bottom = ToScreenY(rect.Y1, viewport, clippingArea);
45      int right = ToScreenX(rect.X2, viewport, clippingArea);
46      int top = ToScreenY(rect.Y2, viewport, clippingArea);
47      return new Rectangle(left, top, right - left + 1, bottom - top + 1);
48    }
49
50    public static Point ToScreen(PointD point, Rectangle viewport, RectangleD clippingArea) {
51      int x = ToScreenX(point.X, viewport, clippingArea);
52      int y = ToScreenY(point.Y, viewport, clippingArea);
53      return new Point(x, y);
54    }
55
56    public static int ToScreenX(double x, Rectangle viewport, RectangleD clippingArea) {
57      return (int)(viewport.Left + viewport.Width/clippingArea.Width*(x - clippingArea.X1));
58    }
59
60    public static int ToScreenY(double y, Rectangle viewport, RectangleD clippingArea) {
61      return (int)(viewport.Bottom - viewport.Height/clippingArea.Height*(y - clippingArea.Y1));
62    }
63  }
64}
Note: See TracBrowser for help on using the repository browser.