1 | using System.Drawing;
|
---|
2 |
|
---|
3 | namespace HeuristicLab.Visualization {
|
---|
4 | /// <summary>
|
---|
5 | /// GDI (or screen) coordinate system
|
---|
6 | /// The GDI coordinate system is specified in pixels. X goes from left to right
|
---|
7 | /// and Y goes from top to bottom.
|
---|
8 | ///
|
---|
9 | /// World coordinate system
|
---|
10 | /// A world coordinate system can be freely specified. X goes from left to
|
---|
11 | /// right and Y goes from bottom to top.
|
---|
12 | ///
|
---|
13 | /// The transformation between world- and screen-coordinate systems is done using
|
---|
14 | /// a view port and a clipping area.
|
---|
15 | /// </summary>
|
---|
16 | public static class Transform {
|
---|
17 | /// <summary>
|
---|
18 | /// Transforms a rectangle in screen coordinates to world coordinates
|
---|
19 | /// </summary>
|
---|
20 | /// <param name="rect">The rectangle in screen coordinates that should be transformed</param>
|
---|
21 | /// <param name="viewport">The target view port</param>
|
---|
22 | /// <param name="clippingArea">The target clipping area</param>
|
---|
23 | /// <returns>The rectangle rect transformed to world coordinates</returns>
|
---|
24 | public static RectangleD ToWorld(Rectangle rect, Rectangle viewport, RectangleD clippingArea) {
|
---|
25 | double x1 = ToWorldX(rect.Left, viewport, clippingArea);
|
---|
26 | double y1 = ToWorldY(rect.Bottom, viewport, clippingArea);
|
---|
27 | double x2 = ToWorldX(rect.Right, viewport, clippingArea);
|
---|
28 | double y2 = ToWorldY(rect.Top, viewport, clippingArea);
|
---|
29 | return new RectangleD(x1, y1, x2, y2);
|
---|
30 | }
|
---|
31 |
|
---|
32 | /// <summary>
|
---|
33 | /// Transforms a 2d point in screen coordinates to world coordinates
|
---|
34 | /// </summary>
|
---|
35 | /// <param name="point">The point in screen coordinates that should be transformed</param>
|
---|
36 | /// <param name="viewport">The target view port</param>
|
---|
37 | /// <param name="clippingArea">The target clipping area</param>
|
---|
38 | /// <returns>The point transformed to world coordinates</returns>
|
---|
39 | public static PointD ToWorld(Point point, Rectangle viewport, RectangleD clippingArea) {
|
---|
40 | double x = ToWorldX(point.X, viewport, clippingArea);
|
---|
41 | double y = ToWorldY(point.Y, viewport, clippingArea);
|
---|
42 | return new PointD(x, y);
|
---|
43 | }
|
---|
44 |
|
---|
45 | /// <summary>
|
---|
46 | /// Transforms a point on the X-axis in screen coordinates to world coordinates
|
---|
47 | /// </summary>
|
---|
48 | /// <param name="x">The point on the X-axis in screen coordinates that should be transformed</param>
|
---|
49 | /// <param name="viewport">The target view port</param>
|
---|
50 | /// <param name="clippingArea">The target clipping area</param>
|
---|
51 | /// <returns>The point transformed to world coordinates</returns>
|
---|
52 | public static double ToWorldX(int x, Rectangle viewport, RectangleD clippingArea) {
|
---|
53 | return clippingArea.X1 + clippingArea.Width/viewport.Width*(x - viewport.Left);
|
---|
54 | }
|
---|
55 |
|
---|
56 | /// <summary>
|
---|
57 | /// Transforms a point on the Y-axis in screen coordinates to world coordinates
|
---|
58 | /// </summary>
|
---|
59 | /// <param name="y">The point on the Y-axis in screen coordinates that should be transformed</param>
|
---|
60 | /// <param name="viewport">The target view port</param>
|
---|
61 | /// <param name="clippingArea">The target clipping area</param>
|
---|
62 | /// <returns>The point transformed to world coordinates</returns>
|
---|
63 | public static double ToWorldY(int y, Rectangle viewport, RectangleD clippingArea) {
|
---|
64 | return clippingArea.Y1 - clippingArea.Height/viewport.Height*(y - viewport.Bottom);
|
---|
65 | }
|
---|
66 |
|
---|
67 | /// <summary>
|
---|
68 | /// Transforms a rectangle in world coordinates to screen coordinates
|
---|
69 | /// </summary>
|
---|
70 | /// <param name="rect">The rectangle in world coordinates that should be transformed</param>
|
---|
71 | /// <param name="viewport">The target view port</param>
|
---|
72 | /// <param name="clippingArea">The target clipping area</param>
|
---|
73 | /// <returns>The rectangle rect transformed to screen coordinates</returns>
|
---|
74 | public static Rectangle ToScreen(RectangleD rect, Rectangle viewport, RectangleD clippingArea) {
|
---|
75 | int left = ToScreenX(rect.X1, viewport, clippingArea);
|
---|
76 | int bottom = ToScreenY(rect.Y1, viewport, clippingArea);
|
---|
77 | int right = ToScreenX(rect.X2, viewport, clippingArea);
|
---|
78 | int top = ToScreenY(rect.Y2, viewport, clippingArea);
|
---|
79 | return new Rectangle(left, top, right - left , bottom - top );
|
---|
80 | }
|
---|
81 |
|
---|
82 | /// <summary>
|
---|
83 | /// Transforms a 2d point in world coordinates to screen coordinates
|
---|
84 | /// </summary>
|
---|
85 | /// <param name="point">The point in world coordinates that should be transformed</param>
|
---|
86 | /// <param name="viewport">The target view port</param>
|
---|
87 | /// <param name="clippingArea">The target clipping area</param>
|
---|
88 | /// <returns>The point transformed to screen coordinates</returns>
|
---|
89 | public static Point ToScreen(PointD point, Rectangle viewport, RectangleD clippingArea) {
|
---|
90 | int x = ToScreenX(point.X, viewport, clippingArea);
|
---|
91 | int y = ToScreenY(point.Y, viewport, clippingArea);
|
---|
92 | return new Point(x, y);
|
---|
93 | }
|
---|
94 |
|
---|
95 | /// <summary>
|
---|
96 | /// Transforms a point on the X-axis in world coordinates to screen coordinates
|
---|
97 | /// </summary>
|
---|
98 | /// <param name="x">The point on the X-axis in world coordinates that should be transformed</param>
|
---|
99 | /// <param name="viewport">The target view port</param>
|
---|
100 | /// <param name="clippingArea">The target clipping area</param>
|
---|
101 | /// <returns>The point transformed to screen coordinates</returns>
|
---|
102 | public static int ToScreenX(double x, Rectangle viewport, RectangleD clippingArea) {
|
---|
103 | return (int)(viewport.Left + viewport.Width/clippingArea.Width*(x - clippingArea.X1));
|
---|
104 | }
|
---|
105 |
|
---|
106 | /// <summary>
|
---|
107 | /// Transforms a point on the Y-axis in world coordinates to screen coordinates
|
---|
108 | /// </summary>
|
---|
109 | /// <param name="y">The point on the Y-axis in world coordinates that should be transformed</param>
|
---|
110 | /// <param name="viewport">The target view port</param>
|
---|
111 | /// <param name="clippingArea">The target clipping area</param>
|
---|
112 | /// <returns>The point transformed to screen coordinates</returns>
|
---|
113 | public static int ToScreenY(double y, Rectangle viewport, RectangleD clippingArea) {
|
---|
114 | return (int)(viewport.Bottom - viewport.Height/clippingArea.Height*(y - clippingArea.Y1));
|
---|
115 | }
|
---|
116 | }
|
---|
117 | } |
---|