001: package org.ofbiz.rules.utensil;
002:
003: /**
004: * <p><b>Title:</b> Linear Calculator
005: * <p><b>Description:</b> None
006: * <p>Copyright (c) 1999 Steven J. Metsker.
007: * <p>Copyright (c) 2001 The Open For Business Project - www.ofbiz.org
008: *
009: * <p>Permission is hereby granted, free of charge, to any person obtaining a
010: * copy of this software and associated documentation files (the "Software"),
011: * to deal in the Software without restriction, including without limitation
012: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
013: * and/or sell copies of the Software, and to permit persons to whom the
014: * Software is furnished to do so, subject to the following conditions:
015: *
016: * <p>The above copyright notice and this permission notice shall be included
017: * in all copies or substantial portions of the Software.
018: *
019: * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
020: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
021: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
022: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
023: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
024: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
025: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
026: *
027: * <br>
028: * <p>A LinearCalculator models two variables that vary
029: * linearly with each other.
030: *
031: * For example, Fahrenheit and Celsius temperate scales vary
032: * linearly. Fahrenheit temperature varies from 32 to 212 as
033: * Celsius varies 0 to 100. A LinearCalculator can model the
034: * whole scale, if it is created as:
035: *
036: * <blockquote><pre>
037: *
038: * LinearCalculator lc =
039: * new LinearCalculator(32, 212, 0, 100);
040: * System.out.println(lc.calculateYforGivenX(68));
041: *
042: * </pre></blockquote>
043: *
044: * @author Steven J. Metsker
045: * @version 1.0
046: */
047: public class LinearCalculator {
048: double xFrom;
049: double xTo;
050: double yFrom;
051: double yTo;
052:
053: /**
054: * Create a LinearCalculator from known points on two scales.
055: *
056: * @param double xFrom
057: * @param double xTo
058: * @param double yFrom
059: * @param double yTo
060: */
061: public LinearCalculator(double xFrom, double xTo, double yFrom,
062: double yTo) {
063: this .xFrom = xFrom;
064: this .xTo = xTo;
065: this .yFrom = yFrom;
066: this .yTo = yTo;
067: }
068:
069: /**
070: * Return the value on the first scale, corresponding to the given
071: * value on the second scale.
072: *
073: * @return the value on the first scale, corresponding to the given
074: * value on the second scale
075: */
076: public double calculateXforGivenY(double y) {
077: if (yTo == yFrom) {
078: return (xFrom + xTo) / 2;
079: }
080: return (y - yFrom) / (yTo - yFrom) * (xTo - xFrom) + xFrom;
081: }
082:
083: /**
084: * Return the value on the second scale, corresponding to the given
085: * value on the first scale.
086: *
087: * @return the value on the second scale, corresponding to the given
088: * value on the first scale
089: */
090: public double calculateYforGivenX(double x) {
091: if (xTo == xFrom) {
092: return (yFrom + yTo) / 2;
093: }
094: return (x - xFrom) / (xTo - xFrom) * (yTo - yFrom) + yFrom;
095: }
096:
097: /**
098: * Show the example in the class comment.
099: *
100: * @param args ignored.
101: */
102: public static void main(String args[]) {
103: LinearCalculator lc = new LinearCalculator(32, 212, 0, 100);
104:
105: System.out.println(lc.calculateYforGivenX(68));
106: }
107:
108: /**
109: * Return a textual description of this object.
110: *
111: * @return a textual description of this object
112: */
113: public String toString() {
114: return "" + xFrom + ":" + xTo + "::" + yFrom + ":" + yTo;
115: }
116: }
|