The final restructured version for automatic build using maven tycho
[Mograsim.git] / plugins / net.mograsim.logic.model / src / net / mograsim / logic / model / model / components / OrientationCalculator.java
1 package net.mograsim.logic.model.model.components;
2
3 /**
4  * This class simplifies the calculation of coordinates, especially for model components.
5  * <p>
6  * Supply it with the original width and height and an orientation, and use the methods {@link #newX(double, double)} and
7  * {@link #newY(double, double)} to retrieve the new coordinates, relative to the upper left corner of {@link Orientation#RIGHT}. The
8  * {@link #height()} and {@link #width()} methods return the width and height in the new orientation.
9  * <p>
10  * This is meant to be used in the context of a a classic display coordinate system, as done in the {@link ModelComponent}s.
11  *
12  * @see Orientation
13  * @author Christian Femers
14  */
15 public class OrientationCalculator
16 {
17         final Orientation o;
18         final double oldWHalf;
19         final double oldHHalf;
20         final double w;
21         final double h;
22         final double wHalf;
23         final double hHalf;
24
25         public OrientationCalculator(Orientation o, double width, double height)
26         {
27                 this.o = o;
28                 this.oldWHalf = width / 2;
29                 this.oldHHalf = height / 2;
30
31                 if (o.swapsWidthAndHeight())
32                 {
33                         w = height;
34                         h = width;
35                         wHalf = oldHHalf;
36                         hHalf = oldWHalf;
37                 } else
38                 {
39                         w = width;
40                         h = height;
41                         wHalf = oldWHalf;
42                         hHalf = oldHHalf;
43                 }
44         }
45
46         /**
47          * Returns the new height (that equals the old width if {@link Orientation#swapsWidthAndHeight()} is true)
48          */
49         public double height()
50         {
51                 return h;
52         }
53
54         /**
55          * Returns the new width (that equals the old height if {@link Orientation#swapsWidthAndHeight()} is true)
56          */
57         public double width()
58         {
59                 return w;
60         }
61
62         public double newX(double x, double y)
63         {
64                 return (x - oldWHalf) * o.trans11 + (y - oldHHalf) * o.trans12 + wHalf;
65         }
66
67         public double newY(double x, double y)
68         {
69                 return (x - oldWHalf) * o.trans21 + (y - oldHHalf) * o.trans22 + hHalf;
70         }
71
72         public final Orientation getOrientation()
73         {
74                 return o;
75         }
76 }