01: /*
02: * Copyright 2003-2004, Franz-Josef Elmer, All rights reserved
03: *
04: * This library is free software; you can redistribute it and/or modify
05: * it under the terms of the GNU Lesser General Public License as published by
06: * the Free Software Foundation; either version 2.1 of the License, or
07: * (at your option) any later version.
08: *
09: * 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,
57: boolean yLogScale, DataPoint dataPoint1,
58: GraphPoint graphPoint1, DataPoint dataPoint2,
59: GraphPoint graphPoint2) {
60: _xLogScale = xLogScale;
61: double d1 = Util.log(dataPoint1.getX(), xLogScale);
62: double dd = Util.log(dataPoint2.getX(), xLogScale) - d1;
63: check(dd, "data", "x", d1);
64: _xScale = (graphPoint2.getX() - graphPoint1.getX()) / dd;
65: check(_xScale, "graphical", "x", graphPoint1.getX());
66: _xOffset = graphPoint1.getX() - d1 * _xScale;
67:
68: _yLogScale = yLogScale;
69: d1 = Util.log(dataPoint1.getY(), yLogScale);
70: dd = Util.log(dataPoint2.getY(), yLogScale) - d1;
71: check(dd, "data", "y", d1);
72: _yScale = (graphPoint2.getY() - graphPoint1.getY()) / dd;
73: check(_yScale, "graphical", "y", graphPoint1.getY());
74: _yOffset = graphPoint1.getY() - d1 * _yScale;
75: }
76:
77: private void check(double valueToCheck, String type, String axis,
78: double value) {
79: if (valueToCheck == 0) {
80: throw new IllegalArgumentException("The " + type
81: + " reference points in " + axis
82: + " must be different; both are " + value);
83: }
84: }
85:
86: public GraphPoint transformToGraph(DataPoint point) {
87: return new GraphPoint(_xOffset
88: + Util.log(point.getX(), _xLogScale) * _xScale,
89: _yOffset + Util.log(point.getY(), _yLogScale) * _yScale);
90: }
91:
92: public DataPoint transformToData(GraphPoint point) {
93: return new DataPoint(Util.exp((point.getX() - _xOffset)
94: / _xScale, _xLogScale), Util.exp(
95: (point.getY() - _yOffset) / _yScale, _yLogScale));
96: }
97: }
|