KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcckit > transformation > CartesianTransformation


1 /*
2  * Copyright 2003-2004, Franz-Josef Elmer, All rights reserved
3  *
4  * This library is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details
13  * (http://www.gnu.org/copyleft/lesser.html).
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package jcckit.transformation;
20
21 import jcckit.data.DataPoint;
22 import jcckit.graphic.GraphPoint;
23 import jcckit.util.Util;
24
25 /**
26  * Two-dimensional Cartesian transformation. The two independent
27  * transformations for the x-axis and the y-axis can be logarithmic
28  * from data coordinates to device-independent coordinates in order to
29  * realize diagrams with logarithmic scales.
30  *
31  * @author Franz-Josef Elmer
32  */

33 public class CartesianTransformation implements Transformation {
34   private final boolean _xLogScale;
35   private final double _xOffset;
36   private final double _xScale;
37   private final boolean _yLogScale;
38   private final double _yOffset;
39   private final double _yScale;
40
41   /**
42    * Creates an instance from the specified reference points.
43    * Note, that the reference points must differ in x and y coordinates
44    * otherwise a transformation would not be possible.
45    * @param xLogScale <tt>true</tt> if logarithmic x axis.
46    * @param yLogScale <tt>true</tt> if logarithmic y axis.
47    * @param dataPoint1 First reference point in data coordinates.
48    * @param graphPoint1 First reference point in device-independent
49    * coordinates.
50    * @param dataPoint2 Second reference point in data coordinates.
51    * @param graphPoint2 Second reference point in device-independent
52    * coordinates.
53    * @throws IllegalArgumentException if transformation in at least
54    * one of both directions is not possible.
55    */

56   public CartesianTransformation(boolean xLogScale, boolean yLogScale,
57                            DataPoint dataPoint1, GraphPoint graphPoint1,
58                            DataPoint dataPoint2, GraphPoint graphPoint2) {
59     _xLogScale = xLogScale;
60     double d1 = Util.log(dataPoint1.getX(), xLogScale);
61     double dd = Util.log(dataPoint2.getX(), xLogScale) - d1;
62     check(dd, "data", "x", d1);
63     _xScale = (graphPoint2.getX() - graphPoint1.getX()) / dd;
64     check(_xScale, "graphical", "x", graphPoint1.getX());
65     _xOffset = graphPoint1.getX() - d1 * _xScale;
66
67     _yLogScale = yLogScale;
68     d1 = Util.log(dataPoint1.getY(), yLogScale);
69     dd = Util.log(dataPoint2.getY(), yLogScale) - d1;
70     check(dd, "data", "y", d1);
71     _yScale = (graphPoint2.getY() - graphPoint1.getY()) / dd;
72     check(_yScale, "graphical", "y", graphPoint1.getY());
73     _yOffset = graphPoint1.getY() - d1 * _yScale;
74   }
75
76   private void check(double valueToCheck, String JavaDoc type, String JavaDoc axis,
77                      double value) {
78     if (valueToCheck == 0) {
79       throw new IllegalArgumentException JavaDoc("The " + type
80           + " reference points in " + axis + " must be different; both are "
81           + value);
82     }
83   }
84
85   public GraphPoint transformToGraph(DataPoint point) {
86     return new GraphPoint(
87         _xOffset + Util.log(point.getX(), _xLogScale) * _xScale,
88         _yOffset + Util.log(point.getY(), _yLogScale) * _yScale);
89   }
90
91   public DataPoint transformToData(GraphPoint point) {
92     return new DataPoint(
93         Util.exp((point.getX() - _xOffset) / _xScale, _xLogScale),
94         Util.exp((point.getY() - _yOffset) / _yScale, _yLogScale));
95   }
96 }
97
Popular Tags