1 19 package jcckit.transformation; 20 21 import jcckit.data.DataPoint; 22 import jcckit.graphic.GraphPoint; 23 import jcckit.util.Util; 24 25 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 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 type, String axis, 77 double value) { 78 if (valueToCheck == 0) { 79 throw new IllegalArgumentException ("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 |