/************************************************************************** /* This class is part of the Java Plotlib Toolkit. /* /* Copyright (c) 1999-2004 by Bernhard Bablok (bablokb@gmx.de) /* /* This program is free software; you can redistribute it and/or modify /* it under the terms of the GNU General Public License as published /* by the Free Software Foundation; either version 2 of the License or /* (at your option) any later version. /* /* This program is distributed in the hope that it will be useful, but /* WITHOUT ANY WARRANTY; without even the implied warranty of /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /* GNU Library General Public License for more details. /* /* You should have received a copy of the GNU General Public License /* along with this program; see the file COPYING. If not, write to /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, /* Boston, MA 02111-1307 USA /**************************************************************************/ package de.bablokb.plotlib.plot; import java.util.*; import java.awt.*; import java.awt.geom.*; import de.bablokb.plotlib.core.*; import de.bablokb.plotlib.data.*; /** Plot a function y=f(x) or (x,y)=f(t). @version $Revision: 1.4 $ @author $Author: bablokb $ */ public class FunctionPlot extends LinePlot { /** Minimum x (respective t) value. Defaults to minimum value of range. */ private double iMinValue = Double.NaN; /** Maximum x (respective t) value. Defaults to maximum value of range. */ private double iMaxValue = Double.NaN; /** Interval size to calculate values for. This defaults to the unit in device coordinates. */ private double iDelta = Double.NaN; /** Function object. */ private Yfx iFkt = null; ////////////////////////////////////////////////////////////////////////////// /** Construct a FunctionPlot-object from given function, value range and interval size. */ public FunctionPlot(Yfx f, double minVal, double maxVal, double delta) { iFkt = f; iMinValue = minVal; iMaxValue = maxVal; iDelta = delta; } ////////////////////////////////////////////////////////////////////////////// /** Construct a FunctionPlot-object from given function. */ public FunctionPlot(Yfx f) { iFkt = f; } ////////////////////////////////////////////////////////////////////////////// /** Set the range and interval size. */ public void setRange(double minVal, double maxVal, double delta) { iMinValue = minVal; iMaxValue = maxVal; iDelta = delta; } ////////////////////////////////////////////////////////////////////////////// /** Calculate values in the given range.

FIXME: Should we clear iData in case iData != null? */ public void createData(PlottingContext pc) { if (iFkt == null) throw new NoFunctionException(); double min=iMinValue, max=iMaxValue, delta=iDelta; if (Double.isNaN(iMinValue)) min = pc.getRange().getMinX(); if (Double.isNaN(iMaxValue)) max = pc.getRange().getMaxX(); if (Double.isNaN(iDelta)) delta = pc.getXratio(); if (iData == null) iData = new Dataset(new ArrayList((int) ((max-min)/delta))); for (double x=min+delta;x<=max;x+=delta) { Point2D p = iFkt.f(x); iData.add(p); } } ////////////////////////////////////////////////////////////////////////////// /** Plot the function. Implements AbstractPlottable.plotObjects. */ public void plotObjects(PlottingContext pc) { if (iData == null) createData(pc); super.plotObjects(pc); } ////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// /** Interface for functions. The method returning a Point2D given a x-coordinate must be implemented. The same interface can be used to plot function of the form (x,y) = f(t), since the interface does not enforce that the x-value of the Point2D must be equal to the argument. */ static public interface Yfx { /** Return a Point2D given a value. */ public Point2D f(double x); } }